Find and Replace

M

Maria

I have a scan of a pretty long document in unformatted
text which can be completed using only two styles: the
longer paragraphs are in a body text style (customized)
and at certain intervals between these longer paragraphs
are short sentences that need to be formatted as
headings.

I'm sure there must be a way to identify these short
sentences in the Find and Replace dialog box in order to
apply a heading. I tried my best to do it before writing
you, but each attempt changed more than was intended.

Would you please help? Thanks a million.
 
L

Larry

Hi Maria,

This macro will change all paragraphs from the cursor to the end of the
document that contain 100 characters or less to Heading 2 style. I
would place the cursor at the beginning of the text you want to search,
so as not to include any title material at the beginning of the document
that you don't want to be changed. If the 100 character limit doesn't
work for you, you can change it.

If you don't know how to install and run a macro and how to assign a key
or menu button to a macro, see this article:

http://www.mvps.org/word/FAQs/MacrosVBA/CreateAMacro.htm


Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Style = ActiveDocument.Styles("Heading 2")
With Selection.Find
.Text = "^13[!^13]{1,100}^13"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = True
.MatchWildcards = True
Do While .Execute
Selection.Collapse wdCollapseStart
Selection.MoveRight wdCharacter, 2
Selection.Paragraphs(1).Style = ("Heading 2")
Selection.MoveDown wdParagraph, 1
Selection.MoveLeft wdCharacter, 1
Loop
End With

Hope that helps,

Larry
 
L

Larry

Here's a little improvement. This version of the macro will return the
cursor to the starting point, instead of leaving the cursor at the point
where the last change was made.

Dim r As Range
Set r = Selection.Range
Application.ScreenUpdating = False
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Style = ActiveDocument.Styles("Heading 2")
With Selection.Find
.Text = "^13[!^13]{1,100}^13"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = True
.MatchWildcards = True
Do While .Execute
Selection.Collapse wdCollapseStart
Selection.MoveRight wdCharacter, 2
Selection.Paragraphs(1).Style = ("Heading 2")
Selection.MoveDown wdParagraph, 1
Selection.MoveLeft wdCharacter, 1
Loop
End With
r.Select



Larry said:
Hi Maria,

This macro will change all paragraphs from the cursor to the end of the
document that contain 100 characters or less to Heading 2 style. I
would place the cursor at the beginning of the text you want to search,
so as not to include any title material at the beginning of the document
that you don't want to be changed. If the 100 character limit doesn't
work for you, you can change it.

If you don't know how to install and run a macro and how to assign a key
or menu button to a macro, see this article:

http://www.mvps.org/word/FAQs/MacrosVBA/CreateAMacro.htm


Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Style = ActiveDocument.Styles("Heading 2")
With Selection.Find
.Text = "^13[!^13]{1,100}^13"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = True
.MatchWildcards = True
Do While .Execute
Selection.Collapse wdCollapseStart
Selection.MoveRight wdCharacter, 2
Selection.Paragraphs(1).Style = ("Heading 2")
Selection.MoveDown wdParagraph, 1
Selection.MoveLeft wdCharacter, 1
Loop
End With

Hope that helps,

Larry





Maria said:
I have a scan of a pretty long document in unformatted
text which can be completed using only two styles: the
longer paragraphs are in a body text style (customized)
and at certain intervals between these longer paragraphs
are short sentences that need to be formatted as
headings.

I'm sure there must be a way to identify these short
sentences in the Find and Replace dialog box in order to
apply a heading. I tried my best to do it before writing
you, but each attempt changed more than was intended.

Would you please help? Thanks a million.
 
M

Maria

