【发布时间】:2016-01-18 17:46:07
【问题描述】:
我在outlook和excel vba中写了一个宏,描述如下: 1. 如果电子邮件主题行匹配,则在 Outlook 中打开 Excel 文件的代码:
Private Sub Items_ItemAdd(ByVal Item As Object)
If TypeOf Item Is Outlook.MailItem Then
'// Subject line here
If InStr(Item.Subject, "Run Dashboard") Then
Call openExcel
End If
End If
End Sub
打开 Excel 并运行仪表板后,必须通过 excel 发送电子邮件。
vba 和代码:
Dim outapp As Object
Dim nmail As Object
Set outapp = CreateObject("Outlook.Application")
Set nmail = outapp.CreateItem(0)
With nmail
.To = "xxxxxx@xxxx.com"
.cc = ""
.bcc = ""
.Subject = flname
.htmlbody = RangetoHTML(Range("A1:" & Split(Cells(, lastcol1).Address, "$")(1) & lastrow1))
.attachments.Add ActiveWorkbook.FullName
.display
End With
On Error GoTo 0
Set nmail = Nothing
Set outapp = Nothing
现在我面临Set outapp = CreateObject("Outlook.Application") 的错误
此错误仅在我通过第 1 点中提到的 Outlook 电子邮件打开 excel 文件时显示,如果我以正常方式打开文件,即没有 Outlook 帮助,则代码运行成功。
请帮忙。
提前致谢
【问题讨论】:
-
而不是
Dim outapp as object你试过Dim outapp as outlook.Application和Dim nmail as outlook.MailItem还检查是否包含Microsoft Outlook参考 -
感谢这个工作:)
-
现在在 excel vba 中出现“Set nmmail = outapp.CreateItem(olMailItem)”错误