esoteric Word VBA run order issue - not trying to scare off anyone

T

traderjoes

I created a form and some VBA code behind it in a Word doc file. Under the
ThisDocument (Microsoft Word Objects) for Document_Open() I have the
following code:

Private Sub Document_Open()
form1.Show
End Sub

That kicks off the form but I get a Run-time error '5852': Requested object
is not available. I did a line by line execution run and found that the error
occurs in this line

strPatMasterID =
ThisDocument.MailMerge.DataSource.DataFields("PatentMasterID").Value

The problem is order in which the code is executed. The mailmerge (based on
msquery) is kicked off after the form opens (or in this case because of the
error, when I click "end" to stop the code)... so obiviously it cannot
retrieve the mailmerge.datasource.datafields if the mailmerge doesn't run
first.

So, does anyone how now I can manipulate the run order of the VBA code or
explicitly force the msquery to run first before I retrieve the data? Would
setting a timer work?

thanks in advance for your help
 
C

Cindy Meister

The standard way to get around a problem like this is to put some code at the
beginning of Document_Open that loops until a certain criterium has been
fulfilled. For example

Dim bReady as Boolean
Dim counter as Long
bReady = false
counter = 0
On Error Resume Next
Do
counter = counter +1
ActiveDocument.MailMerge.DataSource.DataFields("PatentMasterID").Value
<> ""
Loop until bReady = true OR counter = 5000
On Error GoTo 0

form1.Show
 
T

traderjoes

Thanks Cindy for response (Btw, I've been to your word FAQ webpage and quite
thankful for the advice found there.)

I tried the code and got the bReady and Counter variables to work however,
the msquery/mailmerge still won't kick off.... It seems like it doesn't kick
off until the VBA is completely finish executing or if I click "end" to exit
out.

Do you know what might be happening with the way and order in which VBA code
executes in word? Another work around that I've consider is actually not have
Document_Open() kick off anything but rather put a command button on the
actual doc and have the users manual click the button to retrieve the data.
The only problem with this is that when you print the doc, the button also
get printed and so far, I've been getting some other runtime error with the
command button.

Tony
 
C

Cindy Meister

Hi Tony

OK, maybe you need to explain in more detail in what order the code
executes. You say you have form.Show in DocumentOpen. That should display a
userform, and doesn't, in and of itself, have anything to do with mail merge.
At what point (in what form event) is the mail merge stuff executed?

And is the MSQuery bound to the document? Or does your code load it?

Another thing to consider/try would be an AutoOpen proc (in a normal module)
instead of Document_Open event. They don't execute at the same time, so
AutoOpen might be better - you'd have to try.
 

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