Remove space before and after in Outlook - vba

I'm attempting to write a macro for Outlook (never written a macro, or VBA for that matter) that will remove the space before and after the text that I have selected.
This is what I've cobbled together from examples that I've found:
Sub FixParagraphSpacing()
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.ParagraphFormat.SpaceBefore = 0
objSel.ParagraphFormat.SpaceAfter = 0
Set objOL = Nothing
Set objDoc = Nothing
Set objSel = Nothing
End Sub
The problem is that the code executes and almost nothing happens. The body of the email isn't affected, but I'm not able to remove the spacing before and after manually anymore because Outlook thinks it's already been done.
What am I missing here?
Update
Here is my updated code, based on #KevinPope's answer:
Sub FixParagraphSpacing()
Dim objOL As Application
Dim sel As Object
Set objOL = Application
Set sel = objOL.ActiveInspector().WordEditor.Application.Selection
For Each para In sel.Paragraphs
para.SpaceBefore = 0
para.SpaceAfter = 0
Next para
End Sub
Before I run the code, here's what I see under Line and Paragraph Spacing:
And here's what I see after I run the macro:
Unfortunately, other than this, no visible change is made in the body of the email.
Screenshot of text per request:

I too faced the same problem.
When running the macro, it does seem to update the values (space before/after to 0), but doesnt apply the settings to the selected text.
But then adding the SpaceBeforeAuto = False worked...
Sub FixParagraphSpacing()
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.ParagraphFormat.SpaceBefore = 0
objSel.ParagraphFormat.SpaceBeforeAuto = False
objSel.ParagraphFormat.SpaceAfter = 0
objSel.ParagraphFormat.SpaceAfterAuto = False
Set objOL = Nothing
Set objDoc = Nothing
Set objSel = Nothing
End Sub

Try using "Selection.WholeStory" before you set the paragraph formatting. It worked for me.

Something like this should sort out the line spacing before and after a selected Paragraph:
Sub Test()
Dim objOL As Application
Dim sel As Object
Set objOL = Application
Set sel = objOL.ActiveInspector().WordEditor.Application.Selection
sel.Paragraphs(1).SpaceBefore = 0
sel.Paragraphs(1).SpaceAfter = 0
End Sub
Let me know if this doesn't work and we can iterate on it.

Related

How to insert a content control checkbox into an Outlook task using Word VBA?

I would like add a working checkbox (content control?) to an existing task, like a list of sub-tasks, that could just be checked off.
Within Outlook I referenced the Microsoft Word 16.0 Object Library, and I have tried the suggestions at http://www.vboffice.net/en/developers/use-word-macro-in-outlook/ and https://www.slipstick.com/developer/word-macro-apply-formatting-outlook-email/ without success.
I tried
Option Explicit
Public Sub Checkbox()
Dim objItem As Object
Dim objInsp As Outlook.Inspector
' Add reference to Word library
' in VBA Editor, Tools, References
Dim objWord As Word.Application
Dim objDoc As Word.Document
Dim objSel As Word.Selection
On Error Resume Next
' Reference the current Outlook item
Set objItem = Application.ActiveInspector.CurrentItem
If Not objItem Is Nothing Then
If objItem.Class = olMail Then
Set objInsp = objItem.GetInspector
If objInsp.EditorType = olEditorWord Then
Set objDoc = objInsp.WordEditor
Set objWord = objDoc.Application
Set objSel = objWord.Selection
' Formatting code goes here
Selection.Range.ContentControls.Add (wdContentControlCheckBox)
End If
End If
End If
Set objItem = Nothing
Set objWord = Nothing
Set objSel = Nothing
Set objInsp = Nothing
End Sub
I have also tried
Public Sub Check2()
Dim Ins As Outlook.Inspector
Dim Document As Word.Document
Dim Word As Word.Application
Dim Selection As Word.Selection
Set Ins = Application.ActiveInspector
Set Document = Ins.WordEditor
Set Word = Document.Application
Set Selection = Word.Selection
Selection.Range.ContentControls.Add (wdContentControlCheckBox)
End Sub
The first one didn't do anything, as I recall.
The second one showed
Run-time error '445'".
Object doesn't support this action
Your VBA code works correctly:
Public Sub Check2()
Dim Ins As Outlook.Inspector
Dim Document As Word.Document
Dim Word As Word.Application
Dim Selection As Word.Selection
Set Ins = Application.ActiveInspector
Set Document = Ins.WordEditor
Set Word = Document.Application
Set Selection = Word.Selection
Selection.Range.ContentControls.Add (wdContentControlCheckBox)
End Sub
You just needed to add a reference to the Word object library (Tools -> References):

Programmatically change properties in email body in Outlook with VBA

