2016-04-14 18 views
1

Bir meslektaşım makinesinde bazı kodları yüklemeye çalıştığımda bazı kodlarla ilgili bir sorunum var. Makine ve Outlook sürümü tam olarak aynıdır ve aynı kitaplığa başvurur. Ancak, komut dosyasını makinede çalıştırmayı denediğimde, 'Set xlWB = xlApp.Workbooks.Open (strPath)' üzerinde bir hata 91 oluşturur.Farklı bir makineye yüklemeye çalışırken hata kodu 91

niyet belirtilen bir dizinde bulunan bir Excel elektronik tabloya seçilen bir e-posta iletisinin gerekli verileri ihraç etmektir.

Hatayı gidermek için ne denemeliyim diye bir ipucu var mı? Aşağıdaki kodun ilk yarısı.

Çok teşekkürler!

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

Olası yinelenen [VBA: Çalışma zamanı hatası '91'] (http://stackoverflow.com/questions/18927297/vba-run-time-error-91) – Marged

+0

Sabit kodlanmış dosya yolları diğer makinede erişilebilir mi? – Comintern

+0

İş arkadaşlarınızdaki 'H' sürücüsü, sizinkiyle aynı 'H' sürücüsünü kullanıyor mu? Bunun yerine UNC adını kullanın (ör. \\ MyServerName \ MyFolder \). Ayrıca 'GetObject' kontrol edin - Siddarth onun cevabında özetliyor. Düzenle - çizin, bunu 'Dir' ile kontrol edin. –

cevap

1

Büyük ihtimalle bu hatayı alıyorsunuz çünkü çalışan Excel örneği yok.

  1. GetObject bir tane bulamadığında yeni bir örnek oluşturmanız gerekir.
  2. Err <> 0 varsa, dosyanın varlığını kontrol ediyor musunuz? Excel örneği bulunamadığı zaman mı? Bu mantıklı değil. Önce dosyanın varlığını kontrol edin ve sonra Excel'i kontrol edin.

bu çalıştığınız şey bu mu? (denenmemiş)

kodunuzu değiştirin

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 

için
'~~> 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 
İlgili konular