我有一个Excel文件,其中包含一个嵌入的word对象,用于编辑电子邮件,然后发送它。
在代码中,问题出现在我将WordEditor对象设置为粘贴邮件之后,之前添加的任何附件都丢失了,如果我在WordEditor处理邮件后更改了代码并添加了它,虽然没有显示错误,但没有附加任何内容。
以下是代码的简化版本:
Dim OlApp As Outlook.Application
Dim Editor As Object
Dim ObjMail as Outlook.Mailitem
Dim WdTag As OLEObject
Dim WdDocTag As Word.Document
Dim WSmail as Worksheet
set WSmail = ThisWorkbook.Sheets("Email")
Set WdTag = WSmail.OLEObjects("WordTags")
WdTag.Verb xlVerbPrimary
Set WdDocTag = WdTag.Object
WdDocTag.Content.Copy
Set OlApp = CreateObject("Outlook.Application")
Set ObjMail = OlApp.CreateItem(olMailItem)
With ObjMail
.Attachments.Add "C:\Users\Me\Desktop\txt.txt",,1
'If I check the Attachments Property of ObjMail here at runtime,
'I can see the information on the attached file.
'However, as soon as the code continues, it vanishes.
.BodyFormat = olFormatRichText
Set Editor = .GetInspector.WordEditor
Editor.Content.Paste
Application.CutCopyMode = False
.To = "Someone"
.Cc = "Someone"
.Subject = "MySubject"
.Display
End with
编辑:
实际上,我发现在设置.BodyFormat = olFormatRichText
之后,在该行之后或之前设置的任何附件都将被放置在消息正文中。
现在的问题是,如何在适当的字段中显示附件,而不是在邮件正文中显示?
发布于 2018-12-05 14:19:44
您可以参考以下代码:
Set oOutlookMail = oOutlook.CreateItem(olMailItem) 'Start a new e-mail
With oOutlookMail
.Display 'Had to move this command here to resolve a bug only existent in Access 2016!
'To Recipient(s)
Set oOutlookRecip = .Recipients.Add(sTo)
oOutlookRecip.Type = olTo
'CC Recipient(s)
If Not IsMissing(sCC) Then
Set oOutlookRecip = .Recipients.Add(sCC)
oOutlookRecip.Type = olCC
End If
'BCC Recipient(s)
If Not IsMissing(sBCC) Then
Set oOutlookRecip = .Recipients.Add(sBCC)
oOutlookRecip.Type = olBCC
End If
.Subject = sSubject 'Subject
Set oOutlookInsp = .GetInspector 'Retains the signature if applicable
.Importance = 1 'Importance Level 0=Low,1=Normal,2=High
' .BodyFormat = olFormatHTML
Set oWordEditor = .GetInspector.WordEditor
'oWordEditor.Content.Paste 'Overwrite any existing content, ie:signature
oWordEditor.Application.Selection.Start = 0
oWordEditor.Application.Selection.Paste
' Add attachments to the message.
If Not IsMissing(AttachmentPath) Then
If IsArray(AttachmentPath) Then
For i = LBound(AttachmentPath) To UBound(AttachmentPath)
If AttachmentPath(i) <> "" And AttachmentPath(i) <> "False" Then
Set oOutlookAttach = .Attachments.Add(AttachmentPath(i))
End If
Next i
Else
If AttachmentPath <> "" And AttachmentPath(i) <> "False" Then
Set oOutlookAttach = .Attachments.Add(AttachmentPath)
End If
End If
End If
For Each oOutlookRecip In .Recipients
If Not oOutlookRecip.Resolve Then
bProbRecip = True
'Display msg to user?
End If
Next
If bProbRecip = False And bEdit = False Then 'Send the e-mail
.Send
End If
End With
有关详细信息,请参阅此链接:
https://stackoverflow.com/questions/53615299
复制相似问题