问题
I am trying to run a macro from excel that will open up an existing word document, populate the fields from excel to word via content controls, and paste/format a table into the word document at a certain location. About 20% of the time, I'll get this Run Time Error 462 stating "The remote Server Machine Does Not Exist or is Unavailable". I will provide the code and the section where is usually gets stuck at as everything after works 100% of the time. Please help on this.
Public Sub Agreement()
With Sheets("Price List Table")
.ListObjects(1).Name = "Table1"
End With
Dim tbl As Excel.Range
Dim wrdApp
Dim wrdDoc
Dim WordTable As Word.Table
Dim c As Integer
Set tbl = ThisWorkbook.Worksheets(Sheet2.Name).ListObjects("Table1").Range
Set wrdApp = CreateObject("Word.Application")
wrdApp.Visible = False
Set wrdDoc = wrdApp.Documents.Open("C:\Users\Documents\Example")
With wrdDoc
tbl.Copy
wrdApp.Selection.Find.Text = "This is the section for the table to be pasted below it."
wrdApp.Selection.Find.Execute
wrdApp.Selection.MoveDown Unit:=wdLine, Count:=5, Extend:=wdMove
wrdApp.Selection.PasteExcelTable False, False, False
Dim objTable As Object
For Each objTable In ActiveDocument.Tables ' (This is where the error occurs)
objTable.AutoFitBehavior (wdAutoFitWindow)
objTable.AllowAutoFit = True
Next
wrdApp.Selection.GoTo wdGoToPage, wdGoToAbsolute, 1
wrdApp.Selection.GoTo What:=wdGoToTable, Which:=GoToNext
wrdApp.Selection.GoTo What:=wdGoToTable, Which:=GoToNext
wrdApp.Selection.GoTo What:=wdGoToTable, Which:=GoToNext
wrdApp.Selection.Tables(1).Select
With wrdApp.Selection.Borders(wdBorderTop)
.LineStyle = Options.DefaultBorderLineStyle
.LineWidth = Options.DefaultBorderLineWidth
.Color = Options.DefaultBorderColor
End With
With wrdApp.Selection.Borders(wdBorderLeft)
.LineStyle = Options.DefaultBorderLineStyle
.LineWidth = Options.DefaultBorderLineWidth
.Color = Options.DefaultBorderColor
End With
With wrdApp.Selection.Borders(wdBorderBottom)
.LineStyle = Options.DefaultBorderLineStyle
.LineWidth = Options.DefaultBorderLineWidth
.Color = Options.DefaultBorderColor
End With
With wrdApp.Selection.Borders(wdBorderRight)
.LineStyle = Options.DefaultBorderLineStyle
.LineWidth = Options.DefaultBorderLineWidth
.Color = Options.DefaultBorderColor
End With
With wrdApp.Selection.Borders(wdBorderHorizontal)
.LineStyle = Options.DefaultBorderLineStyle
.LineWidth = Options.DefaultBorderLineWidth
.Color = Options.DefaultBorderColor
End With
With wrdApp.Selection.Borders(wdBorderVertical)
.LineStyle = Options.DefaultBorderLineStyle
.LineWidth = Options.DefaultBorderLineWidth
.Color = Options.DefaultBorderColor
End With
wrdApp.Selection.Tables(1).Columns(3).Select
wrdApp.Selection.ParagraphFormat.Alignment = wdAlignParagraphRight
wrdApp.Selection.Tables(1).Columns(4).Select
wrdApp.Selection.ParagraphFormat.Alignment = wdAlignParagraphRight
来源:https://stackoverflow.com/questions/50940143/run-time-error-462