Text replacement

L

LEU

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
 
G

Greg Maxey

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
 
K

Klaus Linke

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
 
L

LEU

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

Klaus Linke

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
 
L

LEU

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
 

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