我不知道你的代码到底有什么问题。但是,一方面,您并没有验证新的、可编辑的电子邮件是否已打开。以下概念验证完全符合我的想法:在正在撰写的活动电子邮件中插入一些文本。如果这是不可能的,它会显示一个消息框来解释原因。
插入文本的部分仅在 Word 用作电子邮件编辑器时才有效(在 Outlook 2010+ 中总是如此)。如果不是,您将不得不直接解析和更新 Body 或 HTMLBody 文本。
Sub InsertText()
Dim myText As String
myText = "Hello world"
Dim NewMail As MailItem, oInspector As Inspector
Set oInspector = Application.ActiveInspector
If oInspector Is Nothing Then
MsgBox "No active inspector"
Else
Set NewMail = oInspector.CurrentItem
If NewMail.Sent Then
MsgBox "This is not an editable email"
Else
If oInspector.IsWordMail Then
' Hurray. We can use the rich Word object model, with access
' the caret and everything.
Dim oDoc As Object, oWrdApp As Object, oSelection As Object
Set oDoc = oInspector.WordEditor
Set oWrdApp = oDoc.Application
Set oSelection = oWrdApp.Selection
oSelection.InsertAfter myText
oSelection.Collapse 0
Set oSelection = Nothing
Set oWrdApp = Nothing
Set oDoc = Nothing
Else
' No object model to work with. Must manipulate raw text.
Select Case NewMail.BodyFormat
Case olFormatPlain, olFormatRichText, olFormatUnspecified
NewMail.Body = NewMail.Body & myText
Case olFormatHTML
NewMail.HTMLBody = NewMail.HTMLBody & "<p>" & myText & "</p>"
End Select
End If
End If
End If
End Sub