Help Urgent-Regarding Mail merge

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.
 
C

Cindy M -WordMVP-

Hi Nkh,
my real problem is it's taking a lot off time to get the
final output document.
I'm not surprised...

I'll start at the top of your code and make some
suggestions that occur to me. Note that although you use
the term "mail merge", what you're doing is NOT mail merge.
That's a "reserved keyword" for Word, it means a very
specific thing.
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
This header/footer will always "Exist", in the terms of how
this functions. "Exists" doesn't have anything to do with
whether or not the header/footer contains text. It reflects
whether "Different first page" or "Odd and Even" are
activated.

Also, why "Copy as Picture" and then Paste? This is
certainly time-consuming. Better would be something along
these lines

If Len(g_objDoc.Sections(1).Headers
(wdHeaderFooterPrimary).Range.Text) > 0 Then
g_objCopy.ActiveDocument.Sections(1).Headers
(wdHeaderFooterPrimary).Range.FormattedText =
g_objDoc.Sections(1).Headers(wdHeaderFooterPrimary).
Range.FormattedText
End If
If CStr(g_objDoc.Bookmarks(iBookmarksCount).Range)
The above is "not logical". You're having a type mismatch
problem, I imagine, because you're trying to pickup the
Range, which will by default give back text. But you should
use .Range.Text and forget CStr.
g_objDoc.Bookmarks(iBookmarksCount).Range.CopyAsPicture
g_objCopy.Selection.Paste
g_objCopy.Selection.InsertAfter " "
Similar remark as with headers/footers; use
.FormattedText, instead. AVOID using the Selection object
at all costs. It's usually not necessary, and it's much
faster to set Range to Range. Also avoids screen flicker.
'To Enter the values from the database
It's not clear what the code under this comment is doing,
it's certainly not inserting data from the database that
I can see... But in any case, use .Range.Text = StringValue
to drop database data into a document being
created/processed as you're doing.

One further note: I'm not clear on why you're COPYING an
existing document. This would only make sense if this
document is something a user just finished setting up. If
this is NOT the case, then what you probably should be
doing is using the .ADD method to create a new document
from this one - an exact copy. It might even make sense to
have saved the original document as a TEMPLATE.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update
Sep 30 2003)
http://www.mvps.org/word

This reply is posted in the Newsgroup; please post any
follow question or reply in the newsgroup and not by e-mail
:)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top