通过Outlook VBA从Excel附件查找LastRow

我一直在寻找这个问题的答案,但我似乎无法find一个。 我正在尝试将多个基于Excel的列表作为电子邮件附件接收。 只是为了给出一点上下文,这个macros已经工作了近两年没有错误,但我最近从运行Excel 2007和Outlook 2010的系统切换到运行Excel 2007和Outlook 2007的系统。

下面的行给我一个1004: Application-defined or object-defined error

LR = xlAtt.ActiveSheet.Range("A" & xlAtt.ActiveSheet.Rows.Count).End(xlUp).Row

在上下文中的代码是:

 Private Sub ProcessAttachments(olFolder As Outlook.MAPIFolder) Dim xlApp As Object, xlAtt As Object Dim LR As Long Dim olItem As Outlook.MailItem Dim count As Integer Set xlApp = CreateObject("Excel.Application") xlApp.Visible = False For count = olFolder.Items.Count To 1 Step -1 Set olItem = olFolder.Items.Item(count) If olItem.Class = olMail And olItem.Attachments.Count > 0 Then 'Omitted a few lines here that verify if the attachment is an Excel file 'and then saves it to a folder Set xlAtt = xlApp.Workbooks.Open("pathToFile") xlAtt.Activate LR = xlAtt.ActiveSheet.Range("A" & xlAtt.ActiveSheet.Rows.Count).End(xlUp).Row 'More VBA after 

以上只是代码的一小部分,但希望给予足够的上下文。

我试过testing每个单独的一行给我一个错误,我已经能够缩小到.End(xlUp).Row部分的行。

任何帮助表示赞赏。

Outlook 2007不能识别Excel常量,而OL 2010则可以。

xlUp而不是写xlUp的枚举,即-4162

所以你的代码看起来像这样:

 LR = xlAtt.ActiveSheet.Range("A" & xlAtt.ActiveSheet.Rows.Count).End(-4162).Row 

要在Excel VBA中查找任何常数的枚举,一旦进入VBE,按F2键打开对象浏览器并在双筒望远镜旁边的框中input常量,然后单击双眼。 点击search结果中的常量,底部的框将显示枚举。

或者,您可以为枚举设置一个常量variables,并在您的语法中仍然使用xlUp

 Constant xlUp = -4162