Larry, thanks more than I can say! I intend to install
it first thing tomorrow (I'm brain dead now!) and I'll
let you know what happens. Thanks again, Larry, for
being there.
-----Original Message-----

Here's a little improvement. This version of the macro will return the
cursor to the starting point, instead of leaving the cursor at the point
where the last change was made.

Dim r As Range
Set r = Selection.Range
Application.ScreenUpdating = False
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Style = ActiveDocument.Styles ("Heading 2")
With Selection.Find
.Text = "^13[!^13]{1,100}^13"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = True
.MatchWildcards = True
Do While .Execute
Selection.Collapse wdCollapseStart
Selection.MoveRight wdCharacter, 2
Selection.Paragraphs(1).Style = ("Heading 2")
Selection.MoveDown wdParagraph, 1
Selection.MoveLeft wdCharacter, 1
Loop
End With
r.Select



Larry said:
Hi Maria,

This macro will change all paragraphs from the cursor
to the end of
the
document that contain 100 characters or less to Heading 2 style. I
would place the cursor at the beginning of the text
you want to
search,
so as not to include any title material at the
beginning of the
document
that you don't want to be changed. If the 100 character limit doesn't
work for you, you can change it.

If you don't know how to install and run a macro and
how to assign a
key
or menu button to a macro, see this article:

http://www.mvps.org/word/FAQs/MacrosVBA/CreateAMacro.htm


Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Style = ActiveDocument.Styles("Heading 2")
With Selection.Find
.Text = "^13[!^13]{1,100}^13"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = True
.MatchWildcards = True
Do While .Execute
Selection.Collapse wdCollapseStart
Selection.MoveRight wdCharacter, 2
Selection.Paragraphs(1).Style = ("Heading 2")
Selection.MoveDown wdParagraph, 1
Selection.MoveLeft wdCharacter, 1
Loop
End With

Hope that helps,

Larry





I have a scan of a pretty long document in unformatted
text which can be completed using only two styles: the
longer paragraphs are in a body text style (customized)
and at certain intervals between these longer paragraphs
are short sentences that need to be formatted as
headings.

I'm sure there must be a way to identify these short
sentences in the Find and Replace dialog box in order to
apply a heading. I tried my best to do it before writing
you, but each attempt changed more than was intended.

Would you please help? Thanks a million.


.
 
S

Suzanne S. Barnhill

You might try AutoFormat (with automatic headings enabled); it deals with
this sort of situation pretty well.

--
Suzanne S. Barnhill
Microsoft MVP (Word)
Words into Type
Fairhope, Alabama USA

Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
M

Maria

Thanks, Suzanne, for your usual great tip!
-----Original Message-----
You might try AutoFormat (with automatic headings enabled); it deals with
this sort of situation pretty well.

--
Suzanne S. Barnhill
Microsoft MVP (Word)
Words into Type
Fairhope, Alabama USA

Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.



.
 
L

Larry

I never tried that before, so I tried it now. The potential headings
have to be just so, in order to get the heading styles formatting
applied to them.
 
G

Greg

Larry,

I added a couple of bells and whistles :)

