2009-11-19 82 views
10

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

例如,下面的简单代码:

Selection.Font.Strikethrough = True 

作品的词,但给人的Outlook中的错误:

Run-time error '424': 
Object required 
+0

我使用MS Outlook 2003中的想法是不适用字体预定义的文本块(例如,“这句话是粗体”,或匹配消息主体一些图案),但手动选择的文本(ⅰ意思是,用鼠标)。 – Andy 2009-11-20 13:33:03

+0

只是想跟进,看看下面是否回答你的问题。 – 2011-02-18 20:07:56

回答

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 
+1

这个想法是应用字体手动选择(例如,用鼠标)文本;并且邮件正文没有任何关于选择的信息。 – Andy 2009-11-20 13:34:36

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 
+2

+1,如果您没有Word,或者Outlook设置为使用HTML,则可以通过“ActiveInspector.HTMLEditor”进行访问。 – Aaronaught 2009-12-31 17:24:01

1

您需要访问检查器的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