MS Outlook macro to strikeout selected text
Asked Answered
H

4

10

The task is to apply strikeout to current font in selected text area. The difficulty is that Outlook doesn't support recording macros on the fly - it wants code to be written by hand.

For example, the following simple code:

Selection.Font.Strikethrough = True

works for Word, but gives an error for Outlook:

Run-time error '424':
Object required
Hereinafter answered 19/11, 2009 at 13:19 Comment(2)
I'm using MS Outlook 2003. The idea is to apply font not to predefined text block (e.g. "The sentence is bold", or matching some pattern in message body), but to manually selected text (i mean, with mouse).Hereinafter
just wanted to follow up to see if the below answers your question.Bumpy
B
11

This assumes that you also have Word installed on your box. If so, you can access most of the Word OM from the Outlook VBE without referencing Word by using the ActiveInspector.WordEditor object.

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
Bumpy answered 31/12, 2009 at 14:25 Comment(1)
+1, and if you don't have Word, or if Outlook is set up to use HTML instead, then you can get to it through ActiveInspector.HTMLEditor.Krefetz
A
1

Here are a few notes on messing around with the open message, there are no checks, it just assumes that you have an open mail item. If you would like to say a little more about what you want to do, and in what version, I may be able to help a little more.

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
Antisepticize answered 19/11, 2009 at 15:59 Comment(1)
The idea is to apply font to manually selected (e.g, with mouse) text; and message body doesn't have any information about selection.Hereinafter
W
1

You need to access the Inspector's HTMLEditor or WordEditor. Check the help file for sample code. If you are using WordEditor then you can record macro in Word and incorporate the resultant code into the Outlook macro by using the WordEditor.

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
Warmth answered 2/3, 2010 at 14:12 Comment(0)
E
0

Jumping off from Todd Main's excellent example above.
I slightly modified the code to work in the inline reply pane as we couldn't find a simple way to add strikethrough to the QAT or ribbon.
I also added an if block to toggle the strikethrough if it was already set.

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
Erythema answered 15/1, 2020 at 22:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.