Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error 462: The remote server machine does not exist when working with Word via Excel VBA

Tags:

excel

vba

I open a Word file programmatically in Excel VBA and add/edit contents using bookmarks.

I find that on alternate runs, I get

Error 462: The remote server does not exist

I researched and understood this has something to do with 'Unqualified references'.

I don't understand how to correct the code to qualified references.

        Set exR = ActiveSheet.Range(TestIdCol & CStr(DataRowNum) & ":" & TestIdCol & CStr(RowEnd))

           ExistingEvidenceDoc = UseFileDialogOpen("Word Documents", "*.doc;*.docx")

           Set objWord = CreateObject("Word.Application")

           If ExistingEvidenceDoc <> "" Then
                Set objDoc = objWord.Documents.Open(ExistingEvidenceDoc)
           Else
                Exit Sub
           End If

           objWord.Visible = True
           Application.Wait Now() + TimeSerial(0, 0, 5)


           Set objSelection = objWord.Selection

           getExistingEvidences = ExistingTestEvidences(objDoc)
           o = DataRowNum
            For Each cell In exR
                If cell.Value <> "" And Not IsInArray(cell.Value, getExistingEvidences) Then
                    objSelection.Style = ActiveDocument.Styles("Heading 1")
                    objSelection.TypeText text:="Heading " + cell.Value
                    objSelection.TypeParagraph
                    objSelection.MoveLeft
                    objSelection.HomeKey Unit:=wdLine
                    objSelection.EndKey Unit:=wdLine, Extend:=wdExtend
                    objDoc.Bookmarks.Add Name:="BMrk" + CStr(o), Range:=objSelection
                    objSelection.Copy
                    ActiveSheet.Range("Q" + CStr(o)).Select
                           ActiveSheet.PasteSpecial Format:="Hyperlink", Link:=False, DisplayAsIcon _
                                   :=False
                    objSelection.MoveRight

                    'objSelection.Style = ActiveDocument.Styles("Paragraph")
                    objSelection.TypeText text:=Range(DescriptionCol + CStr(cell.Row)).Value
                    objSelection.TypeParagraph


                ElseIf IsInArray(cell.Value, getExistingEvidences) = False Then
                    objSelection.EndKey
                    objSelection.Style = ActiveDocument.Styles("Heading 1")
                    objSelection.TypeText text:="Heading " + cell.Value
                    objSelection.TypeParagraph
                    objSelection.MoveLeft
                    objSelection.HomeKey Unit:=wdLine
                    objSelection.EndKey Unit:=wdLine, Extend:=wdExtend
                    objDoc.Bookmarks.Add Name:="BMrk" + CStr(o), Range:=objSelection
                    objSelection.Copy
                    ActiveSheet.Range("Q" + CStr(o)).Select
                           ActiveSheet.PasteSpecial Format:="Hyperlink", Link:=False, DisplayAsIcon _
                                   :=False
                    objSelection.MoveRight

                    'objSelection.Style = ActiveDocument.Styles("Paragraph")
                    objSelection.TypeText text:=Range(DescriptionCol + CStr(cell.Row)).Value
                    objSelection.TypeParagraph
                End If

                o = o + 1

            Next cell


        MyErrorHandler:
                MsgBox "SeeHeadingPageNumber" & vbCrLf & vbCrLf & "Err = " & Err.Number & vbCrLf & "Description: " & Err.Description

Additionaly, whatever exR range I define, it completes execution for the entire range but at the end MyErrorHandler is invoked. Is there a reason for it?

like image 432
Sunny D'Souza Avatar asked Sep 25 '14 04:09

Sunny D'Souza


People also ask

How to fix runtime error 462 in VBA?

To resolve this problem, modify the code so each call to an Excel object, method, or property is qualified with the appropriate object variable. The author of the post was getting the error because he was not using the Access object to open and close the database.

How to fix error code 462?

Microsoft Corporation can then fix these errors in the source code and prepare an update for download. If there is notification for a Windows Operating System update, it can be a solution for fixing issues like error 462 and additional issues discovered.

What is error 462?

Run-time error '462': The remote server machine does not exist or is unavailable.


3 Answers

You have two unqualified references to Word objects:

objSelection.Style = ActiveDocument.Styles("Heading 1")

which appears twice, needs to be:

objSelection.Style = objWord.ActiveDocument.Styles("Heading 1")

Otherwise you're creating an implicit reference to Word that you can't destroy in your code.

like image 189
Rory Avatar answered Oct 17 '22 11:10

Rory


You should first ensure there are no oprhan winword.exe in task manager. Kill then or log out/in to get rid of them.

Then you should add something like this code to the end to 'explcitly' close word:

(I'm not sure of the exact syntax, hopefully you can work it out)

IF Not(objWord Is Nothing) Then

    objWord.Close(False)
    Set objWord = Nothing

End If

You should add something similar to your error handler.

What often happens is during development and debugging, sometimes word doesn't get closed properly and 'orphan' processes hang around even though they are not visible.

You may also wish to use

Set objWord = New Word.Application

instead of

Set objWord = CreateObject("Word.Application")

as that gives you autocomplete etc.

But there area advantages to each way.

like image 38
Nick.McDermaid Avatar answered Oct 17 '22 11:10

Nick.McDermaid


[SOLVED] Err 462 – "The remote server machine does not exist or is unavailable"

ErrResume:
    On Error GoTo ErrPaste
        Set objDoc = objWord.Documents.Open(ExistingEvidenceDoc)
    On Error GoTo 0

ErrPaste:
  'The remote server machine does not exist or is unavailable
    If Err.Number = 462 Then
        Set wdApp = CreateObject("Word.Application")
        Resume ErrResume
    End If
like image 35
Gajendra Santosh Avatar answered Oct 17 '22 12:10

Gajendra Santosh