I have the following macro that finds the word â??Orderâ?? and changes it to all
caps and bold in the whole document. How do I change it to just replace it in
my ActiveDocument.Styles "Heading 1", "Heading 2", "Heading 3" and "Heading
4"?

Dim SearchRange As Range
Set SearchRange = ActiveDocument.Range
With SearchRange.Find
.ClearFormatting
.Replacement.ClearFormatting
.Forward = True
.Format = True
.Wrap = wdFindContinue
.Replacement.Font.Bold = True
.Text = "Order"
.Replacement.Text = "ORDER"
.Execute Replace:=wdReplaceAll
End With

Re: Text replacement by Greg

Greg
Fri Mar 16 23:55:56 CDT 2007

LEU,

AFAIK, you can't use a Replacement and wdReplaceAll for this purpose. You
need to find, evaluate and as appropriate manipulate the found range using a
While .Execute statement:

Sub Test()
Dim SearchRange As Range
Set SearchRange = ActiveDocument.Range
With SearchRange.Find
.Text = "Order"
While .Execute
Select Case SearchRange.Style
Case "Heading 1", "Heading 2", "Heading 3", "Heading 4"
SearchRange.Font.AllCaps = True
SearchRange.Font.Bold = True
Case Else
'Do Nothing
End Select
Wend
End With
End Sub


--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.


LEU wrote:
> I have the following macro that finds the word 'Order' and changes it
> to all caps and bold in the whole document. How do I change it to
> just replace it in my ActiveDocument.Styles "Heading 1", "Heading 2",
> "Heading 3" and "Heading 4"?
>
> Dim SearchRange As Range
> Set SearchRange = ActiveDocument.Range
> With SearchRange.Find
> .ClearFormatting
> .Replacement.ClearFormatting
> .Forward = True
> .Format = True
> .Wrap = wdFindContinue
> .Replacement.Font.Bold = True
> .Text = "Order"
> .Replacement.Text = "ORDER"
> .Execute Replace:=wdReplaceAll
> End With



Re: Text replacement by Klaus

Klaus
Sat Mar 17 02:38:37 CDT 2007

You could also do 4 ReplaceAll (one for each paragraph style):
.Style = ActiveDocument.Styles(wdStyleHeading1)
It probably isn't much slower (if at all).

Maybe also better use
.MatchWholeWord = True

BTW, I agree very much with Greg that it's better to apply Format > Font >
All caps, than to actually change the text.
Often, you later decide you don't want to use "All caps" for emphasis, or
that you want to use bold/italic/... instead, but if you changed the actual
text, you need to proof-read and fix the whole text manually.

Regards,
Klaus



Re: Text replacement by LEU

LEU
Sat Mar 17 10:56:03 CDT 2007

Thank you for the help, that worked.

Can I ask a related â??Headingâ?? question? I have the following macro that lets
me inserts a $ at the beginning of a Heading if my cursor is in that Heading.
This macro works fine, but is there a better way to write it?.

Dim textbox As Shape
If Selection.Style = ActiveDocument.Styles("Heading 1") Then
With ActiveDocument.Shapes.AddTextbox(msoTextOrientationHorizontal, 35, _
Selection.Information(wdVerticalPositionRelativeToPage) - 5, 22, 24)
With .TextFrame.TextRange
.Text = "$"
.Font.Size = 14
End With
.Fill.Visible = msoFalse
.Line.Visible = msoFalse
End With
Else
If Selection.Style = ActiveDocument.Styles("Heading 2") Then
With ActiveDocument.Shapes.AddTextbox(msoTextOrientationHorizontal, 35, _
Selection.Information(wdVerticalPositionRelativeToPage) - 5, 22, 24)
With .TextFrame.TextRange
.Text = "$"
.Font.Size = 14
End With
.Fill.Visible = msoFalse
.Line.Visible = msoFalse
End With
Else
If Selection.Style = ActiveDocument.Styles("Heading 3") Then

â??and so on...


"Klaus Linke" wrote:

> You could also do 4 ReplaceAll (one for each paragraph style):
> ..Style = ActiveDocument.Styles(wdStyleHeading1)
> It probably isn't much slower (if at all).
>
> Maybe also better use
> ..MatchWholeWord = True
>
> BTW, I agree very much with Greg that it's better to apply Format > Font >
> All caps, than to actually change the text.
> Often, you later decide you don't want to use "All caps" for emphasis, or
> that you want to use bold/italic/... instead, but if you changed the actual
> text, you need to proof-read and fix the whole text manually.
>
> Regards,
> Klaus
>
>
>

Re: Text replacement by Klaus

Klaus
Sat Mar 17 12:35:42 CDT 2007

Your code looks just fine... Grasping for some criticism, maybe a Select
Case would be easier to read:

Select Case Selection.Style
Case ActiveDocument.Styles(wdStyleHeading1),
ActiveDocument.Styles(wdStyleHeading1)
' ...
Case ActiveDocument.Styles(wdStyleHeading3)
' ...

