Run time error 462 Access VBA using Excel

倾然丶 夕夏残阳落幕 提交于 2020-08-05 19:16:21

问题


I occasionally get a run time error when trying to open/manipulate Excel files using Access VBA. The error is

"Run-Time error '462': The remote server machine does not exist or is unavailable

What is frustrating is that the error occurs only for certain files and not others and in different instances. Here is my code, the error occurs at the workbooks.open(sPath) line:

    DoCmd.SetWarnings False

Dim oExcel As New Excel.Application
Dim oWB As Workbook
Dim oWS As Worksheet

Set oExcel = Excel.Application
Set oWB = oExcel.Workbooks.Open(sPath)
Set oWS = oWB.Sheets(1)

oExcel.Visible = False

    If fGetFileName(sPath) = "FILE_NAME1.xlsx" Then
        'oExcel.Visible = False
        oWS.Range("AW1").Value = "TEXT1"
        oWS.Range("AX1").Value = "TEXT2"
        oWS.Range("AY1").Value = "TEXT3"
    End If

oWB.Save
Debug.Print "Amended " & sPath

oWB.Close False
Set oWB = Nothing

oExcel.Quit
Set oExcel = Nothing

DoCmd.SetWarnings True

After a bit of research online, I've found this document gives a good overview of the error: https://anictteacher.files.wordpress.com/2011/11/vba-error-462-explained-and-resolved.pdf

Using the logic from that document, the error is that:

object has not been fully qualified by reference to the Office object in every case

However, I amended the row where the error occurs to specifically reference the Excel object (Set oWB = oExcel.Workbooks.Open(sPath)). Have tried declaring the dimensions as Objects and put reference to oExcel in every mention of a workbook/sheet. Any ideas? Does sPath need to better qualified?


回答1:


You declare oExcel as a new Excel.Application:

Dim oExcel As New Excel.Application

That means later in your code, Set oExcel = Excel.Application is not really useful ... because oExcel is already a reference to Excel.Application. Add in a MsgBox to demonstrate what oExcel is at that point ...

MsgBox "TypeName(oExcel): " & TypeName(oExcel)
'Set oExcel = Excel.Application

Consider a different approach for your Excel object variable. Test this stripped down procedure (the purpose is only to see whether it eliminates your current error):

Dim oExcel As Excel.Application
Dim oWB As Excel.Workbook

DoCmd.SetWarnings True

MsgBox "TypeName(oExcel): " & TypeName(oExcel)
Set oExcel = New Excel.Application
MsgBox "TypeName(oExcel): " & TypeName(oExcel)
Set oWB = oExcel.Workbooks.Open(sPath)
oWB.Close False
Set oWB = Nothing
oExcel.Quit
Set oExcel = Nothing



回答2:


Also, do use WorkSheet and close this:

Set oWS = oWB.WorkSheets(1)
' snip.
oWB.Save
Set oWs = Nothing    
oWB.Close False
Set oWB = Nothing


来源:https://stackoverflow.com/questions/30649425/run-time-error-462-access-vba-using-excel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!