尝试在不同的计算机上安装时出现错误代码91

时间:2016-04-14 03:23:13

标签: vba excel-vba outlook-vba excel

当我尝试在同事机器上安装某些代码时,我遇到了一些代码问题。 Outlook的机器和版本完全相同,并引用相同的库。但是,当我尝试在她的机器上运行脚本时,它会在'Set xlWB = xlApp.Workbooks.Open(strPath)'上抛出错误91.

目的是将所选电子邮件中的必要数据导出到位于指定目录中的Excel电子表格中。

有关我应该尝试什么以消除错误的任何线索?下面的代码的前半部分。

非常感谢!

Option Explicit
 Sub ServiceRequestTool()
 Dim xlApp As Object
 Dim xlWB As Object
 Dim xlSheet As Object
 Dim rCount As Long
 Dim bXStarted As Boolean
 Dim enviro As String
 Dim strPath As String
 Dim currentExplorer As Explorer
 Dim Selection As Selection
 Dim olItem As Outlook.MailItem
 Dim obj As Object
 Dim strColA, strColB, strColC As String

     strPath = "H:\My Documents\General Docs\Govtnz-Service-Request.xlsm"

     On Error Resume Next
     Set xlApp = GetObject(, "Excel.Application")
     If Err <> 0 Then
        If Dir$("H:\My Documents\General Docs\Govtnz-Service-Request.xlsm") = "" Then
            MsgBox "Contact the spreadsheet administrator for assistance.", vbOKOnly + vbCritical, "File not found!"
            Exit Sub
        End If
     End If
     On Error GoTo 0
     Set xlWB = xlApp.Workbooks.Open(strPath)
     Set xlSheet = xlWB.Sheets("requestAssignment")

    On Error Resume Next

rCount = xlSheet.Range("C" & xlSheet.Rows.Count).End(-4162).Row + 1

Set currentExplorer = Application.ActiveExplorer
Set Selection = currentExplorer.Selection
  For Each obj In Selection

    Set olItem = obj

    strColA = olItem.SenderName
    strColB = olItem.SenderEmailAddress
    strColC = olItem.ReceivedTime


  xlSheet.Range("B" & rCount) = strColC
  xlSheet.Range("C" & rCount) = strColA
  xlSheet.Range("D" & rCount) = strColB

  rCount = rCount + 1

 Next

     xlWB.Close 1
     If bXStarted Then
         xlApp.Quit
     End If

     Set olItem = Nothing
     Set obj = Nothing
     Set currentExplorer = Nothing
     Set xlApp = Nothing
     Set xlWB = Nothing
     Set xlSheet = Nothing
 End Sub

1 个答案:

答案 0 :(得分:1)

您很可能会收到该错误,因为没有正在运行的Excel实例。

  1. GetObject找不到时,您需要创建一个新实例。
  2. 如果Err <> 0,您正在检查文件是否存在?即没有找到Excel实例?这没有意义。首先检查文件是否存在,然后检查Excel。
  3. 这是你在尝试什么? (的未测试

    更改您的代码

     On Error Resume Next
     Set xlApp = GetObject(, "Excel.Application")
     If Err <> 0 Then
        If Dir$("H:\My Documents\General Docs\Govtnz-Service-Request.xlsm") = "" Then
            MsgBox "Contact the spreadsheet administrator for assistance.", _
            vbOKOnly + vbCritical, "File not found!"
            Exit Sub
        End If
     End If
     On Error GoTo 0
    

    '~~> Move ths out of that IF/EndIf
    If Dir$("H:\My Documents\General Docs\Govtnz-Service-Request.xlsm") = "" Then
        MsgBox "Contact the spreadsheet administrator for assistance.", _
        vbOKOnly + vbCritical, "File not found!"
        Exit Sub
    End If
    
    On Error Resume Next
    Set xlApp = GetObject(, "Excel.Application")
    If Err <> 0 Then
       Set xlApp = CreateObject("Excel.Application") '<~~ Add this line
    End If
    On Error GoTo 0
    
    If xlApp Is Nothing Then
        MsgBox "Excel is not installed"
        Exit Sub
    End If