run time error 462 the remote server machine does not exist

cErasmus
cErasmus used Ask the Experts™
on
Hi All

When i run the code below for the first time i receive the error as per the title. When i run it a second time it run through correctly. I have read a bit on it and see that it could be late bindings or Implicit references but i don't really see if and where this would apply in the code below.
The code breaks on this line
 ActiveDocument.MailMerge.Fields.Add Range:=WdDoc.Bookmarks(cell.Offset(0, 2).Value).Range, Name:=txt & cell.Offset(0, 3).Value

Open in new window



Sub DoMerge()
Dim appWd As Word.Application
Dim WdDoc As Word.Document
Dim strBookFullName As String
Dim cell As Excel.Range
'Dim wdFind As Object
Dim txt As String
'Dim MyDoc As String

strBookFullName = ActiveWorkbook.FullName

Set appWd = CreateObject("Word.Application")
appWd.Visible = True

With appWd
    Set WdDoc = appWd.Documents.Open("C:\Users\Elmo\Documents\Aaron\2nd Project 2017\WorkingOn\VendorRebateClientDoc4March17_2.docx")
    WdDoc.Activate
    WdDoc.MailMerge.OpenDataSource Name:=(strBookFullName), _
    ReadOnly:=True, LinkToSource:=0, AddToRecentFiles:=False, _
    PasswordDocument:="", PasswordTemplate:="", WritePasswordDocument:="", _
    WritePasswordTemplate:="", Revert:=False, Format:=wdOpenFormatAuto, _
    Connection:="", SQLStatement:="SELECT * FROM `MergeRange`", SQLStatement1:=""

'Set wdFind = appWd.Selection.Find
    'finds text in word doc and replace with merge field
    For Each cell In Range("MergeText")
        
        'wdFind.Text = cell.Value
        
        
        'MyDoc = WdDoc.Range.Text
            txt = cell.Offset(0, 1).Value
            ActiveDocument.MailMerge.Fields.Add Range:=WdDoc.Bookmarks(cell.Offset(0, 2).Value).Range, Name:=txt & cell.Offset(0, 3).Value
    Next cell
    
    .ActiveDocument.MailMerge.Execute
    .ActiveDocument.SaveAs "C:\Users\Elmo\Documents\Aaron\From Client\Output\test2.docx"
    
End With

End Sub

Open in new window


Thanks in advance
Elmo
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Change these two lines:
Dim appWd As Word.Application
Dim WdDoc As Word.Document

Open in new window

To this:
Dim appWd As Object
Dim WdDoc As Object

Open in new window

Author

Commented:
Hi MarcoShadow

Now it works on the first run but gives the same error on the second run
Commented:
I've managed to fix it, by adding a . before the line where it breaks. This makes sure that i'm referring to the active word doc
 .ActiveDocument.MailMerge.Fields.Add Range:=WdDoc.Bookmarks(cell.Offset(0, 2).Value).Range, Name:=txt & cell.Offset(0, 3).Value

Open in new window

Thanks

Author

Commented:
I found the answer on my own. The answers provided did nothing to solve the problem

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial