引用运行时错误时找不到excel文件

我试图运行一个macros,但是当引用一个excel文件时,我已经设置了运行时错误,并突出显示哪个引用了我的path。 我几乎可以肯定,这个path是正确的,因为我去了excel文件的属性,并复制的位置

Set xlWB = xlApp.Workbooks.Open(strPath) 

 ' Get Excel set up enviro = CStr(Environ("USERPROFILE")) 'the path of the workbook Debug.Print strPath = enviro & "\Documents\multipliers.xls" On Error Resume Next Set xlApp = GetObject(, "Excel.Application") If Err <> 0 Then Application.StatusBar = "Please wait while Excel source is opened ... " Set xlApp = CreateObject("Excel.Application") bXStarted = True End If On Error GoTo 0 'Open the workbook to input the data Set xlWB = xlApp.Workbooks.Open(strPath) Set xlSheet = xlWB.Sheets("Test1") ' Process the message record 

位置

您的文件是.xlsx文件,而不是您的代码中的.xls文件。 更改以下行:

 strPath = enviro & "\Documents\multipliers.xls" 

至:

 strPath = enviro & "\Documents\multipliers.xlsx" 
  Private Sub doingstuff() Dim xlWB As Workbook On Error Resume Next Set xlApp = GetObject(, "Excel.Application") If Err <> 0 Then Application.StatusBar = "Please wait while Excel source is opened ... " Set xlApp = CreateObject("Excel.Application") bXStarted = True End If On Error GoTo 0 strPath = Environ("USERPROFILE") & "\Desktop" & "\" & "Book2" & ".xlsm" Set xlWB = Workbooks.Open(strPath) End Sub 

这对我来说工作得很好。