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
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