Access control of Word through VBA (error: remote server machine does not exist)

directormac

Occasional Presence
Local time
Today, 17:25
Joined
Oct 24, 2001
Messages
259
Hey Gang!

I've searched the archives but haven't found this one yet, and the MS KB was entirely unhelpful. I've created a tool to simplify and automate a lot of advance tasks for a touring stage production. In the interest of max flexibility for the end users, several of these tasks revolve around template MS Word documents (these folks have no interest in learning to edit reports--or, for that matter, Word bookmarks--they want to use their existing stock document files). No problem, I open up their stock docs, find and replace the agreed-upon markers with the correct values, save off the modified documents in the correct folder, then e-mail them or print them or whatever the occasion calls for.

BUT

I'm doing something wrong somewhere, because I get an error the SECOND time I do any kind of work in Word: "The remote server machine does not exist or is unavailable." Seems like I must be missing something in cleaning up after myself, because the first time I touch Word in a given session, everything's great. If I close my db (but not Access) and open again, it behaves nicely for one more call--then the same error again.

This has to be something simple, but I haven't found it yet. I found this thread which is tantalizingly close but centers around the use of ActiveDocument.Name which I don't use. Is the use of ANY ActiveDocument property or method suspect? Do I need to replace all such calls?

Relevant code below--any suggestions?

--Stumped Mac



Code:
Public Function SetupBooking() As String

[COLOR=Gray]<SNIP documentation, other declarations...>[/COLOR]
   
' for dealing with the new .doc file...
Dim strPath As String
Dim strFileName As String

' for our work in MS Word...
Dim WordApp As New Word.Application
Dim WordReplace As Word.Dialog
   
[COLOR=Gray]<SNIP steps one and two...>[/COLOR]

' ***** STEP THREE: Create a template press schedule for the booking...

[COLOR=Gray]<SNIP setting up path & file name of template, other init stuff...>[/COLOR]

' Intialize the MS Word Application object:
Set WordApp = CreateObject("Word.Application")

With WordApp

   [COLOR=Gray]<SNIP application visible or not based on Access user's prefs...>[/COLOR]

    ' Open the document.
    .Documents.Open (strPath & strFileName)
    
    ' Do a global search and replace for each of the "variables" inserted...

    With Selection.Find
    
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = "{{BookingCity}}"
        .Replacement.Text = GetCurrentVenueInfo("City")
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
        .Execute Replace:=wdReplaceAll

        [COLOR=Gray]<SNIP other replacements...>[/COLOR]
        [COLOR=Green][B]' Note: I'm going to create a table of standard markers
        ' and replacements and just loop through them, but there
        ' are other fish to fry first...[/B][/COLOR]
         
                                 
    End With
    
    [COLOR=Gray]<SNIP set up path & file name of new customized version to save...>[/COLOR]

    ' And call Word's save routine:
    .ActiveDocument.SaveAs strPath & strFileName

    ' We're done in Word, so close it up...
    ' Close the current document:
    .ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
    ' Quit the application:
    .Quit
    
' Since we're done with Word, we can close the WITH bracket:
End With

' Last step: release the object resources:
Set WordApp = Nothing
Set WordReplace = Nothing

[COLOR=Gray]<SNIP step four and the rest of this function, which doesn't hit Word again>[/COLOR]
 
I've never really used Word Automation, but it does seem that you're actually creating two instances of the Word Application by:
directormac said:
Dim WordApp As New Word.Application
Set WordApp = CreateObject("Word.Application")
Surely you can take the second statement out as it's already being accomplished by the first? Also, I seem to remember that you'll get an error if you use CreateObject while the application object being created is already open.
 
Dugantrain said:
Surely you can take the second statement out as it's already being accomplished by the first?

I'm always ready to be wrong, but my understanding is that the Dim statement is just letting Access VBA know that it needs to set aside resources for a new instance of a Word.Application object. The object is not created until the CreateObject() call, which returns an object pointer to the newly created instance, which now gets stored in the previously declared variable WordApp.

It's like when you Dim MyInt as Integer - the space is reserved, but there's nothing in it. Then you [Set] MyInt = 5 to actually set the value.

--Dim Mac (as New objUser)
 
directormac said:
The object is not created until the CreateObject() call, which returns an object pointer to the newly created instance, which now gets stored in the previously declared variable WordApp.


Mac,

Don't know for certain if this will solve your problem, but perhaps you can try something like this:

Code:
Dim WordApp As Word.Application        		'Declare variable
Set WordApp = New "Word.Application"		'Create instance of object

or this...

Code:
'Declare and create in one shot using the New keyword.
Dim WordApp As New Word.Application

or even this...

Code:
Dim WordApp As Word.Application			'Declare.
Set WordApp = CreateObject("Word.Application")	'Create.

Object variables act a little differently than traditional variables which can make it very confusing. Yet as it is now, as DuganTrain said, the posted code creates two instances of the WordApp object where it looks like just one will do.

Hoping for a Happy Mac...

Regards,
Tim
 

Users who are viewing this thread

Back
Top Bottom