Error 462 - Remote server machine does not exist or is unavailable

D

Dale Fye

I'm using Access to build a Word document from scratch.

Sometimes, the code runs smoothly, but others, I get this error message. My
code (abbreviated) looks like below. When I insert a breakpoint and walk it
through, the error appears to be occuring is in the BoldUnderText subroutine.
When I get to that routine, and mouse over the first "Selection" line it
displays the message.

Would appreciate any help you can provide.

Public Sub Word_Doc()

Dim strSQL As String
Dim rs As dao.Recordset

Dim appWord As Word.Application
Dim wdDoc As Word.Document
Dim bWordWasOpen As Boolean

On Error GoTo ProcError

bWordWasOpen = True
Set appWord = GetObject(, "Word.Application")
appWord.Visible = True
Set wdDoc = appWord.Documents.Add(, , , True)

strSQL = "SELECT * FROM myQuery"
Set rs = CurrentDb.OpenRecordset(strSQL, , dbFailOnError)

While Not rs.EOF

With wdDoc
Call BoldUnderText(wdDoc, "Title:")
Selection.TypeText Text:=" " & rs("Field1")
Selection.TypeParagraph

Call BoldUnderText(wdDoc, "Statement:")
Selection.TypeText Text:=" " & rs("Field2")
Selection.TypeParagraph

rs.MoveNext

If Not rs.EOF Then Selection.InsertBreak Type:=wdPageBreak
End With

Wend

ProcExit:
If Not rs Is Nothing Then
rs.Close
Set rs = Nothing
End If
Set wdDoc = Nothing
Set appWord = Nothing
Exit Sub

ProcError:
If Err.Number = 429 Then
bWordWasOpen = False
Set appWord = CreateObject("Word.Application")
Resume Next
Else
Debug.Print Err.Number, Err.Description
MsgBox Err.Number & vbCrLf & Err.Description
Resume ProcExit
End If

End Sub

Private Sub BoldUnderText(ByRef wdDoc As Word.Document, TextToBoldUnder As
Variant)

With wdDoc
Selection.Font.Bold = wdToggle
Selection.Font.UnderlineColor = wdColorAutomatic
Selection.Font.Underline = wdUnderlineSingle

Selection.TypeText Text:=TextToBoldUnder

Selection.Font.Bold = wdToggle
Selection.Font.UnderlineColor = wdColorAutomatic
Selection.Font.Underline = wdUnderlineNone
End With

End Sub
 
T

Tony Jollans

Your problem is probably caused by the use of Selection, instead of
..Selection (i.e. it should be preceded by a period to indicate that it is a
property of the your With variable - wdDoc).
 
D

Dale Fye

Nope, that is not it.

I replaced all of the

With wdDoc

lines with:

With Selection

It still runs fine the first time through during a given Access session, but
subsequent runs of the subroutine during the same Access session result in
the 462 error. The applicable part of the code now looks like:

Set wdDoc = appWord.Documents.Add(, , , True)
wdDoc.Select

'Set the margins
With Selection
.PageSetup.TopMargin = InchesToPoints(0.5)
.PageSetup.BottomMargin = InchesToPoints(0.5)
End With

and the error is occuring on the With Selection line.


----
HTH
Dale



Tony Jollans said:
Your problem is probably caused by the use of Selection, instead of
..Selection (i.e. it should be preceded by a period to indicate that it is a
property of the your With variable - wdDoc).
 
T

Tony Jollans

Your reference to Selection is still wrong. Without qualification, it leads
to implicit instantiation of a Word object

You must use "With wdDoc.Selection" or some construct that gives the same
result.

--
Enjoy,
Tony

www.WordArticles.com

Dale Fye said:
Nope, that is not it.

I replaced all of the

With wdDoc

lines with:

With Selection

It still runs fine the first time through during a given Access session,
but
subsequent runs of the subroutine during the same Access session result in
the 462 error. The applicable part of the code now looks like:

Set wdDoc = appWord.Documents.Add(, , , True)
wdDoc.Select

'Set the margins
With Selection
.PageSetup.TopMargin = InchesToPoints(0.5)
.PageSetup.BottomMargin = InchesToPoints(0.5)
End With

and the error is occuring on the With Selection line.
 
D

Dale Fye

Then why does it work the first time through the subroutine?

When I type "wdDoc." intellisense does not identify Selection as a method or
property of that object (which is dimensioned as Word.Doc). And when I try
to use:

With wdDoc.Selection

I get err 438:Object doesn't support this property or method.

----
Dale



Dale Fye said:
Nope, that is not it.

I replaced all of the

With wdDoc

lines with:

With Selection

It still runs fine the first time through during a given Access session, but
subsequent runs of the subroutine during the same Access session result in
the 462 error. The applicable part of the code now looks like:

Set wdDoc = appWord.Documents.Add(, , , True)
wdDoc.Select

'Set the margins
With Selection
.PageSetup.TopMargin = InchesToPoints(0.5)
.PageSetup.BottomMargin = InchesToPoints(0.5)
End With

and the error is occuring on the With Selection line.
 
T

Tony Jollans

Sorry. My original point was that using Selection was wrong - which, indeed,
it is - and I missed the fact that you were using the Document rather than
the Application as the apparent qualifier.

Every Window in Word has a Selection, with the default Selection being the
one in the ActiveWindow. In your case you (explicitly) want
wdDoc.ActiveWindow.Selection, which will be the same as appWord.Selection
or, as you don't have a reference to the application in the procedure,
wdDoc.Parent.Selection or wdDoc.Application.Selection.

