Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / March 2007

Tip: Looking for answers? Try searching our database.

Text replacement

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
LEU - 17 Mar 2007 05:15 GMT
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
Greg Maxey - 17 Mar 2007 05:55 GMT
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

Signature

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

> 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
[quoted text clipped - 14 lines]
> .Execute Replace:=wdReplaceAll
> End With
Klaus Linke - 17 Mar 2007 08:38 GMT
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
LEU - 17 Mar 2007 16:56 GMT
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...

> You could also do 4 ReplaceAll (one for each paragraph style):
> ..Style = ActiveDocument.Styles(wdStyleHeading1)
[quoted text clipped - 11 lines]
> Regards,
> Klaus
Klaus Linke - 17 Mar 2007 18:35 GMT
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

> Thank you for the help, that worked.
>
[quoted text clipped - 50 lines]
>> Regards,
>> Klaus
LEU - 17 Mar 2007 21:42 GMT
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

> Your code looks just fine... Grasping for some criticism, maybe a Select
> Case would be easier to read:
[quoted text clipped - 63 lines]
> >> Regards,
> >> Klaus
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.