Run Time Error 462

CPO_Rick

New Member
Joined
Jul 18, 2024
Messages
7
Office Version
  1. 2019
Platform
  1. Windows
I am programming a Word document using Excel VBA. Can someone please help me get rid of Run Time Error 462, (the remote server does not exist or is unavailable)? I know that this is a common problem that occurs the second time a procedure is run, and I haver read many posts about it, but I still cannot determine what I am doing wrong.

VBA Code:
Private Sub CreateOldRoster()
    Dim OldRoster As Word.Application
    Dim SaveName As String, LabelDate As String, yr As String, fullyr As String
    Dim WkSht As Worksheet
    Dim rng As Word.Range, s As Word.Section, hf As Word.HeaderFooter
    Dim hdr As Word.HeaderFooter
    Dim OldR As Word.Document
    Dim aTbl As Word.Table, aCell As Word.cell
    Dim WordOpen As Boolean
    
    WordOpen = False
    Set WkSht = ThisWorkbook.Worksheets("ROSTER")
    Set OldRoster = CreateObject("Word.Application")
    WordOpen = True
    If MsgBox("Select Yes to label the roster for this month (" & Format(Now, "mmmm") & _
        ") or No to label as next month (" & Format(CDate(Format(Now, "MMMM") & "/2 0") + 30, "mmmm") & _
        ")", vbYesNo) = vbNo Then
            LabelDate = Format(CDate(Format(Now, "MMMM") & "/2 0") + 30, "mmmm")
        Else
            LabelDate = Format(Now, "MMMM")
    End If
    
    If (Format(Now, "mmmm") <> LabelDate) And (LabelDate = "January") Then
            yr = Trim(Format(Now, "YY") + 1)
            fullyr = Trim(Format(Now, "YYYY") + 1)
        Else
            yr = Format(Now, "YY")
            fullyr = Format(Now, "YYYY")
    End If
         SaveName = "C:\OEN\ActiveByCityRoster" & LabelDate & yr & ".docx"
    '~~> Check if word file exists
    If Dir(SaveName) <> "" Then
        If MsgBox("File already exists for this month.  Delete it?", vbYesNo) = vbYes Then
            Kill SaveName
        Else
            MsgBox "Keeping old file and exiting."
            GoTo cleanup
        End If
    End If
    With OldRoster
        .Documents.Add "C:\OEN\ActiveCityRosterTemplate.dotx"
        .Visible = True
        .Activate
         With WkSht
             .Range("A1:J" & Rows.Count).Copy
         End With
        .Documents(1).Content.Font.Size = 10
        OldRoster.Selection.Paste
[B]' ********* BELOW IS THE OFFENDING LINE.  IT WORKS THE FIRST TIME.[/B]
        OldRoster.ActiveDocument.Tables(1).Rows.SetHeight RowHeight:=InchesToPoints(0.4), HeightRule:=wdRowHeightExactly
        Application.ScreenUpdating = False
        With OldRoster.ActiveDocument.Sections.First
            With .Headers(wdHeaderFooterPrimary).Range
                .Collapse wdCollapseEnd
                .Text = " " & vbLf & " " & LabelDate & " " & fullyr
                .Font.Name = "Arial"
            End With
        End With
        Application.ScreenUpdating = True
         OldRoster.ActiveDocument.Tables(1).Rows(1).HeadingFormat = True
         OldRoster.ActiveDocument.SaveAs2 SaveName
         OldRoster.ActiveDocument.Close
    End With
    If WordOpen Then OldRoster.Quit
cleanup:
    Set OldRoster = Nothing
    Set WkSht = Nothing
    Set OldR = Nothing
    Set aTbl = Nothing
End Sub
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Never mind. :) As soon as I finished posting this, I found another anser that told me to add this to the top of my code
Const wdReplaceAll As Long = 2
Apparently this removes other instances of Word apps.
 