Regards,
Klaus




"LEU" wrote:
> Thank you for the help, that worked.
>
> Can I ask a related "Heading" question? I have the following macro that
> lets
> me inserts a $ at the beginning of a Heading if my cursor is in that
> Heading.
> This macro works fine, but is there a better way to write it?.
>
> Dim textbox As Shape
> If Selection.Style = ActiveDocument.Styles("Heading 1") Then
> With ActiveDocument.Shapes.AddTextbox(msoTextOrientationHorizontal, 35,
> _
> Selection.Information(wdVerticalPositionRelativeToPage) - 5, 22, 24)
> With .TextFrame.TextRange
> .Text = "$"
> .Font.Size = 14
> End With
> .Fill.Visible = msoFalse
> .Line.Visible = msoFalse
> End With
> Else
> If Selection.Style = ActiveDocument.Styles("Heading 2") Then
> With ActiveDocument.Shapes.AddTextbox(msoTextOrientationHorizontal, 35,
> _
> Selection.Information(wdVerticalPositionRelativeToPage) - 5, 22, 24)
> With .TextFrame.TextRange
> .Text = "$"
> .Font.Size = 14
> End With
> .Fill.Visible = msoFalse
> .Line.Visible = msoFalse
> End With
> Else
> If Selection.Style = ActiveDocument.Styles("Heading 3") Then
>
> 'and so on...
>
>
> "Klaus Linke" wrote:
>
>> You could also do 4 ReplaceAll (one for each paragraph style):
>> ..Style = ActiveDocument.Styles(wdStyleHeading1)
>> It probably isn't much slower (if at all).
>>
>> Maybe also better use
>> ..MatchWholeWord = True
>>
>> BTW, I agree very much with Greg that it's better to apply Format > Font
>> >
>> All caps, than to actually change the text.
>> Often, you later decide you don't want to use "All caps" for emphasis, or
>> that you want to use bold/italic/... instead, but if you changed the
>> actual
>> text, you need to proof-read and fix the whole text manually.
>>
>> Regards,
>> Klaus
>>
>>
>>



Re: Text replacement by LEU

LEU
Sat Mar 17 15:42:00 CDT 2007

Thank you for the input Klaus. I'm new to writing macros and sense I don't
know all the VB language I sometimes feel Iâ??m taking the long way to get a
macro written.

Thanks again.
LEU



"Klaus Linke" wrote:

> Your code looks just fine... Grasping for some criticism, maybe a Select
> Case would be easier to read:
>
> Select Case Selection.Style
> Case ActiveDocument.Styles(wdStyleHeading1),
> ActiveDocument.Styles(wdStyleHeading1)
> ' ...
> Case ActiveDocument.Styles(wdStyleHeading3)
> ' ...
>
> Regards,
> Klaus
>
>
>
>
> "LEU" wrote:
> > Thank you for the help, that worked.
> >
> > Can I ask a related "Heading" question? I have the following macro that
> > lets
> > me inserts a $ at the beginning of a Heading if my cursor is in that
> > Heading.
> > This macro works fine, but is there a better way to write it?.
> >
> > Dim textbox As Shape
> > If Selection.Style = ActiveDocument.Styles("Heading 1") Then
> > With ActiveDocument.Shapes.AddTextbox(msoTextOrientationHorizontal, 35,
> > _
> > Selection.Information(wdVerticalPositionRelativeToPage) - 5, 22, 24)
> > With .TextFrame.TextRange
> > .Text = "$"
> > .Font.Size = 14
> > End With
> > .Fill.Visible = msoFalse
> > .Line.Visible = msoFalse
> > End With
> > Else
> > If Selection.Style = ActiveDocument.Styles("Heading 2") Then
> > With ActiveDocument.Shapes.AddTextbox(msoTextOrientationHorizontal, 35,
> > _
> > Selection.Information(wdVerticalPositionRelativeToPage) - 5, 22, 24)
> > With .TextFrame.TextRange
> > .Text = "$"
> > .Font.Size = 14
> > End With
> > .Fill.Visible = msoFalse
> > .Line.Visible = msoFalse
> > End With
> > Else
> > If Selection.Style = ActiveDocument.Styles("Heading 3") Then
> >
> > 'and so on...
> >
> >
> > "Klaus Linke" wrote:
> >
> >> You could also do 4 ReplaceAll (one for each paragraph style):
> >> ..Style = ActiveDocument.Styles(wdStyleHeading1)
> >> It probably isn't much slower (if at all).
> >>
> >> Maybe also better use
> >> ..MatchWholeWord = True
> >>
> >> BTW, I agree very much with Greg that it's better to apply Format > Font
> >> >
> >> All caps, than to actually change the text.
> >> Often, you later decide you don't want to use "All caps" for emphasis, or
> >> that you want to use bold/italic/... instead, but if you changed the
> >> actual
> >> text, you need to proof-read and fix the whole text manually.
> >>
> >> Regards,
> >> Klaus
> >>
> >>
> >>
>
>
>