2009-11-19 50 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