N
NKH
Hi,
I am into developing a Mail Merge equivalent COM Addin for
MS-Word in VB.I am using the following code to generate
the resultant document
g_objDoc is Word.Document object
G-objCopy is a Word.Application object
If g_objDoc.Content.Characters.Count > 1 Then
inof = g_objDoc.MailMerge.Fields.Count
g_objCopy.Documents.Add , , wdNewBlankDocument
'To copy the header and footer to the new document
If g_objDoc.Sections(1).Headers wdHeaderFooterPrimary).
Exists = True Then
g_objDoc.Sections(1).Headers(wdHeaderFooterPrimary).
Range.CopyAsPicture
g_objCopy.ActiveDocument.Sections(1).Headers
(wdHeaderFooterPrimary).Range.Paste
End If
If g_objDoc.Sections(1).Footers(wdHeaderFooterPrimary).
Exists = True Then
g_objDoc.Sections(1).Footers(wdHeaderFooterPrimary).
Range.CopyAsPicture
g_objCopy.ActiveDocument.Sections(1).Footers
(wdHeaderFooterPrimary).Range.Paste
End If
'To insert values into the new document based on the
bookmarks in the base document
'Recordset which contains the data from the Database View
g_rsGenerate.MoveFirst
For iDocCount = 1 To g_iTotalDocCount 'total no. of
records
For iCount = 0 To inof - 1
iBookmarksCount = fnGetBookmark(iCount)
If CStr(g_objDoc.Bookmarks(iBookmarksCount).Range)
<> "" Then
g_objDoc.Bookmarks(iBookmarksCount).Range.CopyAsPicture
g_objCopy.Selection.Paste
g_objCopy.Selection.InsertAfter " "
Else
bNoRangeExist = True
End If
'To Enter the values from the database
If bNoRangeExist = True Then
If iCount = 0 Then
prInsertValue True, g_rsGenerate.Fields(iCount).Value
Else
prInsertValue False, g_rsGenerate.Fields
(iCount).Value
End If
bNoRangeExist = False
Else
prInsertValue False, g_rsGenerate.Fields(iCount).Value
End If
g_objCopy.Selection.TypeText " "
Next iCount
'To copy the contents after the last inserted mergefield
If iCount <> g_objDoc.Bookmarks.Count Then
iBookmarksCount = fnGetBookmark(iCount)
g_objDoc.Bookmarks(iBookmarksCount).Range.CopyAsPicture
g_objCopy.Selection.Paste
End If
If fnCheckSPBPresent(g_objDoc) = False Then
'Insert a page break only if there are further records to
be printed
If iDocCount <> g_rsGenerate.RecordCount Then
g_objCopy.Selection.InsertBreak (1)
End If
Else
If iDocCount <> g_rsGenerate.RecordCount Then
g_objCopy.Selection.InsertBreak (wdLineBreak)
End If
End If
g_rsGenerate.MoveNext
End If
DoEvents
Next iDocCount
'To delete the merge fields inserted in the copied document
Call prDeleteMergeFields(g_objCopy)
DoEvents
bReportCompleted = True
g_objCopy.Visible = True
my real problem is it's taking a lot off time to get the
final output document.
Can anyone please suggest a solution for this?
I am taking data from database view.
I am into developing a Mail Merge equivalent COM Addin for
MS-Word in VB.I am using the following code to generate
the resultant document
g_objDoc is Word.Document object
G-objCopy is a Word.Application object
If g_objDoc.Content.Characters.Count > 1 Then
inof = g_objDoc.MailMerge.Fields.Count
g_objCopy.Documents.Add , , wdNewBlankDocument
'To copy the header and footer to the new document
If g_objDoc.Sections(1).Headers wdHeaderFooterPrimary).
Exists = True Then
g_objDoc.Sections(1).Headers(wdHeaderFooterPrimary).
Range.CopyAsPicture
g_objCopy.ActiveDocument.Sections(1).Headers
(wdHeaderFooterPrimary).Range.Paste
End If
If g_objDoc.Sections(1).Footers(wdHeaderFooterPrimary).
Exists = True Then
g_objDoc.Sections(1).Footers(wdHeaderFooterPrimary).
Range.CopyAsPicture
g_objCopy.ActiveDocument.Sections(1).Footers
(wdHeaderFooterPrimary).Range.Paste
End If
'To insert values into the new document based on the
bookmarks in the base document
'Recordset which contains the data from the Database View
g_rsGenerate.MoveFirst
For iDocCount = 1 To g_iTotalDocCount 'total no. of
records
For iCount = 0 To inof - 1
iBookmarksCount = fnGetBookmark(iCount)
If CStr(g_objDoc.Bookmarks(iBookmarksCount).Range)
<> "" Then
g_objDoc.Bookmarks(iBookmarksCount).Range.CopyAsPicture
g_objCopy.Selection.Paste
g_objCopy.Selection.InsertAfter " "
Else
bNoRangeExist = True
End If
'To Enter the values from the database
If bNoRangeExist = True Then
If iCount = 0 Then
prInsertValue True, g_rsGenerate.Fields(iCount).Value
Else
prInsertValue False, g_rsGenerate.Fields
(iCount).Value
End If
bNoRangeExist = False
Else
prInsertValue False, g_rsGenerate.Fields(iCount).Value
End If
g_objCopy.Selection.TypeText " "
Next iCount
'To copy the contents after the last inserted mergefield
If iCount <> g_objDoc.Bookmarks.Count Then
iBookmarksCount = fnGetBookmark(iCount)
g_objDoc.Bookmarks(iBookmarksCount).Range.CopyAsPicture
g_objCopy.Selection.Paste
End If
If fnCheckSPBPresent(g_objDoc) = False Then
'Insert a page break only if there are further records to
be printed
If iDocCount <> g_rsGenerate.RecordCount Then
g_objCopy.Selection.InsertBreak (1)
End If
Else
If iDocCount <> g_rsGenerate.RecordCount Then
g_objCopy.Selection.InsertBreak (wdLineBreak)
End If
End If
g_rsGenerate.MoveNext
End If
DoEvents
Next iDocCount
'To delete the merge fields inserted in the copied document
Call prDeleteMergeFields(g_objCopy)
DoEvents
bReportCompleted = True
g_objCopy.Visible = True
my real problem is it's taking a lot off time to get the
final output document.
Can anyone please suggest a solution for this?
I am taking data from database view.