Log in

View Full Version : Solved: Find/Replace preformed only on text selected before macro is run



saban
06-19-2008, 03:50 PM
Can someone point me how to preform find replace only on text i selected before running the macro

Thnx

Crooz
06-20-2008, 02:47 AM
Hi Saban,
Select the text that has to be searched.
In the Find and Replace dialog, click on More (next to Find Next).
In the Search Options, choose the direction Down.
Now Find and Replace will only search the selected area - but you will be asked if you want to search the rest of the document - and here of course you'll choose NO.
Hope this helps.

fumei
06-20-2008, 09:20 AM
As the question is regarding a macro, I assume you want to do it by code.

"on text i selected "

Selected text is...Selection. So:
With Selection.Find

saban
06-21-2008, 06:34 AM
So how do i then modify this macro to remove spaces only in selected text and also just in footnotes that are in selected text

Public Sub ReplaceMultipleSpaces()
Selection.HomeKey wdStory
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
'Here is where it is actually looking for spaces between words
.Text = " [ ]@([! ])"
'This line tells it to replace the excessive spaces with one space
.Replacement.Text = " \1"
.MatchWildcards = True
.Wrap = wdFindStop
.Format = False
.Forward = True
'execute the replace
.Execute Replace:=wdReplaceAll
End With
Dim objFtn As Footnote
Selection.HomeKey wdStory
Dim rngFoot As Footnote
Dim rngTmp As Range
For Each rngFoot In ActiveDocument.Footnotes
Set rngTmp = rngFoot.Range
'rngTmp.Select
With rngTmp.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "^w" ' .Text = " [ ]@([! ])"
.Replacement.Text = " " '.Replacement.Text = " \1"
' .MatchWildcards = True
.Wrap = wdFindStop
.Format = False
.Forward = True
.Execute Replace:=wdReplaceAll
End With
Next
End Sub

saban
06-21-2008, 08:25 AM
this one goes also thrugh footnotes that are not in selection any ideas why
Dim objFtn As Footnote
'Selection.HomeKey wdStory
Dim rngFoot As Footnote
Dim rngTmp As Range
Dim sel As Selection
Set sel = Selection
For Each rngFoot In sel.Footnotes
Set rngTmp = rngFoot.Range
With rngTmp.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "^w" ' .Text = " [ ]@([! ])"
.Replacement.Text = " " '.Replacement.Text = " \1"
' .MatchWildcards = True
.Wrap = wdFindStop
.Format = False
.Forward = True
.Execute Replace:=wdReplaceAll
End With
Next rngFoot

MOS MASTER
06-22-2008, 02:25 PM
Saban,

I presume this is the same question as:
http://vbaexpress.com/forum/showthread.php?t=20334

Therefore I'll mark this one solved.