Dennis
Dennis

Reputation: 21

Error 426: The remote server machine does not exist or is unavailable when running Code a second time

I've looked through other solutions and none of the other posts seems to be identical to mine, trying to replicate the solutions I could, did not solve my problem

I'm trying to create a Master Word document using Word Documents as Templates using VBA in MS Access.

The code runs fine the first time, but then every subsequent run after, I receive the following error:

Error No:462 Error Desc: The remote server machine does not exist or is unavailable

I can't for the life of me figure out why it's erroring on subsequent runs.

It will run perfectly the first time, then error out on the line: Selection.WholeStory

When the error starts happening, I confirm in Taskmanager there are no word instances running before I try running it again.

Restarting Access seems to fix the problem as it will run successfully the first time after a restart.

The folder I'm creating the files in are not Onedrive synced

When initializing the word object I've also tried:

    Set gobjWord = GetObject(, "Word.Application")
    If Err.Number <> 0 Then
        Err.Clear
        Set gobjWord = **CreateObject("Word.Application")**
        gblnWordCreated = True
        gobjWord.Activate
        gobjWord.Visible = True
        
    End If

I've tried both late and early binding Dim objWord As Object

Here is the full function:

Public Function f_Test(lID As Long, strSQL As String)
  
    Dim objWord As Word.Application
    Dim blnWordCreated As Boolean
    
    Dim NewDoc As Word.Document
    Dim TemplateDoc As Word.Document

    Dim strTemplatePath As String
    
    Dim strNewFilePath As String
    Dim strNewFileName As String
    
    Dim rs As DAO.Recordset

On Error Resume Next
    
    blnWordCreated = False
    
    Set objWord = GetObject(, "Word.Application")
    
    If Err.Number <> 0 Then
    
        Err.Clear
        Set objWord = New Word.Application
        blnWordCreated = True
        objWord.Activate
        objWord.Visible = True
        
    End If

 On Error GoTo Error_Handler
         
    Set rs = CurrentDb.OpenRecordset(strSQL)
    
    If rs.RecordCount > 0 Then
    
        With rs
        
            strNewFilePath = "c:\Test\" & lID & "\"
            strNewFileName = strNewFilePath & "Empty " & f_GetDateForUseInFileName(Now()) & ".docx"
            
            .MoveFirst

            Set NewDoc = objWord.Documents.Add  'Create a new document
            NewDoc.SaveAs2 strNewFileName
            
            Do Until .EOF
                
                strTemplatePath = !FileName
                Set TemplateDoc = objWord.Documents.Open(FileName:=strTemplatePath)
                
                ' Copy Entire Document
                TemplateDoc.Activate
                Selection.WholeStory ' THIS IS WHERE IT ERRORS OUT ON SUBSEQUENT RUNS
                Selection.Copy
                
                ' Activate New Document and Paste
                NewDoc.Activate
                Selection.PasteAndFormat (wdUseDestinationStylesRecovery)
                Selection.InsertBreak Type:=wdPageBreak

                ' Close Template without saving
                TemplateDoc.Close SaveChanges:=wdDoNotSaveChanges
                .MoveNext
            Loop

        End With
    End If

Exit_Routine:

    ' cleanup
    rs.Close
    Set rs = Nothing
    
    NewDoc.Close SaveChanges:=wdSaveChanges

    Set TemplateDoc = Nothing
    Set NewDoc = Nothing
    
    If blnWordCreated Then
        objWord.Quit
        Set objWord = Nothing
    End If
    
    Exit Function

Error_Handler:
    
    Debug.Print Err.Number & " " & Err.Description
    Resume Exit_Routine

End Function

Upvotes: 1

Views: 52

Answers (2)

Dennis
Dennis

Reputation: 21

Use wordobject.Selection as Tim Williams suggested.

Upvotes: 1

BlueBlur22
BlueBlur22

Reputation: 46

You have a few options.

You have a reference to both documents, so use it instead of Activate

Do Until .EOF
    strTemplatePath = !FileName
    Set TemplateDoc = objWord.Documents.Open(FileName:=strTemplatePath)
                
    ' Copy Entire Document
    TemplateDoc.ActiveWindow.Selection.WholeStory
    TemplateDoc.ActiveWindow.Selection.Copy
                
    ' Paste into New Document
    NewDoc.ActiveWindow.Selection.PasteAndFormat (wdUseDestinationStylesRecovery)
    NewDoc.ActiveWindow.Selection.InsertBreak Type:=wdPageBreak

    ' Close Template without saving
    TemplateDoc.Close SaveChanges:=wdDoNotSaveChanges
    .MoveNext
Loop

Avoid using the Selection object and instead use a Range. Selection is slower and less flexible as you can only have one but you can have any number of Range instances. Stay out of the user's clipboard and pump the data directly with a Range or FileInsert.

If you just want to insert one Word document into another, use this for reference:

Dim InsertedDocument As Word.Range

Do Until .EOF
    If InsertedDocument Is Nothing Then 
        Set InsertedDocument = NewDoc.Range.Duplicate ' If the InsertedDocument is not defined, set it to the whole NewDoc
        InsertedDocument.WholeStory
    End If

    InsertedDocument.InsertBefore " " ' Adds an extra space character to the end which makes sure the end of the range encapsulates the inserted document (When you use InsertedDocument.InsertFile, the range ends up collapsed at the start of the inserted text rather than at the end)
    InsertedDocument.InertFile !FileName, ConfirmConversions:=False, Link:=False, Attachment:=False
    InsertedDocument.Characters.Last.Delete ' Removed the extra space character added to the end
    InsertedDocument.Characters.Last.InsertBreak Type:=wdPageBreak
    InsertedDocument.Collapse wdCollapseEnd ' Collapse the new insertion point to the end of the last inserted document
Loop

Upvotes: 1

Related Questions