Removing space before and after in Outlook

I am trying to write a macro for Outlook (I never wrote a macro, or VBA, for that matter), which will remove the space before and after the text that I selected.

Here is what I compiled from the examples I 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 is executing, and almost nothing is happening. The body of the message is not affected, but I cannot remove the before and after interval manually, because Outlook believes that this has already been done.

What am I missing here?

Update

Here is my updated code based on @KevinPope 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 is what I see under Line Spacing:

Remove spacing

And here is what I see after running the macro:

Add spacing

Unfortunately, besides this, there are no visible changes in the body of the letter .


Screenshot with text for request:

enter image description here

+4
source share
3 answers

I had the same problem too. When you run the macro, it seems to update the values ​​(space before / after to 0), but does not apply the settings to the selected text.

But then adding 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 
+2
source

Something like this should sort the line spacing before and after the 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 does not work, and we can continue it.

+1
source

Try using "Selection.WholeStory" before setting paragraph formatting. It worked for me.

+1
source

All Articles