We help IT Professionals succeed at work.

Excel automation from Access: Err# 462 , The remote server machine does not exist

Dale Fye
Dale Fye asked
I'm exporting an Access query to Excel and attempting to format some of the rows and columns.  

Sometimes, this code works, other times, I get the "The remote server machine does not exist or is unavailable" error message on the line indicated.  When it doesn't work, the header row is already populated with the column names.

Code is basically:
Dim rs as dao.Recordset
Dim xl as object
dim wbk as object
dim sht as object
dim rs as object

set rs = currentdb.querydefs("myQueryName").openrecordset

set xl = createobject("Excel.Application")
set wbk = xl.workbooks.Add
set sht = wbk.sheets(1)

'Write the column headers
For intLoop = 1 to rs.fields.count
     sht.cells(1, intLoop).Value = rs.fields(intloop-1).Name

'Highlight the column headers and copy the header column
sht.Range(Selection, Selection.End(xlToRight)).Select     'this is the line that raises the error
set rng = Selection                                       'this is used later in the code
with rng.Interior
    .Pattern = xlSolid
    .PatternColorIndex = xlAutomatic
    .ThemeColor = xlThemeColorDark1
    .TintAndShade = -0.14996795556505
    .PatternTintAndShade = 0
End With

sht.Range("A2").CopyFromRecordset rs

if NOT (rs is nothing) then
    set rs = nothing
end if

'whole bunch of additional formatting functions here

if NOT (rng is Nothing) then set rng = nothing
if NOT (sht is nothing) then set sht = nothing
if NOT (wbk is nothing) then set wbk = nothing
if NOT (xl is nothing) then set xl = nothing

Open in new window

Watch Question

Distinguished Expert 2017

Never ever use the Selection object, it is user dependant, by nature chaotic, and it is slow as hell !!
As a developer, you don't want to use chaotic objects.
Plus: set rng = Selection
Selection alone is the same as ActiveWorkbook.ActiveSheet.Selection, wich guarantees do you have that the workbook you created a couple of instructions before is the active one ?
Answer is: None (the user can have one or more workbooks already open).

Replace your lines 19 to 21 by:
Set rng = sht.Range(sht.Range("B1"), sht.Range("B1").End(xlToRight))

Open in new window

and it should work.
Dale FyeOwner, Dev-Soln LLC
Most Valuable Expert 2014
Top Expert 2010


Thanks, Fabrice.  Somehow this question had fallen off my radar, although I did use your suggestions to resolve the issue.