我得到
编译错误:未定义用户定义的类型。
黄色突出显示用于此行:
Public Function SendMessage(strSubject, strRecip, strMsg, strAttachment) As Boolean
此行的蓝色突出显示:
Dim mItem As Outlook.MailItem
Option Explicit
Public Function SendMessage(strSubject, strRecip, strMsg, strAttachment) As Boolean
Dim mOutlookApp As Object
Dim mNameSpace As Object
Set mOutlookApp = GetObject("", "Outlook.application")
Set mNameSpace = mOutlookApp.GetNamespace("MAPI")
Dim mFolder As Object
Dim mItem As Outlook.MailItem
Set mItem = mOutlookApp.CreateItem(0)
mItem.To = "Americas"
mItem.CC = strRecip
mItem.SentOnBehalfOfName = "Jordan"
mItem.Subject = strSubject
mItem.Body = strMsg
mItem.Attachments.Add strAttachment
mItem.Display
mItem.Recipients.ResolveAll
End Function
Sub Summarydraft()
Dim result As Boolean
Dim strRecip As String
Dim strSubject As String
Dim strMsg As String
Dim strAttachment As String
Dim LastRow As Long
LastRow = Worksheets("Control").Cells(Rows.Count, "N").End(xlUp).Row
Dim rng As Range, fullrng As Range
Set fullrng = Worksheets("Control").Range("N3:N" & LastRow)
Dim recip As String
For Each rng In fullrng
recip = recip & "; " & rng.Value
Next
strRecip = recip
strSubject = Worksheets("Control").Range("G14")
strMsg = Worksheets("Control").Range("G17")
strAttachment = Worksheets("Control").Range("G20")
result = SendMessage(strSubject, strRecip, strMsg, strAttachment)
End Sub
更改行
Dim mItem As Outlook.MailItem
至
Dim mItem As Object
或者将Outlook添加为VBA工程中的引用。
如果要在代码中声明Outlook类型,则需要向VBA项目添加Outlook COM引用。
在代码中,您使用了后期绑定技术。此技术使用Visual BasicGetObject
函数或CreateObject
函数来初始化Outlook。
若要使用早期绑定,首先需要设置对Outlook对象库的引用。使用Visual Basic for Applications(VBA(Tools
菜单上的"引用"命令可以设置对MicrosoftOutlook xx.x Object Library
的引用,其中xx.x
表示您正在使用的Outlook版本。然后,您可以使用以下语法启动Outlook会话。
Dim objOL as Outlook.Application
Set objOL = New Outlook.Application
在Visual Basic应用程序中的自动化Outlook文章中了解更多信息。