merge documents

M

Mark Kurten

Description of program:

There is a form with labels on it. These labels are linked to Bookmarks in
a word document. So when the program runs, the word document gets
generated. My task is to re-write this, because it is a slow process. Can
someone please point me in the right direction. What should i be looking at
for a more effective way to merge data from VB into a word doc?

thanks.
 
D

Doug Robbins - Word MVP

Hi Mark,

Tell us more about the form and in particular how the information is being
passed to the bookmarks - i.e., the code that is being used. Copy and paste
it into a message that you post back here.

--
Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
 
P

Peter Hewett

Hi Mark Kurten

I assume you mean a VB Form with TextBox controls? Can you elaborate further on your
statement <My task is to re-write this, because it is a slow process>. What's slow about
it? Using Word as a COM component either from VB or another Office application is all
pretty standard. It should perform OK. Updating a bookmark in a Word document should be
quick.

You don't' say anything about the document you are producing. Is it large, are you adding
lots of graphic, is it complex etc. We need to know more about your specific problem
before we can be of help.

Cheers - Peter


Description of program:

There is a form with labels on it. These labels are linked to Bookmarks in
a word document. So when the program runs, the word document gets
generated. My task is to re-write this, because it is a slow process. Can
someone please point me in the right direction. What should i be looking at
for a more effective way to merge data from VB into a word doc?

thanks.

HTH + Cheers - Peter
 
M

Mark Kurten

The labels are populated with data from a database table.

I think there are extreme inefficiencies in this program. Unfortunately, I
haven't worked with integrating word before, so I can't pick them out
without first knowing how all this works.

I have enclosed an if/then which there are a bunch of them within the code.
There is an if/then statement for each potential bookmark. (around 60)

I think all of the documents we produce are 1 page with no graphics. There
are roughly between 10 and 20 bookmarks on each given document.

Without giving you all of the code, most of the stuff below won't make
sense, but it should show you in general terms how populating the word doc.
takes place.

The variable conditionreturn is just a data element from a table.

thanks for the help.


If APPWORD.ActiveDocument.Bookmarks.Exists("IHSC41BL2") = True Then
stopcode = "IHSC41BL2"
condcode = Letters.Recordset.Fields("IHSC41BL2")
Call CHECKCOND(document, stopcode, condcode)
If conditionreturn = "" Then
If Form1.objadorecordset2.Fields("OPER") = "GT" And
Form1.objadorecordset2.Fields("COND") = "a" Then
APPWORD.Selection.GoTo what:=wdGoToBookmark,
Name:="IHSC41BL2"


Call deletebookmark(stopcode)
End If
Else
Label44.Caption = conditionreturn
Label44.LinkTopic = "WinWord|" & Trim(docname)
Label44.LinkItem = "IHSC41BL2"
Label44.LinkMode = 2
Label44.LinkPoke
Label44.LinkMode = 0
End If
End If
 
P

Peter Hewett

Hi Mark Kurten

The code a code aint crash hot for sure! There is no use of intermediate variables or
With blocks. The statement:
If APPWORD.ActiveDocument.Bookmarks.Exists("IHSC41BL2") = True Then

I'd create and use and intermediate Bookmarks collection object and use that in your code.

The example only deletes the bookmark, it does not seem to update a bookmark with text
from your application. I presume that happens elsewhere. It's also more efficient to use
a Word Range object than a Selection object.

Basically you want to minimise the number COM references that are made. The simple rule
of thumb is the more dots in your statement the more expensive it is. So you could rewrite
the above line of code as:
Dim bmsAll as Word.Bookmarks

' Do this just the once for the entire document
Set bmsAll = APPWORD.ActiveDocument.Bookmarks

' From hereon we use to the intermediate object
If bmsAll.Exists("IHSC41BL2") = True Then

This is only a trivial example but is "best practice" for working with COM.

Use the following code for updating a bookmark, it also demonstrates the use of Word Range
object:

Public Sub UpdateBMText(ByVal strBMName As String, _
ByVal strBMValue As String)
Dim rngBM As Word.Range

With APPWORD.ActiveDocument.Bookmarks
Set rngBM = .Item(strBMName).Range
rngBM.Text = strBMValue
.Add strBMName, rngBM
End With
End Sub

You can call it like this:
UpdateBMText "bookmark name", "bookmark value"

HTH + Cheers - Peter
 
M

Mark Kurten

thanks alot..this is exactly what i needed..i found yesterday that the
syntax

Set rngrange = ActiveDocument.Bookmarks("ihsc01name").Range
rngrange.Delete
rngrange.InsertBefore "TestHeader"

appears to do the same thing...

thanks again...
 
M

Mark Kurten

one other thought..

is this method of merging data better or worse than using the mailmerge
object?
I'm not sure if these are similar or completely different.

thanks.
 

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