从 Excel 创建 Outlook 应用程序生成类型不匹配错误 [英] Creating Outlook application from Excel generates type mismatch error
问题描述
我正在尝试使用 Outlook 模板创建 Outlook 电子邮件.
I am trying to create an Outlook email using an Outlook template.
在 Set obApp = Outlook.Application
行上,我收到错误:
On the Set obApp = Outlook.Application
line, I am receiving the error:
错误:13 类型不匹配
Error: 13 Type Mismatch
我似乎使用了本网站上其他帖子中关于该主题的相同语法.
I seem to be using the same syntax used in other posts on this site on the subject.
我也试过 Set obApp = CreateObject("Outlook.Applciation")
得到相同的结果.
I also tried Set obApp = CreateObject("Outlook.Applciation")
with the same result.
我在工具"->参考"中检查了 OLE 自动化、Microsoft Outlook 16.0 对象库、Microsoft Office 16.0 库和 Microsoft Excel 16.0 对象库以及 Visual Basic for Applications.
I have OLE Automation, Microsoft Outlook 16.0 Object Library, Microsoft Office 16.0 Library, and Microsoft Excel 16.0 Object Library, and visual Basic for Applications all checked in Tools-> References.
Sub CreateEmailfromTemplate()
Dim obApp As Application
Dim NewMail As Outlook.MailItem
Set obApp = Outlook.Application 'THE PROBLEM IS HERE
Set NewMail = obApp.CreateItemFromTemplate("F:\Folder1\Automation\EmailTemplates\TEST TEST.oft")
NewMail.Display
End Sub
推荐答案
您有两个选项可供选择:
You have two options to choose from:
为了使用早期绑定,你需要设置一个引用:
In order to use early binding, you need to set a reference to:
Microsoft Outlook ##.# Object Library
可以在 VBE > Tools > References 中完成.由于您已经声明了变量的方式,我认为这是您更喜欢的方法.
Which can be done in the VBE > Tools > References. I assume this is the method you are preferring due to the way you have already declared your variables.
使用此方法的代码中的问题是在语句Dim xxxx As Application
中,As Application
指的是Excel 的对象模型.您需要指定您要使用 Outlook.
The issue in your code using this method is that in the statement Dim xxxx As Application
, As Application
is referring to Excel's Object Model. You need to specify you are wanting to use Outlook.
Sub CreateEmailfromTemplate()
Dim obApp As New Outlook.Application '<-- Notice Change
Dim NewMail As Outlook.MailItem
Set NewMail = obApp.CreateItemFromTemplate("F:\Folder1\Automation\EmailTemplates\TEST TEST.oft")
NewMail.Display
End Sub
选项 2:后期绑定
您不需要在此方法中设置引用,但 Outlook 的类型和常量在编译时将不可用.编译器将在运行时获取这些.
Option 2: Late Binding
You will not need to set a reference in this method, but Outlook's types and constants will not be available at compile time. The compiler will obtain these during runtime.
Sub CreateEmailfromTemplate()
Dim obApp As Object
Dim NewMail As Object
Set obApp = CreateObject("Outlook.Application")
Set NewMail = obApp.CreateItemFromTemplate("F:\Folder1\Automation\EmailTemplates\TEST TEST.oft")
NewMail.Display
End Sub
注意此方法中 Outlook 的对象被声明为 Object
类型.
Notice in this method Outlook's objects were declared as type Object
.
这篇关于从 Excel 创建 Outlook 应用程序生成类型不匹配错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!