gpt4 book ai didi

excel - Outlook 错误 : user-defined type not defined referring to Excel workbook

转载 作者:行者123 更新时间:2023-12-04 22:12:21 24 4
gpt4 key购买 nike

我每天收到 50 封带有 Excel 表格的邮件。我想将每个 Excel 工作表的第一行添加到我计算机上现有的 Excel 工作表中。
我首先编写了一个在本地合并这些工作表的脚本(我下载了 Excel 工作表,然后运行我的脚本),这里一切正常。
现在我尝试将脚本直接放入 Outlook,因此只要收到其中一封电子邮件,就会自动完成。
最初我想添加一个规则,在该基础上应该运行宏,但这不起作用。我找到的解决方案是在“ThisOutlookSession”中的子例程中调用宏:
enter image description here
这是我第一次为 Outlook 编写宏,所以我不确定我是否正确传递了参数。
当我的 Modul2 被调用时,我立即得到错误

user-defined type not defined


上线 Dim wb_master As Workbook , 和 Dim wb_email As Workbook .
这是代码的一个迷你示例(这里它只是将文件名添加到 ID 列中):
Sub Merge_oewaReport(itm As Outlook.MailItem)
Dim wb_path As String
Dim wb_master As Workbook
Dim ws_master As String

Dim objAtt As Outlook.Attachment
Dim FileName As String
Dim wb_email As Workbook
Dim j As Integer
Dim ir_last As Integer

wb_path = "\\swi56prof01\UserData$\heinreca\Documents\Outlook-Dateien\AllData.xlsx"
Set wb_master = Workbooks.Open(wb_path)
ir_last = wb_master.Worksheets(ws_master).Range("A" & Rows.Count).End(xlUp).Row

For Each objAtt In itm.Attachments
FileName = objAtt.DisplayName
Set wb_email = Workbooks.Open(FileName, True, True)
fID = Split(FileName, " - ")
j = wb_master.Worksheets(ws_master).Cells.Find(What:="ID", SearchDirection:=xlNext, SearchOrder:=xlByColumns).Column
wb_master.Worksheets(ws_master).Cells(ir_last + 1, j) = fID(0)
Next
我检查了工具>引用解决方案。 Microsoft Office 16.0 对象库的标记已经存在。
我试图定义新工作簿而不仅仅是工作簿。

编辑:
我决定尝试后期绑定(bind)方法并更改了一些 Dims:
Dim app_master As Object
Dim wb_master As Object
Dim ws_master As Object
Dim ic_last As Integer
其次是:
Set app_master = CreateObject("Excel.Application")
Set wb_master = app_master.Workbooks.Open(wb_path)
Set ws_master = wb_master.Sheets(1)
但是现在它返回一个错误

Variable not defined


在线:
ic_last = ws_master.Cells.Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByColumns).Column
它突出显示“xlPrevious”。

最佳答案

When my module2 is called, I immediatly get the "user-defined type not defined" error, on the line Dim wb_master As Workbook, and on Dim wb_email As Workbook.


有两种可能的方法来解决这个问题:
  • 添加 Excel COM 引用,以便声明的类型可用于 VBA。这称为早期绑定(bind)。
  • 如果您不想添加 Excel COM 引用,请将对象声明为 Object。这称为后期绑定(bind)。

  • Using early binding and late binding in Automation 中阅读更多相关信息和 Early Binding vs. Late Binding: The Essential Guide for VBA Developers文章。

    关于excel - Outlook 错误 : user-defined type not defined referring to Excel workbook,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71956769/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com