Upvote 0
Never mind. :) As soon as I finished posting this, I found another anser that told me to add this to the top of my code
Const wdReplaceAll As Long = 2
Apparently this removes other instances of Word apps
Well, I THOUGHT it was fixed. It ran several times without incident but then it came back to haunt me. I gues I still need help.
 
Upvote 0
I am programming a Word document using Excel VBA. Can someone please help me get rid of Run Time Error 462, (the remote server does not exist or is unavailable)? I know that this is a common problem that occurs the second time a procedure is run, and I haver read many posts about it, but I still cannot determine what I am doing wrong.

VBA Code:
Private Sub CreateOldRoster()
    Dim OldRoster As Word.Application
    Dim SaveName As String, LabelDate As String, yr As String, fullyr As String
    Dim WkSht As Worksheet
    Dim rng As Word.Range, s As Word.Section, hf As Word.HeaderFooter
    Dim hdr As Word.HeaderFooter
    Dim OldR As Word.Document
    Dim aTbl As Word.Table, aCell As Word.cell
    Dim WordOpen As Boolean
   
    WordOpen = False
    Set WkSht = ThisWorkbook.Worksheets("ROSTER")
    Set OldRoster = CreateObject("Word.Application")
    WordOpen = True
    If MsgBox("Select Yes to label the roster for this month (" & Format(Now, "mmmm") & _
        ") or No to label as next month (" & Format(CDate(Format(Now, "MMMM") & "/2 0") + 30, "mmmm") & _
        ")", vbYesNo) = vbNo Then
            LabelDate = Format(CDate(Format(Now, "MMMM") & "/2 0") + 30, "mmmm")
        Else
            LabelDate = Format(Now, "MMMM")
    End If
   
    If (Format(Now, "mmmm") <> LabelDate) And (LabelDate = "January") Then
            yr = Trim(Format(Now, "YY") + 1)
            fullyr = Trim(Format(Now, "YYYY") + 1)
        Else
            yr = Format(Now, "YY")
            fullyr = Format(Now, "YYYY")
    End If
         SaveName = "C:\OEN\ActiveByCityRoster" & LabelDate & yr & ".docx"
    '~~> Check if word file exists
    If Dir(SaveName) <> "" Then
        If MsgBox("File already exists for this month.  Delete it?", vbYesNo) = vbYes Then
            Kill SaveName
        Else
            MsgBox "Keeping old file and exiting."
            GoTo cleanup
        End If
    End If
    With OldRoster
        .Documents.Add "C:\OEN\ActiveCityRosterTemplate.dotx"
        .Visible = True
        .Activate
         With WkSht
             .Range("A1:J" & Rows.Count).Copy
         End With
        .Documents(1).Content.Font.Size = 10
        OldRoster.Selection.Paste
[B]' ********* BELOW IS THE OFFENDING LINE.  IT WORKS THE FIRST TIME.[/B]
        OldRoster.ActiveDocument.Tables(1).Rows.SetHeight RowHeight:=InchesToPoints(0.4), HeightRule:=wdRowHeightExactly
        Application.ScreenUpdating = False
        With OldRoster.ActiveDocument.Sections.First
            With .Headers(wdHeaderFooterPrimary).Range
                .Collapse wdCollapseEnd
                .Text = " " & vbLf & " " & LabelDate & " " & fullyr
                .Font.Name = "Arial"
            End With
        End With
        Application.ScreenUpdating = True
         OldRoster.ActiveDocument.Tables(1).Rows(1).HeadingFormat = True
         OldRoster.ActiveDocument.SaveAs2 SaveName
         OldRoster.ActiveDocument.Close
    End With
    If WordOpen Then OldRoster.Quit
cleanup:
    Set OldRoster = Nothing
    Set WkSht = Nothing
    Set OldR = Nothing
    Set aTbl = Nothing
End Sub
Although this is not an urgent issue, I would dearly love to understand how to handle it properly. Please help if you can. Thanks.
 
Upvote 0

Forum statistics

Threads
1,223,884
Messages
6,175,174
Members
452,615
Latest member
bogeys2birdies

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top