SpaceBefore and SpaceAfter will not work.

R

Robert

Dear Experts,
I need to design a macro to process many old documents in the same
way. Unfortunately most of them contain hard paragraph formatting,
with two pilcrows. The User must manually select about 20 lines, and
the macro must change various aspects of formatting and remove the old
hard paras from the Selection. To enable line numbering, a section
needs to be created within which the paragraphs (now converted to
single-pilcrow) need to be reformatted with SpaceAfter (and/or
Before) to correct the spacing.

My attempt at the macro is shown below. Everything works except the
spacing between paragraphs. I have tried several routines gleaned from
Newsgroups and MS Help. Mostly they DO alter the spacing attribute
(to judge from the paragraph Format dialog), but the newly-formatted
text is not displayed with any space - remaining stubbornly UNSPACED.

I have also experimented with another macro containing only a
published Spacing Routine, but that didn't work either. The Document
is usually one page long, with no special formatting before the macro
is run.

All helpful suggestions will be gratefully received. Thank you.

Sub Macro16A()
'
' Attempt at Line Numbering a Selection and reformatting the Section
containing it.
' Macro created 15 July 2009 by Robert Patton
' Select all text to be numbered.

' Call with CTRL + ALT + [

Dim myRange As Range
Dim para As Paragraph

If Selection.Type = wdSelectionIP Then
MsgBox Prompt:="You haven't selected any text! Exiting
Procedure..."
Exit Sub
End If

Set myRange = ActiveDocument.Range(Start:=Selection.Start,
End:=Selection.End)

'This removes double-paragraphs in older documents. Works OK.
With Selection.Find
.ClearFormatting
.Text = vbCr & vbCr
.Replacement.ClearFormatting
.Replacement.Text = vbCr
.Execute Replace:=wdReplaceAll, Forward:=True, Wrap:=wdFindStop
End With

myRange.InsertBreak Type:=wdSectionBreakContinuous

With Selection
.Collapse Direction:=wdCollapseEnd
.InsertParagraphAfter ' ADDED.
.InsertBreak Type:=wdSectionBreakContinuous
.MoveUp Unit:=wdLine, Count:=8 ' Places IP within the Section.
.Sections(1).Range.Select
.ParagraphFormat.Alignment = wdAlignParagraphJustify
End With


MsgBox "Just after 2nd Section Break." & vbCr & "Is Selection still in
place?" 'YES.

'This SpaceBefore/After routine produces no error but doesn't work.
For Each para In Selection.Sections(1).Range.Paragraphs
para.SpaceBefore = 12
para.SpaceAfter = 12
Next para

MsgBox "Is SpaceBefore changed in Section 1?" & vbCr & "Is Selection
intact?"
' NO SPACING. BUT SELECTION ok.

'This alternative SpaceBefore routine produces no error, but doesn't
work either.
'For Each para In ActiveDocument.Sections(2).Range.Paragraphs
' para.SpaceBefore = 12
' para.SpaceAfter = 12
'Next para

Selection.Sections(1).Range.Select ' Added.

'This routine works OK.
With Selection.PageSetup
.LeftMargin = 70
.RightMargin = 70

With .LineNumbering
.Active = True
.StartingNumber = 1
.CountBy = 1
.RestartMode = wdRestartSection
.DistanceFromText = wdAutoPosition
End With

End With

Selection.Collapse Direction:=wdCollapseEnd

End Sub
 
G

Graham Mayor

See http://word.mvps.org/FAQs/Formatting/CleanWebText.htm and
http://gregmaxey.mvps.org/Clean_Up_Text.htm

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>

Dear Experts,
I need to design a macro to process many old documents in the same
way. Unfortunately most of them contain hard paragraph formatting,
with two pilcrows. The User must manually select about 20 lines, and
the macro must change various aspects of formatting and remove the old
hard paras from the Selection. To enable line numbering, a section
needs to be created within which the paragraphs (now converted to
single-pilcrow) need to be reformatted with SpaceAfter (and/or
Before) to correct the spacing.

My attempt at the macro is shown below. Everything works except the
spacing between paragraphs. I have tried several routines gleaned from
Newsgroups and MS Help. Mostly they DO alter the spacing attribute
(to judge from the paragraph Format dialog), but the newly-formatted
text is not displayed with any space - remaining stubbornly UNSPACED.

