が、私は正確に知りませんあなたのコードに何が問題なのですか?しかし、一方では、編集可能な新しい電子メールが公開されていることを検証していません。次のような概念の証明は、あなたが探していると思っていることを正確に行います。構成中のアクティブな電子メールにテキストを挿入します。これが不可能な場合は、理由を説明するメッセージボックスが表示されます。
テキストを挿入する部分は、Wordが電子メールエディタ(ALWAYS be the case in 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
何も選択されていない場合、実際には、「ActiveInspector」は「Nothing」になります。私は 'ActiveWindow'がどのように' Nothing'になるかも知りません。このコードをどこに置いていますか、どのように呼び出すのですか? –
コードはモジュール内にあり、ユーザーがマクロを手動で実行するか、マクロを実行するメニューボタンをクリックすると、プロシージャが呼び出されます。 – dnLL
メソッド全体のコードを投稿できますか? –