Word - Error 462 - FYI [Resolved]
I've seen this mentioned in other threads, but didn't find a clear explanation, so thought I'd post this fyi.
VB Code:
With objWord
.Visible = True
.Documents.Add
.ActiveWindow.ActivePane.View.Type = wdPrintView
With .Selection.PageSetup
.TopMargin = objWord.InchesToPoints(1)
.BottomMargin = objWord.InchesToPoints(1)
.LeftMargin = objWord.InchesToPoints(1)
.RightMargin = objWord.InchesToPoints(1)
End With
With .Selection
[b] .Style = ActiveDocument.Styles(wdStyleHeading1)[/b]
.TypeText "Questions: " & dbname
.TypeParagraph
.TypeParagraph
.TypeText strTable
[b] Set rng = ActiveDocument.Range(start:=ActiveDocument.Paragraphs(3).Range.start, & _
End:=ActiveDocument.Paragraphs(i + 4).Range.End)[/b]
rng.ConvertToTable vbTab
.Tables(1).AutoFormat Format:=wdTableFormatClassic2, & _AutoFit:=True, Applyshading:=False
End With
End With
This causes error 462, unable to connect to remote server, anytime the code is run more than once (first time is ok). The cause is arcane & by design, apparently.
The solution is:
VB Code:
With objWord
.Visible = True
.Documents.Add
.ActiveWindow.ActivePane.View.Type = wdPrintView
With .Selection.PageSetup
.TopMargin = objWord.InchesToPoints(1)
.BottomMargin = objWord.InchesToPoints(1)
.LeftMargin = objWord.InchesToPoints(1)
.RightMargin = objWord.InchesToPoints(1)
End With
With .Selection
[b].Style = objWord.ActiveDocument.Styles(wdStyleHeading1)[/b]
.TypeText "Questions: " & dbname
.TypeParagraph
.TypeParagraph
.TypeText strTable
[b]Set rng = objWord.ActiveDocument.Range(start:=objWord.ActiveDocument.Paragraphs(3).Range.start, & _
End:=objWord.ActiveDocument.Paragraphs(i + 4).Range.End)[/b]
rng.ConvertToTable vbTab
.Tables(1).AutoFormat Format:=wdTableFormatClassic2, & _AutoFit:=True, Applyshading:=False
End With
End With
objWord is added to qualify ActiveDocument in the bolded lines in the second code snippet becuase of some hidden global reference created. And yes, I've read the pitfalls of using ActiveDocument, but 1. never programmed Word before, and 2. I can guarantee this would be the only Word document open at the time.
Hope this helps someone. It's definitely a strange error message given the cause.