Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove space before and after in Outlook

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:

Remove Spacing

And here's what I see after I run the macro:

Add Spacing

Unfortunately, other than this, no visible change is made in the body of the email.


Screenshot of text per request:

enter image description here

like image 531
James Hill Avatar asked Mar 20 '13 11:03

James Hill


2 Answers

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    
like image 182
Rohit Mundhra Avatar answered Sep 30 '22 19:09

Rohit Mundhra


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

like image 24
Bryce Avatar answered Sep 30 '22 20:09

Bryce