The reason it works first time is slightly mysterious, but there was a
change (in Office 2003, I think) which meant that VBA somehow decided that
you meant the Word Selection (as Selection is meaningless in the Access
context) and so instantiated a Word object to do what it thought you wanted.
It does not, however, tidy up properly so this behaviour, whether
intentional or not, is of little use as it leads to the rather obscure error
you are seeing.
 
D

Dale Fye

Tony,

That has not resolved the issue either. I've tried changing all references
to Selection in my code to read:

wdDoc.ActiveWindow.Selection

and then to

wdDoc.Application.Selection

and am still getting the 462 error on subsequent runs of the code. Could it
have anything to do with the way I am exiting my code? Because I want the
user to be able to view (and potentially edit) the Word doc when the
application finishes creating it, I am not "closing" the word doc or the
application. I am just setting the variables to nothing.

ProcExit:
If Not rs Is Nothing Then
rs.Close
Set rs = Nothing
End If
If Not wdDoc Is Nothing Then Set wdDoc = Nothing
If Not appWord Is Nothing Then Set appWord = Nothing
Exit Sub
 
D

Dale Fye

Tony,

Just realized that this works properly with subsequent runs if I don't close
Word. But if I close Word between runs of the code, it generates the error.
 
T

Tony Jollans

Hi Dale,

That means something (quite possibly VBA behind the scenes) is keeping a
reference that really shouldn't exist. Are you quite sure you don't have any
unqualified references to any intended Word objects anywhere? Or any module
level variables that aren't cleared (as you say the posted code is
abbreviated, I'm guessing at possible causes)?


Setting your appWord and wdDoc references to Nothing without closing Word
should be fine if you want Word to stay open.
 
D

Dale Fye

Tony,

Maybe you can spot something I could not. Attached is most of the code.
I've cut some lines (marked as '**** code removed) in the main subroutine,
but they are only repeats of the ones immediately preceeding them.

Public Sub AWFC_Word_Doc()

Dim strSQL As String
Dim rs As dao.Recordset

Dim appWord As Word.Application
Dim wdDoc As Word.Document
Dim bWordWasOpen As Boolean
Dim lngErr As Long

On Error GoTo ProcError

bWordWasOpen = True
Set appWord = GetObject(, "Word.Application")
appWord.Visible = True
Set wdDoc = appWord.Documents.Add(, , , True)

'Set the margins
With wdDoc.Application.Selection
' With wdDoc.ActiveWindow.Selection
.PageSetup.TopMargin = InchesToPoints(0.5)
.PageSetup.BottomMargin = InchesToPoints(0.5)
End With

'Set the paragraph line formatting
With wdDoc.Application.Selection.ParagraphFormat
' With wdDoc.ActiveWindow.Selection.ParagraphFormat
.SpaceBefore = 0
.SpaceBeforeAuto = False
.SpaceAfter = 0
.SpaceAfterAuto = False
.LineSpacingRule = wdLineSpaceSingle
End With

strSQL = "SELECT * FROM qry_rpt_Original_FY_AWFCs"
Set rs = CurrentDb.OpenRecordset(strSQL, , dbFailOnError)

While Not rs.EOF
DoEvents

With wdDoc.Application.Selection
' With wdDoc.ActiveWindow.Selection
Call BoldUnderText(wdDoc, "AWFC #:")
.TypeText Text:=" " & rs("LD_NUM")
.TypeParagraph
.TypeParagraph

Call BoldUnderText(wdDoc, "AWFC Title:")
.TypeText Text:=" " & rs("LD_Name")
.TypeParagraph
.TypeParagraph

'**** code removed

rs.MoveNext

'Insert a page break to start each AWFC on a new page
'Make sure that each AWFC starts on an odd page number
' (for duplex printing)
If rs.EOF Then
'dont add any more pagebreaks
ElseIf .Information(wdActiveEndPageNumber) Mod 2 = 1 Then
.InsertBreak Type:=wdPageBreak
.InsertBreak Type:=wdPageBreak
Else
.InsertBreak Type:=wdPageBreak
End If
End With

Wend

ProcExit:
If Not rs Is Nothing Then
rs.Close
Set rs = Nothing
End If
If Not wdDoc Is Nothing Then Set wdDoc = Nothing
If Not appWord Is Nothing Then Set appWord = Nothing

MsgBox "Done!"
Exit Sub

ProcError:
If Err.Number = 429 Then
bWordWasOpen = False
Set appWord = CreateObject("Word.Application")
Resume Next
Else
Debug.Print Err.Number, Err.Description
MsgBox Err.Number & vbCrLf & Err.Description
Resume ProcExit
End If

End Sub

Private Sub BoldUnderText(ByRef wdDoc As Word.Document, _
TextToBoldUnder As Variant)

With wdDoc.Application.Selection
' With wdDoc.ActiveWindow.Selection
.Font.Bold = wdToggle
.Font.UnderlineColor = wdColorAutomatic
.Font.Underline = wdUnderlineSingle

.TypeText Text:=TextToBoldUnder

.Font.Bold = wdToggle
.Font.UnderlineColor = wdColorAutomatic
.Font.Underline = wdUnderlineNone
End With

End Sub
 
T

Tony Jollans

Not the most obvious, perhaps, but InchesToPoints is a Word function. Try
using appWord.InchesToPoints instead.
 
D

Dale Fye

Bingo,

We have a winner! Now that that works, I've removed the Word reference and
gone to late binding. Still works.

Thanks for sticking with me on this Tony.
 
T

Tony Jollans

Glad you got there in the end. It can be difficult tracking these things
down sometimes - I've seen InchesToPoints cause this before so it wasn't too
hard for me to spot, but if you haven't seen it, it doesn't exactly jump out
at you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top