【问题标题】:Searching an Excel Worksheet from Outlook VBA从 Outlook VBA 搜索 Excel 工作表
【发布时间】:2016-04-04 22:00:13
【问题描述】:

我有一个可以打开一些 excel 文件的 Outlook 宏。我想知道如何在 Outlook 宏中使用 Cells.find 语法和 workbooks.Activate 语法。

'OUTLOOK VBA CODE here (works fine)... 
Dim xlApp As Object
Set xlApp = CreateObject("Excel.Application")
With xlApp
.Visible = True
.EnableEvents = False
.DisplayAlerts = False
.ScreenUpdating = False
.Workbooks.Open ("J:\Retail Finance\Varicent\General Resources\Acting Mgr Assignment Bonus Aggregation.xlsx")
For Each x In AttachNames
    'Open the Attachments (one by one as loop iterates)
        .Workbooks.Open ("J:\Retail Finance\Varicent\General Resources\AAA\" & AttachNames(i))
    'Find the Word End, Activate the Cell that it resides in            
'####This syntax doesn't work ####
      .Worksheets.Cells.Find(What:="End", After:=ActiveCell, LookIn:=xlValues, LookAt:= _
        xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
        , SearchFormat:=False).Activate
    'Declare that row the "Endrow"
'####I'm betting this won't work either####
      endrow = ActiveCell.Row
    'Copy the range of Acting & Additional Bonuses in the file
        xlApp.Worksheet.Rows("6:" & endrow - 1).Copy
    'Activate the Aggregation File,
'####Code for activating a particular workbook####
    'find end,
        'Cells.Find(What:="End", After:=ActiveCell, LookIn:=xlValues, LookAt:= _
        xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
        , SearchFormat:=False).Activate

【问题讨论】:

    标签: vba outlook late-binding


    【解决方案1】:

    您的With 块正在使用xlApp,而您以.Worksheets 开头的错误行

    xlApp 没有 .Worksheets 集合,因此它不起作用。您需要先参考工作簿。

    最后,.Worksheets 是一个集合 - 您需要指定您实际要定位的索引(工作表):


    Dim wb As Object
    
    Set wb = .Workbooks.Open ("J:\Retail Finance\Varicent\General Resources\AAA\" & AttachNames(i))
    
    wb.Worksheets(1).Find("something") '// etc etc....
    

    【讨论】:

    • 我结束了 with 语句并尝试... xlApp.Workbooks.Worksheets.Cells.Find(...) 无用。对此有何进一步指导?
    • 同样,WorkbooksWorksheets集合,您需要指定索引。看看我是如何使用Worksheets(1)
    • Set wb = .Worksheets(1).Cells.Find("End") 在将 wb 声明为对象后就可以解决问题。现在,如果我能弄清楚如何激活找到的单元......有这方面的文档吗?
    • "wb.activate" 让我运行时错误 1004:Range 类的激活方法失败。 wb,copy 有效,但我真的想将第 14 行复制到找到“END”-1 的行,所以如果 end 在第 16 行,则将复制第 14 行和第 15 行。如果这太远了,我可以问一个新问题。感谢您刚才针对您的评论提供的该建议的链接。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-28
    • 1970-01-01
    相关资源
    最近更新 更多