VBA – Outlook到Excel:对象_global的方法行失败

我正在每个电子邮件上运行一个脚本,打我的前景。 该脚本需要打开指定的Excel文档,并保存发件人姓名和地址,主题和date。 我得到运行时错误1004:对象_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 

简化代码以使错误显示出现的位置,例如,

 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 

由于您的客户端应用程序是Outlook ,因此您没有隐式引用Excel应用程序对象模型,因此您必须显式引用每个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 

而且,如果你需要在一个循环中运行这个macros,那么你可以在启动循环之前得到一个Excel引用,在整个循环中使用它,并在完成之后closures它:

 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