MS Outlook macro to strikeout selected text - vba

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

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

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

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

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

Related

Insert a default signature with a picture into an email

I'm trying to write the VBA code that will insert the default Outlook signature into an email that has been generated in Excel. There's a few people that might use the Excel template, so I need the VBA code to pick up the default signature of the user that's creating the email.
I've found various helpful things online, however I can't get it to work. I'm new to VBA so still learning.
The code below works for the email I need - expect the signature as I've removed all that code as I couldn't get it to work.
Any help would be greatly appreciated.
Sub EmailCreator()
ActiveWorkbook.Save
Dim olApp As Outlook.Application
Dim olMail As Outlook.MailItem
Dim blRunning As Boolean
blRunning = True
On Error Resume Next
Set olApp = GetObject(, "Outlook.Application")
If olApp Is Nothing Then
Set olApp = New Outlook.Application
blRunning = False
End If
On Error GoTo 0
strBody = "This is my message in HTML format"
Set olMail = olApp.CreateItem(olMailItem)
With olMail
.Subject = "abc"
.Attachments.Add ActiveWorkbook.FullName
.HTMLBody = strBody
.Display
End With
If Not blRunning Then olApp.Quit
Set olApp = Nothing
Set olMail = Nothing
End Sub
Firstly, Outlook will insert the default signature when you call Display if the message body has not been modified yet.
Secondly, once you call Display (and the message body gets populated with the signature), you will need to merge the two HTML strings - not just set the HTMLBody property (which would wipe out the signature), not concatenate the two HTML strings (which cannot produce a valid HTML document), but merge. The easiest would be to look for the position of the "<body" substring, scroll to the next ">" character (to take care of the body tag with attributes), then insert your HTML after that ">" character.

MailItem.GetInspector.WordEditor returns Nothing

I'm composing an email using VB.Net which is opened in Outlook 2013. I need to the contents to be displayed in its default font which is set by the user.
Now i need to get the default font and set to the email. When i tried it returns null at one place.
//Code:
Private m_valDefaultFontSpec As DefaultFont
Sub GetDefaultFontSpec(Optional blnGetReplyFont As Boolean = False)
Dim objDoc As Document
Dim rng As Range
Dim objDummy As MailItem
On Error Resume Next
' Create a "dummy" mail object,
Set objDummy = Application.CreateItem(olMailItem)
' Get the document object from the current "dummy mail" object.
Set objDoc = objDummy.GetInspector.WordEditor //returns nothing here
' Get the range of the word document object.
Set rng = objDoc.Range
' Get some font properties from the given range.
With rng
m_valDefaultFontSpec.Name = .Font.Name
m_valDefaultFontSpec.Size = .Font.Size
m_valDefaultFontSpec.Bold = .Font.Bold
m_valDefaultFontSpec.Color = .Font.Color
m_valDefaultFontSpec.Italic = .Font.Italic
End With
' Finished with the "dummy" mail, close it.
objDummy.Close olDiscard
On Error GoTo 0
End Sub
Where am i wrong? Any help?
Source
WordEditor property might not be available until the message is shown.
Try to simply set the MailItem.HTMLBody property.

Select from 4th line onwards Word VBA

I am using the following code to copy text and images from Microsoft Word and paste to the body of an Outlook e-mail. I am trying to exclude the first 4 lines from being copied (this code is copying everything in the document). How can I go about doing this?
Sub CopycontentintoOutlook()
Dim oMailItem As Object
Dim oWordApp As Object
Dim oWordDoc As Object
Dim oMailWordDoc As Object
Set oWordApp = CreateObject("Word.Application")
Set oWordDoc = ActiveDocument
oWordDoc.Content.Copy
Set oMailApp = CreateObject("Outlook.Application")
Set oMailItem = oMailApp.CreateItem(0)
With oMailItem
.To = "email"
.Subject = "This email contains Word-formatted text"
.Display
End With
Set oMailWordDoc = oMailApp.ActiveInspector.WordEditor
oMailWordDoc.Application.Selection.Paste
End Sub
I am also wondering if it is possible to use the text in the first line and set this as the email subject?
You can use the GoTo() function with the wdGoToLine value to set the insertion point to a specific line. From there, the MoveEnd() function can set the end of your selection to the end of your document.
' Set start to line 4...
Selection.GoTo What:=wdGoToLine, Which:=wdGoToAbsolute, Count:=4
' Select up to end of document...
Selection.MoveEnd Unit:=wdStory

Make outlook 2003 macro work when word is the editor?

