【发布时间】:2019-05-15 13:14:58
【问题描述】:
我正在尝试将附件添加到从模板创建的电子邮件中。这个想法是能够使用文件选择器来选择多个文件,然后 excel 会向正确的收件人发送一封带有正确附件的电子邮件。
问题是我不能使用“.Display”方法而不出现错误,我想在发送前查看电子邮件,所以我不想使用“.Send”。
但是,无论出于何种原因,如果我使用“.Body = ''”清除电子邮件模板正文,我就可以显示电子邮件并附加正确的文件。我想保留模板中的电子邮件正文,就像不清除它并重写它一样。
所以如果我想在发送前先显示,我似乎不能使用电子邮件模板?有没有人遇到过这个问题或知道如何解决?
错误信息是:
'-2147221233(8004010f)' 尝试的操作失败。找不到对象。
顺便说一句,大多数变量都是全局声明的,所以它们是不可见的。
Dim Agency As String
Dim xfullName As Variant
Dim Template As String
Dim mail As Outlook.mailItem
Dim myOlApp As Outlook.Application
Dim selectedFile As Variant
Dim emailBody As String
Dim emailType As String
Dim recipients As String
Sub Recall_Email()
Dim fileName As String
Dim inputFile As FileDialog
Set myOlApp = CreateObject("Outlook.Application")
Set inputFile = Application.FileDialog(msoFileDialogFilePicker)
Template = "C:\Users\me\AppData\Roaming\Microsoft\Templates\Recall Templates\Recall Template.oft"
With inputFile
.AllowMultiSelect = True
If .Show = False Then Exit Sub
End With
For Each selectedFile In inputFile.SelectedItems
xfullName = selectedFile
fileName = Mid(inputFile.SelectedItems(1), InStrRev(inputFile.SelectedItems(1), "\") + 1, Len(inputFile.SelectedItems(1)))
Agency = Left(fileName, 3)
CreateTemplate(Template)
Next selectedFile
End Sub
Private Sub CreateTemplate(temp)
Set myOlApp = CreateObject("Outlook.Application")
Set mail = myOlApp.CreateItemFromTemplate(temp)
Set olAtt = mail.Attachments
With mail
'.Body = "" -- If I use this line, everything attaches
.Subject = Agency & " Recall File"
.To = "email"
.Attachments.Add xfullName
.Display '.Send
End With
End Sub
【问题讨论】:
-
哪一行报错?
-
.在 CreateTemplate(temp) 中显示
-
@DKM 您的
CreateTemplate子程序需要temp的参数,但您没有在您的Recall_Email子程序中给它任何参数... -
@dwirony 你是对的,这是我实际修复的错误,与此问题无关。我现在将编辑代码。
-
这将有助于查看您的全局变量,例如“selectedFile”。我们如何知道您是否为该变量声明了 Excel 对象?