简体   繁体   English

Outlook 错误:未定义用户定义类型引用 Excel 工作簿

[英]Outlook Error: user-defined type not defined referring to Excel workbook

I get 50 mails with Excel sheets per day.我每天收到 50 封 Excel 张的邮件。 I want to add the first line of each Excel sheet to an existing Excel sheet located on my computer.我想将每张 Excel 工作表的第一行添加到我计算机上现有的 Excel 工作表中。

I first wrote a script that merges those sheets locally (I downloaded the Excel sheet and then run my script) and here everything works.我首先编写了一个脚本,在本地合并这些工作表(我下载了 Excel 工作表,然后运行我的脚本),这里一切正常。

Now I tried to put the script directly into Outlook, so this is done automatically whenever one of those emails is received.现在,我尝试将脚本直接放入 Outlook,因此无论何时收到其中一封电子邮件,都会自动完成此操作。

Originally I wanted to add a rule on which basis the macro should have been run, but this does not work.最初我想添加一个规则,宏应该在什么基础上运行,但这不起作用。 The solution I found was to call the macro in a subroutine within the "ThisOutlookSession":我找到的解决方案是在“ThisOutlookSession”的子例程中调用宏:
在此处输入图像描述

This is the first time I am writing a macro for outlook, so I am unsure if I am passing the arguments correctly.这是我第一次为 outlook 编写宏,所以我不确定我是否正确传递了 arguments。

When my Modul2 is called, I immediately get the error当我的 Modul2 被调用时,我立即得到错误

user-defined type not defined未定义用户定义类型

on the line Dim wb_master As Workbook , and on Dim wb_email As Workbook .Dim wb_master As WorkbookDim wb_email As Workbook上。

Here is a mini example of the code (here it simply adds the name of the file into the ID column):这是代码的一个迷你示例(这里它只是将文件名添加到 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

I checked the Tools>references Solution.我检查了工具>参考解决方案。 The tick at Microsoft Office 16.0 Object Library was already there. Microsoft Office 16.0 Object 库中的勾号已经存在。

I tried to define New Workbook instead of just Workbook.我试图定义新工作簿而不仅仅是工作簿。


Edit: I decided to try the late binding method and changed some of my Dims:编辑:我决定尝试后期绑定方法并更改我的一些 Dims:

Dim app_master As Object
Dim wb_master As Object
Dim ws_master As Object
Dim ic_last As Integer

Followed by:其次是:

Set app_master = CreateObject("Excel.Application")
Set wb_master = app_master.Workbooks.Open(wb_path)
Set ws_master = wb_master.Sheets(1)

However now it returns an error但是现在它返回一个错误

Variable not defined变量未定义

at the line:在该行:

ic_last = ws_master.Cells.Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByColumns).Column

It highlights "xlPrevious".它突出显示“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.当我的 module2 被调用时,我立即在 Dim wb_master As Workbook 和 Dim wb_email As Workbook 行上收到“未定义用户定义类型”错误。

There are two possible ways to solve that:有两种可能的方法来解决这个问题:

  1. Add an Excel COM reference so the types declared will be available to VBA. This is called early-binding.添加一个 Excel COM 引用,这样声明的类型将对 VBA 可用。这称为早期绑定。

  2. Declare objects as Object if you don't want to add an Excel COM reference.如果您不想添加 Excel COM 引用,请将对象声明为 Object。 This is called late-binding.这称为后期绑定。

Read more about that in the Using early binding and late binding in Automation and Early Binding vs. Late Binding: The Essential Guide for VBA Developers articles. 在自动化中使用早期绑定和后期绑定以及早期绑定与后期绑定:VBA 开发人员基本指南文章中阅读更多相关信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM