【问题标题】:VBA to format selected text in OutlookVBA 格式化 Outlook 中的选定文本
【发布时间】:2018-11-27 02:24:47
【问题描述】:

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

我已经尝试过了,但出现错误:

Sub Code()

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

End Sub

运行时错误“429”:

ActiveX 组件无法创建对象

【问题讨论】:

  • 你从哪里得到的 Selection 对象?
  • @EugeneAstafiev 我什至不确定是否需要使用它,因为我会在按下按钮之前手动突出显示文本
  • 注意,Word 提供了一个宏记录器,可以为您生成所需的代码。您只需要启动它并手动执行所需的操作。然后你会得到代码。
  • @EugeneAstafiev 这就是我为得到上面不起作用的代码所做的事情
  • 好吧,你不能从 Outlook 运行它。 Outlook 不提供像 Selection 这样的快捷方式。相反,您需要在代码中检索Selection 对象。只需使用 Inspector 类的 WordEditor 属性从 Word 对象模型中获取 Document 类的实例(不要忘记添加 COM 引用)。

标签: vba outlook outlook-2016


【解决方案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

【讨论】:

  • 收到错误Run-time error '91': Object variable or With block variable not set
  • 我猜它在Set objDoc = ActiveInspector.WordEditor 上意味着没有打开的项目。
  • 没有打开的项目是什么意思?
  • 通过我的设置,如果没有活动的可编辑邮件项,我可以重新创建错误。如果您按照问题中的过程进行操作,那么还有其他需要寻找的东西。请确认错误所在的行。
  • 是的,我打开了回复窗口,并突出显示了文本。
【解决方案2】:

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

Outlook 对象模型提供了三种处理项目主体的主要方式:

  1. Body 属性。原始文本。
  2. HTMLBody 属性。正文由 html 标记表示。
  3. Word 对象模型。 Inspector 类的 WordEditor 属性返回代表正文的 Word 文档类的实例。

您可以在 MSDN 的 Chapter 17: Working with Item Bodies 文章中阅读更多相关信息。它深入描述了所有这些属性。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-08
    • 2018-12-19
    • 2018-12-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多