问题
I am trying to create an Outlook email using an Outlook template.
On the Set obApp = Outlook.Application
line, I am receiving the error:
Error: 13 Type Mismatch
I seem to be using the same syntax used in other posts on this site on the subject.
I also tried Set obApp = CreateObject("Outlook.Applciation")
with the same result.
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
回答1:
You have two options to choose from:
Option 1: Early Binding
In order to use early binding, you need to set a reference to:
Microsoft Outlook ##.# Object Library
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.
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
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
Notice in this method Outlook's objects were declared as type Object
.
来源:https://stackoverflow.com/questions/53246493/creating-outlook-application-from-excel-generates-type-mismatch-error