Sub ApplyStyleToShortParas()
Dim r As Range
Dim Style As String
Dim FindString As String
Dim Char As Long
Set r = Selection.Range
Char = InputBox("What is the maximum paragraph length in
characters that you wish to reformat?", "Paragraph
Length", 100)
FindString = "^13[!^13]{1," & Char & "}^13"
Style = InputBox("Type in the style name to apply e.g.
Heading 1", Style)
Application.ScreenUpdating = False
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Style = ActiveDocument.Styles
(Style)
With Selection.Find
.Text = FindString
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = True
.MatchWildcards = True
Do While .Execute
Selection.Collapse wdCollapseStart
Selection.MoveRight wdCharacter, 2
Selection.Paragraphs(1).Style = (Style)
Selection.MoveDown wdParagraph, 1
Selection.MoveLeft wdCharacter, 1
Loop
End With
r.Select
-----Original Message-----

Here's a little improvement. This version of the macro will return the
cursor to the starting point, instead of leaving the cursor at the point
where the last change was made.

Dim r As Range
Set r = Selection.Range
Application.ScreenUpdating = False
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Style = ActiveDocument.Styles ("Heading 2")
With Selection.Find
.Text = "^13[!^13]{1,100}^13"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = True
.MatchWildcards = True
Do While .Execute
Selection.Collapse wdCollapseStart
Selection.MoveRight wdCharacter, 2
Selection.Paragraphs(1).Style = ("Heading 2")
Selection.MoveDown wdParagraph, 1
Selection.MoveLeft wdCharacter, 1
Loop
End With
r.Select



Larry said:
Hi Maria,

This macro will change all paragraphs from the cursor
to the end of
the
document that contain 100 characters or less to Heading 2 style. I
would place the cursor at the beginning of the text you
want to
search,
so as not to include any title material at the
beginning of the
document
that you don't want to be changed. If the 100 character limit doesn't
work for you, you can change it.

If you don't know how to install and run a macro and
how to assign a
key
or menu button to a macro, see this article:

http://www.mvps.org/word/FAQs/MacrosVBA/CreateAMacro.htm


Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Style = ActiveDocument.Styles ("Heading 2")
With Selection.Find
.Text = "^13[!^13]{1,100}^13"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = True
.MatchWildcards = True
Do While .Execute
Selection.Collapse wdCollapseStart
Selection.MoveRight wdCharacter, 2
Selection.Paragraphs(1).Style = ("Heading 2")
Selection.MoveDown wdParagraph, 1
Selection.MoveLeft wdCharacter, 1
Loop
End With

Hope that helps,

Larry





I have a scan of a pretty long document in unformatted
text which can be completed using only two styles: the
longer paragraphs are in a body text style (customized)
and at certain intervals between these longer paragraphs
are short sentences that need to be formatted as
headings.

I'm sure there must be a way to identify these short
sentences in the Find and Replace dialog box in order to
apply a heading. I tried my best to do it before writing
you, but each attempt changed more than was intended.

Would you please help? Thanks a million.


.
 
L

Larry

Ok, but those lines for the input boxes have to be fixed:

Dim r As Range
Dim Style As String
Dim FindString As String
Dim Char As Long
Set r = Selection.Range
Char = InputBox("What is the maximum paragraph length in characters " &
vbCr & _
"that you wish to reformat?", "Paragraph Length ", 100)
FindString = "^13[!^13]{1," & Char & "}^13"
Style = InputBox("Type in the style name to apply e.g. Heading 1",
"Heading Style")
Application.ScreenUpdating = False
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Style = ActiveDocument.Styles(Style)
With Selection.Find
.Text = FindString
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = True
.MatchWildcards = True
Do While .Execute
Selection.Collapse wdCollapseStart
Selection.MoveRight wdCharacter, 2
Selection.Paragraphs(1).Style = (Style)
Selection.MoveDown wdParagraph, 1
Selection.MoveLeft wdCharacter, 1
Loop
End With
r.Select

Larry,

I added a couple of bells and whistles :)

Sub ApplyStyleToShortParas()
Dim r As Range
Dim Style As String
Dim FindString As String
Dim Char As Long
Set r = Selection.Range
Char = InputBox("What is the maximum paragraph length in
characters that you wish to reformat?", "Paragraph
Length", 100)
FindString = "^13[!^13]{1," & Char & "}^13"
Style = InputBox("Type in the style name to apply e.g.
Heading 1", Style)
Application.ScreenUpdating = False
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Style = ActiveDocument.Styles
(Style)
With Selection.Find
.Text = FindString
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = True
.MatchWildcards = True
Do While .Execute
Selection.Collapse wdCollapseStart
Selection.MoveRight wdCharacter, 2
Selection.Paragraphs(1).Style = (Style)
Selection.MoveDown wdParagraph, 1
Selection.MoveLeft wdCharacter, 1
Loop
End With
r.Select
-----Original Message-----

Here's a little improvement. This version of the macro will return the
cursor to the starting point, instead of leaving the cursor at the point
where the last change was made.

Dim r As Range
Set r = Selection.Range
Application.ScreenUpdating = False
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Style = ActiveDocument.Styles ("Heading 2")
With Selection.Find
.Text = "^13[!^13]{1,100}^13"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = True
.MatchWildcards = True
Do While .Execute
Selection.Collapse wdCollapseStart
Selection.MoveRight wdCharacter, 2
Selection.Paragraphs(1).Style = ("Heading 2")
Selection.MoveDown wdParagraph, 1
Selection.MoveLeft wdCharacter, 1
Loop
End With
r.Select



Larry said:
Hi Maria,

This macro will change all paragraphs from the cursor
to the end of
the
document that contain 100 characters or less to Heading 2 style. I
would place the cursor at the beginning of the text you
want to
search,
so as not to include any title material at the
beginning of the
document
that you don't want to be changed. If the 100 character limit doesn't
work for you, you can change it.

If you don't know how to install and run a macro and
how to assign a
key
or menu button to a macro, see this article:

http://www.mvps.org/word/FAQs/MacrosVBA/CreateAMacro.htm


Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Style = ActiveDocument.Styles ("Heading 2")
With Selection.Find
.Text = "^13[!^13]{1,100}^13"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = True
.MatchWildcards = True
Do While .Execute
Selection.Collapse wdCollapseStart
Selection.MoveRight wdCharacter, 2
Selection.Paragraphs(1).Style = ("Heading 2")
Selection.MoveDown wdParagraph, 1
Selection.MoveLeft wdCharacter, 1
Loop
End With

Hope that helps,

Larry





I have a scan of a pretty long document in unformatted
text which can be completed using only two styles: the
longer paragraphs are in a body text style (customized)
and at certain intervals between these longer paragraphs
are short sentences that need to be formatted as
headings.

I'm sure there must be a way to identify these short
sentences in the Find and Replace dialog box in order to
apply a heading. I tried my best to do it before writing
you, but each attempt changed more than was intended.

Would you please help? Thanks a million.


.
 

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

Similar Threads

Find and Replace 2
Find and Replace 3
Using styles 3
Headins and Table of Contents 1
Heading and Paragraph Numbering 1
Replacing styles?? 5
Using find/replace to insert file? 11
Can I create my own heading style? 6

Top