Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Replace small words with Caps

Status
Not open for further replies.

W1ordvba

Technical User
Dec 12, 2011
7
GB
Hi

What I want to do is change Lecture to LECTURE.
What do I need to modify the macro below.
However, I only want to do this when Lecture is the only word on a line NOT when it is part of a sentence.

e.g.

Change all instances of Lecture to LECTURE when

line is : Lecture

and NOT:

A Maths Lecture starts at 9am.

Thx in advance.


Sub ChangeWords()

Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting

MyString = "Lecture"
StringLength = Len(MyString)

With Selection.Find
.Text = MyString
.Replacement.Text = "LECTURE"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = True
.MatchWholeWord = True 'False
.MatchWildcards = True
.MatchSoundsLike = False
.MatchAllWordForms = False
End With

Selection.Find.Execute Replace:=wdReplaceAll

End Sub
 


Hi,

You might try using the Paragraphs collection. You can lookp thru all the paragraphs in the document. You will have to test, because a paragraph with just Lecture, contains more than 7 characters. Use the Watch Window or Debug.Print.
Code:
Dim par As Paragraph

For Each par In ThisDocument.Paragraphs
  With par
     Debug.Print .Range.Text
  End With
Next



Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 


Try this that does the replace...
Code:
Sub test()
    Dim iPar As Integer
    
    For iPar = 1 To ThisDocument.Paragraphs.Count
        With ThisDocument.Paragraphs(iPar).Range
            If Len(.Text) = 8 And LCase(Left(.Text, 7)) = "lecture" Then
                .Text = UCase(.Text)
            End If
        End With
    Next
End Sub

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Thx Skip. That worked a treat.

However, if the document is massive i.e. over 100 pages, the process might hang. IS there another way to do it?
 
What about this ?
Code:
  Selection.HomeKey Unit:=wdStory
  With Selection.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = "Lecture^p"
    .Replacement.Text = "LECTURE^p"
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = True
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    .Execute Replace:=wdReplaceAll
  End With

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
OOps:
.Text = "[!]^p[/!]Lecture^p"
.Replacement.Text = "[!]^p[/!]LECTURE^p"

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
You don't even need a macro for this!

Per PHV's 'oops', a simple Find/Replace will do it, where:
Find = ^pLecture^p
Replace = ^pLECTURE^p
with the 'match case' option checked.

Cheers
Paul Edstein
[MS MVP - Word]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top