I have an email ready to be sent in Outlook 2013
I want to scan the body of the email for bold text (i.e., bold characters) and change its color to red
(nice to have) Exclude from the macro the signature
I put together the code below but still not working. Any ideas?
Public Sub FormatSelectedText()
Dim objItem As Object
Dim objInsp As Outlook.Inspector
' Add reference to Word library
' in VBA Editor, Tools, References
Dim objWord As Word.Application
Dim objDoc As Word.Document
Dim objSel As Word.Selection
On Error Resume Next
'Reference the current Outlook item
Set objItem = Application.ActiveInspector.CurrentItem
If Not objItem Is Nothing Then
If objItem.Class = olMail Then
Set objInsp = objItem.GetInspector
If objInsp.EditorType = olEditorWord Then
Set objDoc = objInsp.WordEditor
Set objWord = objDoc.Application
Set objSel = objWord.Selection
Set objChar = Characters.Selection
' replace the With block with your code
With objChar
' Formatting code goes here
'.Font.Size = 18
If .Font.Bold = True Then
.Font.Color = wdColorBlue
End If
.Font.Color = wdColorRed
'.Font.Italic = True
'.Font.Name = "Arial"
End With
For Each Char In Characters.Selection
If Char.Font.Bold Then
Char.Font.Color = RGB(0, 0, 255) 'TextRGBTmp
End If
Next Char
For Each Char In Characters.Selection
If Not Char.Font.Bold And Char.Font.Color = RGB(0, 0, 255) Then
Char.Font.Color = RGB(0, 0, 0)
End If
Next Char
End If
End If
End If
Set objItem = Nothing
Set objWord = Nothing
Set objSel = Nothing
Set objInsp = Nothing
End Sub
This is a follow up to question: Programmatically change font properties in email body
first of all: don't use On Error Resume Next when you're trying to debug your code. It makes your life harder.
second: use Option Explicit at the beginning of the module. With that option enabled, VBA will show you every variable that's not initialized (some bugs only occur from misspellings).
I've corrected your code, so it works for me:
Public Sub FormatSelectedText()
Dim objOutlook As Outlook.Application ' i used this because im working in MS Access
Dim objItem As Object
Dim objInsp As Outlook.Inspector
' Add reference to Word library
' in VBA Editor, Tools, References
Dim objWord As Word.Application
Dim objDoc As Word.Document
Dim objSel As Word.Selection
Dim objChar As Object
Dim Char As Object
'Reference the current Outlook item
Set objOutlook = GetObject(, "Outlook.Application")
Set objItem = objOutlook.ActiveInspector.CurrentItem
If Not objItem Is Nothing Then
If objItem.Class = olMail Then
Set objInsp = objItem.GetInspector
If objInsp.EditorType = olEditorWord Then
Set objDoc = objInsp.WordEditor
Set objWord = objDoc.Application
Set objSel = objWord.Selection
Set objChar = objSel.Characters ' this wasn't initialized
' replace the With block with your code
' With objChar ' you don't Need this block because objChar is an array and it throws an error when you try to use this code on the whole objChar object
' ' Formatting code goes here
' '.Font.Size = 18
' If .Font.Bold = True Then
' .Font.color = wdColorBlue
' End If
' .Font.color = wdColorRed
' '.Font.Italic = True
' '.Font.Name = "Arial"
' End With
For Each Char In objSel.Characters
If Char.Font.Bold Then
Char.Font.color = rgb(255, 0, 0) 'TextRGBTmp (the rgb was filled backwards, so the text became blue. i fixed it.
End If
Next Char
' the code of the second For Each was not neccessary.
End If
End If
End If
Set objItem = Nothing
Set objWord = Nothing
Set objSel = Nothing
Set objInsp = Nothing
End Sub

Programmatically change font properties in email body

I have been successfully programming this in PowerPoint VBA but haven't been able to make it work on Outlook.
I have an email ready to be sent in Outlook 2013
I want to scan the body of the email for bold text (i.e., bold characters) and change its color to red
(nice to have) Exclude from the macro the signature
I tried several attempts with "Substitute", "if"-loop but no success. Thanks a lot for putting me on the right track.
The following code converts the color of the body but does not discriminate for bold words. Any ideas?
Public Sub FormatSelectedText()
Dim objItem As Object
Dim objInsp As Outlook.Inspector
' Add reference to Word library
' in VBA Editor, Tools, References
Dim objWord As Word.Application
Dim objDoc As Word.Document
Dim objSel As Word.Selection
On Error Resume Next
'Reference the current Outlook item
Set objItem = Application.ActiveInspector.CurrentItem
If Not objItem Is Nothing Then
If objItem.Class = olMail Then
Set objInsp = objItem.GetInspector
If objInsp.EditorType = olEditorWord Then
Set objDoc = objInsp.WordEditor
Set objWord = objDoc.Application
Set objSel = objWord.Selection
' replace the With block with your code
With objSel
' Formatting code goes here
'.Font.Size = 18
If .Font.Bold = True Then
.Font.Color = wdColorBlue
End If
.Font.Color = wdColorRed
'.Font.Italic = True
'.Font.Name = "Arial"
End With
End If
End If
End If
Set objItem = Nothing
Set objWord = Nothing
Set objSel = Nothing
Set objInsp = Nothing
End Sub
First of all, I'd suggest starting from the Getting Started with VBA in Outlook 2010 article in MSDN.
You can use the HTMLBody property of Outlook items to get the HTML content of the message body or use the Word object model to get the job done. The WordEditor property of the Inspector class returns an instance of the Document class from the WOM (Word object model). See Chapter 17: Working with Item Bodies for more information.

