Dim myOlApp As New Outlook.Application
Dim composeitem As Outlook.MailItem
Set myOlApp = CreateObject("Outlook.Application")
composeitem = myOlApp.CreateItem(0)
With composeitem
.To = "abc.xyz@gmail.com"
.Subject = "ABC meeting"
.Body = "Meeting at 5"
.Send
.Display
End With
错误 VBA抛出的是"对象变量或使用块变量未设置" 我已标记所有必需的引用并在 CreateItem(0)行上获得错误。提前谢谢。
答案 0 :(得分:3)
您似乎错过了 CreateItem(0)行中的设置。将该行更改为以下内容:
Set composeitem = myOlApp.CreateItem(0)
此外,您的 .Display 会抛出错误,因为在您运行 .Send 命令后会删除该电子邮件。我建议移动线来防止这种情况。
答案 1 :(得分:1)
composeitem
是一个对象,因此您需要在分配时使用Set
关键字。
我还注意到你已经为Outlook应用程序冗余地包含了两个对象变量。解决它:
Dim myOlApp As New Outlook.Application
Dim composeitem As Outlook.MailItem
' This isn't needed: --> Set OutApp = CreateObject("Outlook.Application")
Set composeitem = myOlApp.CreateItem(0)
With composeitem
.To = "abc.xyz@gmail.com"
.Subject = "ABC meeting"
.Body = "Meeting at 5"
.Send
'.Display you can't display a message that's already been sent
End With