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

时间:2016-05-12 12:57:34

标签: excel vba excel-vba outlook-vba

我试图运行一个宏,但在引用我已经设置的excel文件时,我遇到了运行时错误,并突出显示了我的路径。我几乎可以肯定这条路径是正确的,因为我去了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

Location

2 个答案:

答案 0 :(得分:2)

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

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

为:

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

答案 1 :(得分:0)

 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

这对我来说很好。