Outlook 2010 Select Calendar

I have some users who would like to have multiple calendars selected when they switch to calendar folder in theirs Outlook.
So I took a sample code from:
http://www.slipstick.com/developer/code-samples/select-multiple-calendars-outlook/
Modified it a little and gave to my users. Problem is that it is not working on one of these Outlooks and I cannot find out why.
Below is my code and the exact problem is that this macro cannot "select/enable" the calendar I want - but if I try to debug the code and put MsgBox for testing - looks like the code is in right place. Procmon is not showing any "access denied" or other kind of errors.
Could You help me to investigate this ?
Sub SelectCalendars()
Dim objPane As Outlook.NavigationPane
Dim objModule As Outlook.CalendarModule
Dim objGroupA, objGroupB As Outlook.NavigationGroup
Dim objNavFolder As Outlook.NavigationFolder
Dim objCalendar As Folder
Dim objFolder As Folder
Dim i As Integer
Dim test As Outlook.NavigationFolder
Set Application.ActiveExplorer.CurrentFolder = Session.GetDefaultFolder(olFolderCalendar)
DoEvents
Set objCalendar = Session.GetDefaultFolder(olFolderCalendar)
Set objPane = Application.ActiveExplorer.NavigationPane
Set objModule = objPane.Modules.GetNavigationModule(olModuleCalendar)
With objModule.NavigationGroups
Set objGroupA = .Item(1)
End With
' First calendar group
Set objNavFolder = objGroupA.NavigationFolders.Item(1)
MsgBox objNavFolder.DisplayName
MsgBox objNavFolder.IsSelected
objNavFolder.IsSelected = False
' Second calendar group
Set objNavFolder = objGroupA.NavigationFolders.Item(2)
MsgBox objNavFolder.DisplayName
MsgBox objNavFolder.IsSelected
objNavFolder.IsSelected = False
Set objPane = Nothing
Set objModule = Nothing
Set objGroup = Nothing
Set objNavFolder = Nothing
Set objCalendar = Nothing
Set objFolder = Nothing
End Sub
First of all, I'd suggest using the Debug.Print statements instead of Message boxes.
The IsSelected property of the NavigationFolder class allows to set a boolean variable that indicates whether the NavigationFolder object is selected for display. Try to set this property to true (instead of false).

VBA Outlook How to add hyperlink into email body

This macro adds hyperlink to email :
Sub test_add_hyperlink()
Dim NewMail As Outlook.MailItem
Set NewMail = Application.ActiveInspector.CurrentItem
NewMail.HTMLBody = "<HTML><BODY><A href=http://www.someaddress.com>URL_TEXT</A></BODY></HTML>" & NewMail.HTMLBody End Sub
but how to add hyperlink in place where active cursor is ? I ask beacause I would like to add hyperlink not at the front of message, but where my currently writing message.
The hyperlink I would like to add is the hyperlink to file which is currently copied to Windows' clipboard, this part I've written, but I can't figure out how to place it not at the front of email, but in place where active cursor is. I think that macro based emulation of Windows' keypressing is one of the directions to follow.
This describes how to paste at the selection.
http://www.slipstick.com/developer/code-samples/paste-formatted-text-vba/
Sub PasteFormattedClipboard()
Dim objItem As Object
Dim objInsp As Outlook.Inspector
Dim objWord As Word.Application
Dim objDoc As Word.Document
Dim objSel As Word.Selection
Set objItem = Application.ActiveInspector.CurrentItem
Set objInsp = objItem.GetInspector
Set objDoc = objInsp.WordEditor
Set objWord = objDoc.Application
Set objSel = objWord.Selection
objSel.PasteAndFormat (wdFormatOriginalFormatting)
Set objItem = Nothing
Set objInsp = Nothing
Set objDoc = Nothing
Set objWord = Nothing
Set objSel = Nothing
End Sub
Sub InsertHyperlinkAtCursorPositon()
On Error GoTo finish
strLink = "http://www.outlookcode.com"
strLinkText = "Get Outlook code samples here"
Set objInsp = Application.ActiveInspector
Set objMsg = objInsp.CurrentItem
Set objDoc = objInsp.WordEditor
Set objSel = objDoc.Windows(1).Selection
If objMsg.BodyFormat <> olFormatPlain Then
objDoc.Hyperlinks.Add objSel.Range, strLink, _
"", "", strLinkText, ""
Else
objSel.InsertAfter strLink
End If
finish:
End Sub