I have also experimented with another macro containing only a
published Spacing Routine, but that didn't work either. The Document
is usually one page long, with no special formatting before the macro
is run.

All helpful suggestions will be gratefully received. Thank you.

Sub Macro16A()
'
' Attempt at Line Numbering a Selection and reformatting the Section
containing it.
' Macro created 15 July 2009 by Robert Patton
' Select all text to be numbered.

' Call with CTRL + ALT + [

Dim myRange As Range
Dim para As Paragraph

If Selection.Type = wdSelectionIP Then
MsgBox Prompt:="You haven't selected any text! Exiting
Procedure..."
Exit Sub
End If

Set myRange = ActiveDocument.Range(Start:=Selection.Start,
End:=Selection.End)

'This removes double-paragraphs in older documents. Works OK.
With Selection.Find
.ClearFormatting
.Text = vbCr & vbCr
.Replacement.ClearFormatting
.Replacement.Text = vbCr
.Execute Replace:=wdReplaceAll, Forward:=True, Wrap:=wdFindStop
End With

myRange.InsertBreak Type:=wdSectionBreakContinuous

With Selection
.Collapse Direction:=wdCollapseEnd
.InsertParagraphAfter ' ADDED.
.InsertBreak Type:=wdSectionBreakContinuous
.MoveUp Unit:=wdLine, Count:=8 ' Places IP within the Section.
.Sections(1).Range.Select
.ParagraphFormat.Alignment = wdAlignParagraphJustify
End With


MsgBox "Just after 2nd Section Break." & vbCr & "Is Selection still in
place?" 'YES.

'This SpaceBefore/After routine produces no error but doesn't work.
For Each para In Selection.Sections(1).Range.Paragraphs
para.SpaceBefore = 12
para.SpaceAfter = 12
Next para

MsgBox "Is SpaceBefore changed in Section 1?" & vbCr & "Is Selection
intact?"
' NO SPACING. BUT SELECTION ok.

'This alternative SpaceBefore routine produces no error, but doesn't
work either.
'For Each para In ActiveDocument.Sections(2).Range.Paragraphs
' para.SpaceBefore = 12
' para.SpaceAfter = 12
'Next para

Selection.Sections(1).Range.Select ' Added.

'This routine works OK.
With Selection.PageSetup
.LeftMargin = 70
.RightMargin = 70

With .LineNumbering
.Active = True
.StartingNumber = 1
.CountBy = 1
.RestartMode = wdRestartSection
.DistanceFromText = wdAutoPosition
End With

End With

Selection.Collapse Direction:=wdCollapseEnd

End Sub
 
P

Pesach Shelnitz

Hi Robert,

Your macro didn't set the paragraph spacing as you would have liked because
the part that replaced two paragraph breaks by a single paragraph break used

vbCr in the search instead of the Word code for a paragraph break in a
search, which is "^p" (as mentioned in the links that Graham sent you). As a
result,

Word did not count the paragraphs in the selection as paragraphs, treated
the whole selection as one paragraph, and did not insert the spacing between
what may have looked like paragraphs.

In the following version of your macro, I fixed the search and replace
strings to use ^p instead of vbCr and made some other small changes to use
the

myRange object more effectively.

Sub Macro16A()

Dim myRange As Range
Dim para As Paragraph

If Selection.Type = wdSelectionIP Then
MsgBox Prompt:="You haven't selected any text!" & _
"Exiting Procedure..."
Exit Sub
End If

Set myRange = ActiveDocument.Range(Start:=Selection.Start, _
End:=Selection.End)

'This removes double-paragraphs in older documents. Works OK.
With Selection.Find
.ClearFormatting
.Text = "^p^p"
.Replacement.ClearFormatting
.Replacement.Text = "^p"
.Execute Replace:=wdReplaceAll, Forward:=True, Wrap:=wdFindStop
End With

Selection.InsertBreak Type:=wdSectionBreakContinuous
myRange.Select
With Selection
.Collapse Direction:=wdCollapseEnd
.InsertParagraphAfter ' ADDED.
.InsertBreak Type:=wdSectionBreakContinuous
End With
myRange.ParagraphFormat.Alignment = wdAlignParagraphJustify

For Each para In ActiveDocument.Sections(2).Range.Paragraphs
para.SpaceBefore = 12
para.SpaceAfter = 12
Next para

