VBA格式化Outlook中的选定文本

时间:2018-06-18 09:12:52

标签: vba outlook outlook-vba outlook-2016

我想突出显示电子邮件中的文字并将其格式化为字体控制并缩进一次。

我试过这个但是收到错误:

Sub Code()

    Selection.Font.Name = "Consolas"
    Selection.Paragraphs.Indent

End Sub
  

运行时错误' 429':

     

ActiveX组件无法创建对象

2 个答案:

答案 0 :(得分:1)

看起来您正在尝试将Word对象模型与Outlook混合使用。 Outlook中的Selection类与Word对象模型中的Selection类不同。此外,Outlook中没有这样的捷径。您必须在每次需要时检索它。

Outlook对象模型提供了三种使用项主体的主要方法:

  1. Body属性。原文。
  2. HTMLBody属性。正文由html标记表示。
  3. Word对象模型。 WordEditor类的Inspector属性返回表示正文的Word Document类的实例。
  4. 您可以在MSDN的Chapter 17: Working with Item Bodies文章中详细了解相关内容。它深入地描述了所有这些属性。

答案 1 :(得分:1)

您可以使用WordEditor编辑邮件中的选定文本:

Private Sub Code()

    ' Mail must be in edit mode - compose, reply, forward
    ' If reading mail then under Actions | Edit Message

    ' Select some text

    Dim objDoc As Object
    Dim objSel As Object

    Set objDoc = ActiveInspector.WordEditor
    Set objSel = objDoc.Windows(1).Selection

    objSel.Font.name = "Consolas"
    objSel.Paragraphs.Indent

End Sub

带验证的代码:

Sub FormatSelection()

    ' With extra validation for troubleshooting

    ' Code in Outlook

    ' Mail must be in edit mode - compose, reply, forward
    ' If reading mail then under Actions | Edit Message

    ' Select some text

    Dim myInspector As Inspector
    Dim myObject As Object
    Dim myItem As mailItem

    Dim myDoc As Word.Document
    Dim mySelection As Word.Selection

    Set myInspector = ActiveInspector

    If myInspector Is Nothing Then
        MsgBox "No inspector. Open a mailitem and select some text."
        GoTo ExitRoutine
    End If

    If myInspector.EditorType <> olEditorWord Then
        'https://msdn.microsoft.com/en-us/vba/outlook-vba/articles/oleditortype-enumeration-outlook
        '  olEditorWord / 4 / Microsoft Office Word editor
        Debug.Print "EditorType: " & myInspector.EditorType
        MsgBox "Editor is not Microsoft Office Word editor"
        GoTo ExitRoutine
    End If

    ' Probably not needed. EditorType should be enough
    'If myInspector.IsWordMail = False Then
    '    MsgBox "myInspector.IsWordMail = False"
    '    GoTo ExitRoutine
    'End If

    On Error Resume Next
    Set myObject = myInspector.currentItem
    On Error GoTo 0

    If myObject Is Nothing Then
        MsgBox "Open a mailitem and select some text."
        GoTo ExitRoutine
    End If

    If myObject.MessageClass = "IPM.Note" Then
        'Should be equivalent to If myObject.Class = olMail Then

        Set myItem = myObject

        Set myDoc = myInspector.WordEditor

        Set mySelection = myDoc.Application.Selection
        Debug.Print "Selected text is: " & mySelection
        MsgBox "Selected text is: " & vbCr & vbCr & mySelection

        mySelection.Font.name = "Consolas"
        mySelection.Paragraphs.Indent

    Else

        MsgBox "Not a mailitem. Open a mailitem and select some text."
        GoTo ExitRoutine

    End If

ExitRoutine:

    Set myInspector = Nothing
    Set myObject = Nothing
    Set myItem = Nothing

    Set myDoc = Nothing
    Set mySelection = Nothing

End Sub