代码之家  ›  专栏  ›  技术社区  ›  Matt

在outlook中设置选定文本格式的vba

  •  0
  • Matt  · 技术社区  · 6 年前

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

    我试过了,但有个错误:

    Sub Code()
    
        Selection.Font.Name = "Consolas"
        Selection.Paragraphs.Indent
    
    End Sub
    

    运行时错误“429”:

    ActiveX组件无法创建对象

    2 回复  |  直到 6 年前
        1
  •  1
  •   niton    6 年前

    您可以使用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
    
        2
  •  1
  •   Eugene Astafiev    6 年前

    看起来您正在尝试将word对象模型与outlook混合使用。这个 Selection outlook中的类与 选择 从word对象模型初始化。而且,在outlook中没有这样的捷径。你每次需要的时候都必须找回它。

    outlook对象模型提供了使用项目体的三种主要方式:

    1. 这个 Body 财产。原始文本
    2. 这个 HTMLBody 财产。正文由HTML标记表示。
    3. 对象模型一词。这个 WordEditor 财产 Inspector 类返回表示正文的Word文档类的实例。

    你可以在 Chapter 17: Working with Item Bodies MSDN中的文章。它深入地描述了所有这些属性。

    推荐文章