ActiveDocument.Sections(2).Range.Select
With Selection.PageSetup
.LeftMargin = 70
.RightMargin = 70
With .LineNumbering
.Active = True
.StartingNumber = 1
.CountBy = 1
.RestartMode = wdRestartSection
.DistanceFromText = wdAutoPosition
End With
End With
Selection.Collapse Direction:=wdCollapseEnd
End Sub

--
Hope this helps,
Pesach Shelnitz


Graham Mayor said:
See http://word.mvps.org/FAQs/Formatting/CleanWebText.htm and
http://gregmaxey.mvps.org/Clean_Up_Text.htm

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>

Dear Experts,
I need to design a macro to process many old documents in the same
way. Unfortunately most of them contain hard paragraph formatting,
with two pilcrows. The User must manually select about 20 lines, and
the macro must change various aspects of formatting and remove the old
hard paras from the Selection. To enable line numbering, a section
needs to be created within which the paragraphs (now converted to
single-pilcrow) need to be reformatted with SpaceAfter (and/or
Before) to correct the spacing.

My attempt at the macro is shown below. Everything works except the
spacing between paragraphs. I have tried several routines gleaned from
Newsgroups and MS Help. Mostly they DO alter the spacing attribute
(to judge from the paragraph Format dialog), but the newly-formatted
text is not displayed with any space - remaining stubbornly UNSPACED.

I have also experimented with another macro containing only a
published Spacing Routine, but that didn't work either. The Document
is usually one page long, with no special formatting before the macro
is run.

All helpful suggestions will be gratefully received. Thank you.

Sub Macro16A()
'
' Attempt at Line Numbering a Selection and reformatting the Section
containing it.
' Macro created 15 July 2009 by Robert Patton
' Select all text to be numbered.

' Call with CTRL + ALT + [

Dim myRange As Range
Dim para As Paragraph

If Selection.Type = wdSelectionIP Then
MsgBox Prompt:="You haven't selected any text! Exiting
Procedure..."
Exit Sub
End If

Set myRange = ActiveDocument.Range(Start:=Selection.Start,
End:=Selection.End)

'This removes double-paragraphs in older documents. Works OK.
With Selection.Find
.ClearFormatting
.Text = vbCr & vbCr
.Replacement.ClearFormatting
.Replacement.Text = vbCr
.Execute Replace:=wdReplaceAll, Forward:=True, Wrap:=wdFindStop
End With

myRange.InsertBreak Type:=wdSectionBreakContinuous

With Selection
.Collapse Direction:=wdCollapseEnd
.InsertParagraphAfter ' ADDED.
.InsertBreak Type:=wdSectionBreakContinuous
.MoveUp Unit:=wdLine, Count:=8 ' Places IP within the Section.
.Sections(1).Range.Select
.ParagraphFormat.Alignment = wdAlignParagraphJustify
End With


MsgBox "Just after 2nd Section Break." & vbCr & "Is Selection still in
place?" 'YES.

'This SpaceBefore/After routine produces no error but doesn't work.
For Each para In Selection.Sections(1).Range.Paragraphs
para.SpaceBefore = 12
para.SpaceAfter = 12
Next para

MsgBox "Is SpaceBefore changed in Section 1?" & vbCr & "Is Selection
intact?"
' NO SPACING. BUT SELECTION ok.

'This alternative SpaceBefore routine produces no error, but doesn't
work either.
'For Each para In ActiveDocument.Sections(2).Range.Paragraphs
' para.SpaceBefore = 12
' para.SpaceAfter = 12
'Next para

Selection.Sections(1).Range.Select ' Added.

'This routine works OK.
With Selection.PageSetup
.LeftMargin = 70
.RightMargin = 70

With .LineNumbering
.Active = True
.StartingNumber = 1
.CountBy = 1
.RestartMode = wdRestartSection
.DistanceFromText = wdAutoPosition
End With

End With

Selection.Collapse Direction:=wdCollapseEnd

End Sub
 
R

Robert

Hello Pesach,
Thank you for a brilliant post! You not only fixed my coding but
gently explained the error in my thinking. I didn't suspect the Find
routine because actually it worked properly, but it had, as I now
know, a knock-on effect elsewhere.

Your work is in the best traditions of these Newsgroups and I am very
grateful.

The amended coding now runs perfectly on any document that I throw at
it and the result is very pleasing indeed. It is going to save me
many hours of frustrating work.

With sincere thanks,

Robert.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top