MS Outlook宏将删除所选文本

时间:2009-11-19 13:19:38

标签: vba outlook outlook-vba text-formatting

任务是将删除线应用于所选文本区域中的当前字体。 难点在于Outlook不支持动态记录宏 - 它希望手动编写代码。

例如,以下简单代码:

Selection.Font.Strikethrough = True

适用于Word,但为Outlook提供错误:

Run-time error '424':
Object required

4 个答案:

答案 0 :(得分:13)

这假设您的盒子上还安装了Word。如果是这样,您可以通过使用 ActiveInspector.WordEditor 对象从Outlook VBE访问大多数Word OM而不引用Word。

Sub StrikeThroughinMailItem()
    Dim objOL As Application
    Dim objDoc As Object
    Dim objSel As Object
    Set objOL = Application
    Set objDoc = objOL.ActiveInspector.WordEditor
    Set objSel = objDoc.Windows(1).Selection
    objSel.Font.Strikethrough = True
End Sub

答案 1 :(得分:1)

以下是关于搞乱打开消息的一些注意事项,没有检查,它只是假设您有一个打开的邮件项目。如果您想更多地谈谈您想要做什么,以及在什么版本中,我可以提供更多帮助。

Dim ActiveMessage As MailItem
Dim strHTML As String

Set ActiveMessage = ActiveInspector.CurrentItem
Debug.Print ActiveMessage.Body
Debug.Print ActiveMessage.HTMLBody

strHTML = Replace(ActiveMessage.Body, "This sentence is bold", _
    "<STRONG>This sentence is bold</STRONG>")

ActiveMessage.HTMLBody = strHTML

Debug.Print ActiveMessage.HTMLBody

答案 2 :(得分:1)

您需要访问Inspector的HTMLEditor或WordEditor。检查帮助文件以获取示例代码。如果您使用的是WordEditor,则可以在Word中录制宏,并使用WordEditor将生成的代码合并到Outlook宏中。

Public Sub DoIt()
    'must set word as mail editor
    'must set reference to word object library

    Dim oInspector As Outlook.Inspector
    Dim oDoc As Word.Document
    Dim oItem  As Outlook.MailItem

    Set oItem = Outlook.Application.CreateItem(olMailItem)
    oItem.BodyFormat = olFormatRichText 'must set, unless default is rich text

    Set oInspector = oItem.GetInspector
    oInspector.Display 'must display in order for selection to work

    Set oDoc = oInspector.WordEditor

    'better to use word document instead of selection
    'this sample uses selection because word's macro recording using the selection object

    Dim oSelection As Word.Selection
    Set oSelection = oDoc.Application.Selection

    oSelection.TypeText Text:="The task is to apply strikethroughout."
    oSelection.MoveLeft Unit:=wdCharacter, Count:=4
    oSelection.MoveLeft Unit:=wdCharacter, Count:=7, Extend:=wdExtend

    oSelection.Font.Strikethrough = True

End Sub

答案 3 :(得分:0)

从上述Todd Main的出色示例中脱颖而出。
我稍微修改了代码以使其可以在内联回复窗格中使用,因为我们找不到简单的方法来将删除线添加到QAT或功能区。
我还添加了一个if块来切换删除线(如果已设置)。

Sub StrikeThroughinInlineReply()
    Dim objOL As Application
    Dim objDoc As Object
    Dim objSel As Object
    Set objOL = Application
    Set objDoc = objOL.ActiveExplorer.ActiveInlineResponseWordEditor
    Set objSel = objDoc.Windows(1).Selection
    If objSel.Font.Strikethrough = False Then
        objSel.Font.Strikethrough = True
    Else
        objSel.Font.Strikethrough = False
    End If
End Sub
相关问题