2016-04-14 4 views
1

私は同僚のマシンにいくつかのコードをインストールしようとしたときにいくつかのコードに問題があります。 Outlookのマシンとバージョンはまったく同じで、同じライブラリを参照します。しかし、自分のマシンでスクリプトを実行しようとすると、Set xlWB = xlApp.Workbooks.Open(strPath)にエラー91がスローされます。エラーコード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

の可能性のある重複した[VBA:?実行時エラー '91'](http://stackoverflow.com/questions/18927297/vba-run-time-error-91) – Marged

+0

ハードコーディングされたファイルパスは他のマシンからアクセスできますか? – Comintern

+0

あなたの同僚のマシンの 'H'ドライブはあなたと同じ 'H'ドライブですか?代わりにUNC名を使用してください(例:\\ MyServerName \ MyFolder \)。 'GetObject'もチェックしてください - Siddarthは彼の答えでそれを説明しています。編集 - あなたが 'Dir'と一緒に存在することを確認します。 –

答えて

1

実行中のExcelのインスタンスが存在しないため、おそらくこのエラーが発生しています。

  1. GetObjectが見つからない場合は、新しいインスタンスを作成する必要があります。
  2. Err <> 0の場合、ファイルの存在を確認していますか? Excelインスタンスが見つからない場合それは意味をなさない。最初にファイルが存在するかどうかをチェックして、Excelを確認します。

これはあなたの試みですか? (未テスト

はあなたのコードを変更し

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 
関連する問題