Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
VBA-Outlook到Excel:对象_全局的方法行失败_Vba_Excel_Outlook - Fatal编程技术网

VBA-Outlook到Excel:对象_全局的方法行失败

VBA-Outlook到Excel:对象_全局的方法行失败,vba,excel,outlook,Vba,Excel,Outlook,我在每一封点击outlook的电子邮件上都运行一个脚本。脚本需要打开指定的excel文档并保存发件人的姓名和地址、主题和日期。我收到运行时错误1004:对于我收到的一些邮件,而不是所有邮件,object\u global的方法行失败。我已经在代码中指定了发生错误的行。这是一个参考问题吗 Public Sub CoupaQueries(MItem As Outlook.MailItem) Dim objOutlook As Outlook.Application PersonName = MI

我在每一封点击outlook的电子邮件上都运行一个脚本。脚本需要打开指定的excel文档并保存发件人的姓名和地址、主题和日期。我收到运行时错误1004:对于我收到的一些邮件,而不是所有邮件,object\u 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

简化代码,让错误显示发生的位置,例如

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对象(例如,
工作表
对象)以访问其成员(例如,
属性)

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
Public子库系列(作为Outlook.mailtime)
Dim objExcel作为Excel.Application
Dim objOutlook As Outlook.Application'由于您的客户端应用程序是
Outlook
,您没有隐式引用Excel应用程序对象模型,因此您必须显式引用每个Excel对象(例如,
工作表
对象)以访问其成员(例如,
属性)

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
Public子库系列(作为Outlook.mailtime)
Dim objExcel作为Excel.Application

Dim objOutlook As Outlook.Application'尝试使用
wks.Rows.Count
而不仅仅是
行。Count
在打开工作簿时引用工作簿-
设置wkb=objExcel.Workbooks.open(“C:\Users\a222012\Desktop\CoupaQueries.xlsx”)
。在打开活动工作簿后的一瞬间,它很可能不是活动工作簿。正如@gizlmo所说,Outlook不理解什么是
Rows.Count
。@gizlmo谢谢你,伙计,这似乎已经解决了这个问题。@DarrenBartrup-Cook谢谢你,这个助手使用
wks.Rows.Count
而不仅仅是
Rows.Count
打开工作簿时参考它-
设置wkb=objExcel.Workbooks.open(“C:\Users\a222012\Desktop\CoupaQueries.xlsx”)
。很可能在打开活动工作簿后的一瞬间它就不是活动工作簿了。正如@gizlmo所说的那样-Outlook不理解
行数是什么。@gizlmo谢谢伙计,这似乎已经解决了问题。@DarrenBartrup-Cook谢谢伙计,这很有帮助