What I have, is a similar piece of code & i made it work with the outlook editor (hard enough) and I am trying to get it to now work with Word acting as the outlook editor. (Users are used to word mail) I tried: To move the code directly into word under this document and it did nothing. To follow code i saw on: creating an objword objdoc and then pairing it with the outlook class type of deal, with no luck. Here is a sample of code:
Sub SetCategory()
Dim olMessage As Outlook.MailItem
Set olMessage = Application.ActiveInspector.CurrentItem
If olMessage.SenderName = donations Then
olMessage.Categories = "donations"
ElseIf olMessage.SenderName = "Donations" Then
olMessage.Categories = "donations"
End If
With olMessage
.Send
End With
End Sub
When using "word mail" you are not using Outlook. This describes how to invoke Outlook from Word. Once Outlook is open you can use Outlook VBA.
http://www.howto-outlook.com/howto/senddocasmail.htm
Untested, and you will have to remove the parts you do not need.
Sub SendDocAsMail()
Dim oOutlookApp As Outlook.Application
Dim oItem As Outlook.MailItem
On Error Resume Next
'Start Outlook if it isn't running
Set oOutlookApp = GetObject(, "Outlook.Application")
If Err <> 0 Then
Set oOutlookApp = CreateObject("Outlook.Application")
End If
On Error GoTo 0 ' <=== Important to see errors now if there are any
'Create a new message
Set oItem = oOutlookApp.CreateItem(olMailItem)
' --------------------------
'Set oItem = oOutlookApp.ActiveInspector.CurrentItem
If oItem.SenderName = donations Then
oItem.Categories = "donations"
ElseIf oItem.SenderName = "Donations" Then
oItem.Categories = "donations"
End If
' --------------------------
'Allow the user to write a short intro and put it at the top of the body
Dim msgIntro As String
msgIntro = InputBox("Write a short intro to put above your default " & _
"signature and current document." & vbCrLf & vbCrLf & _
"Press Cancel to create the mail without intro and " & _
"signature.", "Intro")
'Copy the open document
Selection.WholeStory
Selection.Copy
Selection.End = True
'Set the WordEditor
Dim objInsp As Outlook.Inspector
Dim wdEditor As Word.Document
Set objInsp = oItem.GetInspector
Set wdEditor = objInsp.WordEditor
'Write the intro if specified
Dim i As Integer
If msgIntro = IsNothing Then
i = 1
'Comment the next line to leave your default signature below the document
wdEditor.Content.Delete
Else
'Write the intro above the signature
wdEditor.Characters(1).InsertBefore (msgIntro)
i = wdEditor.Characters.Count
wdEditor.Characters(i).InlineShapes.AddHorizontalLineStandard
wdEditor.Characters(i + 1).InsertParagraph
i = i + 2
End If
'Place the current document under the intro and signature
wdEditor.Characters(i).PasteAndFormat (wdFormatOriginalFormatting)
'Display the message
oItem.Display
'Clean up
Set oItem = Nothing
Set oOutlookApp = Nothing
Set objInsp = Nothing
Set wdEditor = Nothing
End Sub
Edit: Added, based on comment. This is a step that beginners trip on.
"Since this macro also uses Outlook functionality to create the mail we must add the reference to the project. To do this choose Tools-> References… and select Microsoft Outlook 12.0 Object Library (or 14.0 when using Outlook 2010). After this press OK."
Latest Outlook versions use Word as an email editor by default. There is no need to check out the editor type. The WordEditor property of the Inspector class returns the Microsoft Word Document Object Model of the message being displayed. You can read more about that in the Chapter 17: Working with Item Bodies .
Also you may find the How to automate Outlook and Word by using Visual C# .NET to create a pre-populated e-mail message that can be edited article helpful.

Macro VBA to get selected text in Outlook 2003

I am trying to use this code snippet to get the selected text in outlook 2003
Sub SelectedTextDispaly()
On Error Resume Next
Err.Clear
Dim oText As TextRange
''# Get an object reference to the selected text range.
Set oText = ActiveWindow.Selection.TextRange
''# Check to see whether error occurred when getting text object
''# reference.
If Err.Number <> 0 Then
MsgBox "Invalid Selection. Please highlight some text " _
& "or select a text frame and run the macro again.", _
vbExclamation
End
End If
''# Display the selected text in a message box.
If oText.Text = "" Then
MsgBox "No Text Selected.", vbInformation
Else
MsgBox oText.Text, vbInformation
End If
End Sub
When running this macro I get the error
---------------------------
Microsoft Visual Basic
---------------------------
Compile error:
User-defined type not defined
Do I need to add any references to fix this up?
#Kusleika, I tried the option you had suggested and still the same errors came up.
Thanks for the help
May be I had not phrased my question in the proper way
Some more googling revealed that its not possible to get the selected text of a mail in preview pane. http://www.eggheadcafe.com/forumarchives/outlookprogram_VisualBasica/Aug2005/post23481044.asp
So I had to adjust the requirement so that I can do an action from an mail item window.
The following code helped me (had to make some changes to suit my needs)
Sub Blue_Code_Highlight()
Dim msg As Outlook.MailItem
Dim insp As Outlook.Inspector
Set insp = Application.ActiveInspector
If insp.CurrentItem.Class = olMail Then
Set msg = insp.CurrentItem
If insp.EditorType = olEditorHTML Then
Set hed = msg.GetInspector.HTMLEditor
Set rng = hed.Selection.createRange
rng.pasteHTML "<font style='color: blue; font-family:Times New Roman; font-size: 10pt;'>" & rng.Text & "</font><br/>"
End If
End If
Set insp = Nothing
Set rng = Nothing
Set hed = Nothing
Set msg = Nothing
End Sub
Source:http://www.outlookcode.com/threads.aspx?forumid=4&messageid=26992
#Kusleika thanks for the help, can I close this thread. Pls let me know.
Just in case someone is using the word editor instead of html, you can also insert this part:
If insp.EditorType = olEditorWord Then
Set hed = msg.GetInspector.WordEditor
Set word = hed.Application
Set rng = word.Selection
rng.Font.Name = "Times New Roman"
rng.Font.Size = 10
rng.Font.Color = wdColorBlack
End If
to get similar when word is the editor. i tried to paste this into a comment on the accepted answer, but it destroyed the formatting and was pretty useless, so posting as an answer.
Dim oText As Range
TextRange is a property of the TextFrame object. It returns a Range object. There is no TextRange object.