PDA

View Full Version : Search all execute formatting on all



AyeSee
12-20-2010, 03:47 PM
Hello hello,

I need to put subscript on all occurences of a specific word in my text. For example: Emc needs to become Emc (I cant seem to do it on this post, but basically the mc needs to become a subscript)

My current code searches and replaces only one instance in my text. How can I modify this to make it do it for all?

Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext, Name:="1"
Selection.GoTo What:=wdGoToLine, Name:="1"
With Selection.Find
.Text = "Pn-1 "
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.MoveRight Unit:=wdCharacter, Count:=3, Extend:=wdExtend
Selection.Font.Subscript = wdToggle

Many thanks in advance,
Alex

gmaxey
12-20-2010, 05:05 PM
Try:

Sub ScratchMacro()
Dim oRng As Word.Range
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "Emc"
Do While .Execute
oRng.MoveStart wdCharacter, 1
oRng.Font.Subscript = True
oRng.Collapse wdCollapseEnd
Loop
End With

End Sub

macropod
12-20-2010, 11:03 PM
Hi AyeSee,

Here's a different approach that might be useful if you've got multiple expressions to process and/or the subscript string requires a match with the rest of the word in which it appears:
Sub ApplySubscripts()
Dim StringArray As Variant, WordArray As Variant
Dim myRange As Range, oRngSrch As Range, i As Long
StringArray = Array("mc", "yz", "az")
WordArray = Array("Emc", "Xyz", "Lazy")
Set oRngSrch = ActiveDocument.Range
For i = 0 To UBound(StringArray)
Set myRange = oRngSrch.Duplicate
With myRange.Find
.Text = StringArray(i)
.MatchWholeWord = False
.MatchCase = True
While .Execute
With myRange
If .InRange(oRngSrch) And Trim(.Words(1)) _
= WordArray(i) Then .Font.Subscript = True
End With
Wend
End With
Next
End Sub

gmaxey
12-21-2010, 06:45 AM
Paul,

I like that approach and I have used both the .Duplicate and If Range.InRange methods before. I am not trying to be picky and I haven't tested in depth but in this particular case I don't see the need for either.

Of course it works but is there a reason for either in this case that I might have overlooked?

Sub ApplySubscripts()
Dim StringArray() As String, WordArray() As String
Dim myRange As Range, i As Long
StringArray = Split("mc,yz,az", ",")
WordArray = Split("Emc,Xyz,Lazy", ",")
For i = 0 To UBound(StringArray)
Set myRange = ActiveDocument.Range
With myRange.Find
.Text = StringArray(i)
.MatchWholeWord = False
.MatchCase = True
While .Execute
If Trim(myRange.Words(1)) = WordArray(i) Then myRange.Font.Subscript = True
Wend
End With
Next
End Sub


When creating lists of strings to add to an array using Split is easier for me.

AyeSee
12-21-2010, 07:20 AM
Hey guys,

Wow! Thanks for the excellent suggestions! I will take note of both options.

AyeSee
12-21-2010, 07:35 AM
Hey Greg,

I'm testing the second approach, and everything seems to be really clean, but I cannot seem to get numbers and characters. For example in the variable Pn-1 only the n seems to be recognised. Can I work around this? I'm guessing it has to do with the dim type...

Thanks in advance
Alex

gmaxey
12-21-2010, 02:41 PM
Alex,

Actually the problem is the "-" Word doesn't recognize "Pn-1" as a single word. It sees the word as just "Pn."

Using the following text:

Pn-1 Pn1 Pn-1 Pn1

Consider the following cde:
Sub ApplySubscripts()
Dim StringArray() As String, WordArray() As String
Dim myRange As Range, i As Long
StringArray = Split("n-1,n1", ",")
WordArray = Split("Pn,Pn1", ",")
For i = 0 To UBound(StringArray)
Set myRange = ActiveDocument.Range
With myRange.Find
.Text = StringArray(i)
.MatchWholeWord = False
.MatchCase = True
While .Execute
MsgBox Trim(myRange.Words(1))
If Trim(myRange.Words(1)) = WordArray(i) Then myRange.Font.Subscript = True
Wend
End With
Next
End Sub


As illustrated here you may be able to be creative with your WordArray construction and make things work.

macropod
12-21-2010, 03:33 PM
Paul,

I like that approach and I have used both the .Duplicate and If Range.InRange methods before. I am not trying to be picky and I haven't tested in depth but in this particular case I don't see the need for either.

Of course it works but is there a reason for either in this case that I might have overlooked?
Hi Greg,

There's probably no real need in this case. If, however, you used a Selection instead of ActiveDocument, things would be different.

gmaxey
12-21-2010, 03:52 PM
Paul,

Thanks.