Link to home
Start Free TrialLog in
Avatar of Elmo Erasmus
Elmo ErasmusFlag for Namibia

asked on

run time error 462 the remote server machine does not exist

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
Avatar of Joe Howard
Joe Howard
Flag of United States of America image

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

Avatar of Elmo Erasmus

ASKER

Hi MarcoShadow

Now it works on the first run but gives the same error on the second run
ASKER CERTIFIED SOLUTION
Avatar of Elmo Erasmus
Elmo Erasmus
Flag of Namibia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I found the answer on my own. The answers provided did nothing to solve the problem