2017-03-17 69 views
0

我在每封碰到我的outlook的電子郵件上運行腳本。該腳本需要打開指定的Excel文檔並保存發件人名稱&地址,主題和日期。我得到運行時錯誤1004:對象_global的方法行失敗,我的一些郵件收到,不是全部。我在錯誤發生的代碼中指定了這一行。這是一個引用問題嗎?VBA - Outlook到Excel:對象_global的方法行失敗

Public Sub CoupaQueries(MItem As Outlook.MailItem) 

Dim objOutlook As Outlook.Application 

PersonName = MItem.SenderName 
PersonAddress = MItem.SendUsingAccount 
PersonSubject = MItem.Subject 
PersonDate = MItem.ReceivedTime 

Dim objExcel As Excel.Application 
Dim wks As Excel.Worksheet 
Dim wkb As Excel.Workbook 

Set objExcel = New Excel.Application 

objExcel.Workbooks.Open ("C:\Users\a222012\Desktop\CoupaQueries.xlsx") 
objExcel.Visible = True 
Set wkb = objExcel.ActiveWorkbook 
Set wks = wkb.Sheets("Sheet1") 

'Error occurs on the next line 

wks.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Value = PersonName 
wks.Cells(Rows.Count, 2).End(xlUp).Offset(1, 0).Value = PersonAddress 
wks.Cells(Rows.Count, 3).End(xlUp).Offset(1, 0).Value = PersonSubject 
wks.Cells(Rows.Count, 4).End(xlUp).Offset(1, 0).Value = PersonDate 

objExcel.ActiveWorkbook.Save 
objExcel.Quit 

End Sub 
+1

嘗試使用'wks.Rows.Count',而不是僅僅'Rows.Count' – gizlmo

+1

參考您的工作簿當你打開它 - 設置wkb = objExcel.Workbooks.Open(「C:\ Users \ a222012 \ Desktop \ CoupaQueries.xlsx」)'。在打開它之後,它可能不是活動手冊的一小部分。正如@ gizlmo所說 - Outlook不理解「Rows.Count」是什麼。 –

+0

@gizlmo謝謝隊友,這似乎已經解決了這個問題。 – KoderM16

回答

0

簡化代碼讓錯誤顯示的地方發生,例如,

Dim R As Long 

R = Wks.Cells(Rows.Count, 1).End(xlUp) + 1 
Wks.Cells(R, 1).Value = PersonName 
Etc 

我懷疑你的公式設定R.這看起來更爲合理: -

R = Wks.Cells(Rows.Count, 1).End(xlUp).Row + 1 
2

由於您的客戶端應用程序是Outlook,因此您沒有隱式引用Excel應用程序對象模型,因此您必須明確地指定r eference每一個Excel對象(比如,Worksheet對象)來訪問其成員(比如,它的Rows屬性)

With object - End With語法可以進來幫助雙方避免這樣的錯誤,讓你在對象模型處理更多的把握(意識) ,即一旦你引用的任何對象,那麼你必須所有成員(屬性,方法,枚舉)一個簡單的點(.)以外,例如像:

Public Sub CoupaQueries(MItem As Outlook.MailItem) 
    Dim objExcel As Excel.Application 
    Dim objOutlook As Outlook.Application '<--| not needed, since you' are in Outlook its object model is implicitly referenced 
    Dim PersonName As String, PersonAddress As String, PersonSubject As String, PersonDate As Date 

    With MItem '<--| reference passed MItem object 
     PersonName = .SenderName 
     PersonAddress = .SendUsingAccount 
     PersonSubject = .Subject 
     PersonDate = .ReceivedTime 
    End With   

    Set objExcel = New Excel.Application '<--| get a new instance of Excel 
    objExcel.Visible = True '<--| not necessary 
    With objExcel.Workbooks.Open("C:\Users\a222012\Desktop\CoupaQueries.xlsx").Sheets("Sheet1") '<--| get and reference an instance of "Sheet1" sheet of wanted workbook 
     With .Cells(.Rows.Count, 1).End(xlUp) '<--| reference referenced sheet column A first empty cell after last not empty one 
      .Offset(1, 0).Value = PersonName 
      .Offset(1, 1).Value = PersonAddress 
      .Offset(1, 2).Value = PersonSubject 
      .Offset(1, 3).Value = PersonDate 
     End With 
     .Parent.Save '<--| save parent object of currently referenced object: being this latter a worksheet, its parent object is the workbook it belongs to 
    End With 
    objExcel.Quit 
    Set objExcel = Nothing '<--| release application variable 
End Sub 

此外,如果你要運行這個然後,通過許多郵件項目循環內的宏你可以在開始循環之前得到一個Excel引用,用它貫穿並關閉一旦你完成:

Sub main() 
    Dim iMail As Long, nMails As Long 
    Dim MItem As Outlook.MailItem 
    Dim objExcel As Excel.Application '<--| declare an Excel Application object in the main sub 

    Set objExcel = New Excel.Application '<--| get a new Excel application instance before starting the loop 

    For iMail = 1 To nMails 

     ... 
     ... code to get ith mail 
     ... 

     CoupaQueries MItem, objExcel '<--| pass your routine the current mail item and the already gotten Excel application 

    Next 

    objExcel.Quit '<--| quit Excel once the loop has finished 
    Set objExcel = Nothing '<--| release application variable 
End Sub 


Public Sub CoupaQueries(MItem As Outlook.MailItem, objExcel As Excel.Application) 
    Dim PersonName As String, PersonAddress As String, PersonSubject As String, PersonDate As Date 

    PersonName = MItem.SenderName 
    PersonAddress = MItem.SendUsingAccount 
    PersonSubject = MItem.Subject 
    PersonDate = MItem.ReceivedTime 

    With objExcel.Workbooks.Open("C:\Users\a222012\Desktop\CoupaQueries.xlsx").Sheets("Sheet1") '<--| get and reference an instance of "Sheet1" sheet of wanted workbook 
     With .Cells(.Rows.Count, 1).End(xlUp) '<--| reference referenced sheet column A first empty cell after last not empty one 
      .Offset(1, 0).Value = "PersonName" 
      .Offset(1, 1).Value = PersonAddress 
      .Offset(1, 2).Value = PersonSubject 
      .Offset(1, 3).Value = PersonDate 
     End With 
     .Parent.Save '<--| save parent object of currently referenced object: being this latter a worksheet, its parent object is the workbook it belongs to 
    End With 
End Sub 
相關問題