PDA

Click to See Complete Forum and Search --> : Word - Error 462 - FYI [Resolved]


salvelinus
Apr 27th, 2006, 11:53 AM
I've seen this mentioned in other threads, but didn't find a clear explanation, so thought I'd post this fyi.

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
.Style = ActiveDocument.Styles(wdStyleHeading1)
.TypeText "Questions: " & dbname
.TypeParagraph
.TypeParagraph
.TypeText strTable
Set rng = ActiveDocument.Range(start:=ActiveDocument.Paragraphs(3).Range.start, & _
End:=ActiveDocument.Paragraphs(i + 4).Range.End)
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 (http://support.microsoft.com/kb/189618/en-us/) is arcane & by design, apparently.
The solution is:

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
.Style = objWord.ActiveDocument.Styles(wdStyleHeading1)
.TypeText "Questions: " & dbname
.TypeParagraph
.TypeParagraph
.TypeText strTable
Set rng = objWord.ActiveDocument.Range(start:=objWord.ActiveDocument.Paragraphs(3).Range.start, & _
End:=objWord.ActiveDocument.Paragraphs(i + 4).Range.End)
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.