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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With