We take pride in your success. We let our positivity drive us, day in and out. Talk to us at Mindfire to know us more.

Software Technology Tips

Lets say we have a word template file and we need to populate the template for "n" number of records and display a single large word document , instead of "n" separate documents.
 
So after creating the separate documents we need to merge them and create a single document with multiple pages.
 
Following code snippet demonstrates how to do it in VB.NET
[VB.NET CODE STARTS]
 
'== Declare variables
Dim fileArray as new ArrayList
Dim intCount As Integer = 0
 
'== Create an object of WORD.
 
Dim objWord As Object
objWord = CreateObject("Word.Application") ' 
 
'== Loop through the collection of documents and add to main word document 

 For Each fi As System.IO.FileInfo In dirInfo.GetFiles("*.doc") '  dirInfo : is the directory where all the separate docs are saved
 
   If Not fileArray.Contains(fi.FullName) Then ' Check if document is not already added.
          If intCount = 0 Then
             objWord.Documents.Add(fi.FullName) '  for the first document to be added use Documents.Add method
          Else
               objWord.Selection.InsertFile(filename:=fi.FullName, Range:="", ConfirmConversions:=False, Link:=False, Attachment:=False) ' next time onwards use InsertFile method
          End If
  
       objWord.Selection.EndKey(Unit:=6) ' set the control \ cursor to the end of the document to insert next in the new page
       intCount += 1 '  increment count by 1
       fiArr.Add(fi.FullName) ' add the merged document to the collection
 
   End If
 
 Next
 
'== Delete all separate documents from folder after merge
 
For Each fi As System.IO.FileInfo In dirInfo.GetFiles("*.doc")
   fi.Delete()
Next
 
'== Make the main document visible
 
If Not objWord.Visible Then
     objWord.Visible = True
End If
 

[VB.NET CODE ENDS]


Related Tags:

MS Word, Merge, VB.NET, Merge Word Document

Author: Amrita Dash

top

VB.NET

Let us Connect!

privacy

copyright (c) Mindfire Solutions 2007-2012. Login