Search and Replace Question

N

Nick Transier

When I reach a caption text under a picture in my document, I copy the
picture, paste it into a new document, and then rename the new document with
the caption text. In order to do this I have to find and replace any illegal
characters (for save purposes) in the caption text eg ":"

First Question: How do I alter the selection text for the purpose of
formatting a file name without actually changing the text in the document.
For example, I select a caption: "Figure 1: Example Caption" and I want to
find/replace so that the file name is "Figure 1.Example Caption", but I do
not want the actual word document to change. I have a similar problem with
converting a table to text for export, but then the table has been changed
in the actual document which I do not want.

Second question: Can I search for several things at once and replace with
one character or do I need to repeat the following statement for everything:

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = ":" <------------- i.e. can I
put several selections here?
.Replacement.Text = "."
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
 
H

Helmut Weber

Hi Nick,
one very simple way would be,
to copy the selection, do the replacements
and paste what was copied afterwards.
For searching for either one of several characters
and replacing see:
http://word.mvps.org/FAQs/General/UsingWildcards.htm
In this example the search is for < or > or /
As < and > have a special meaning here,
they have to be preceeded by \
Sub Test556()
Resetsearch
Selection.Copy
Dim orng As Range
Set orng = Selection.Range
With orng.Find
.Text = "[\<\>/]"
.Replacement.Text = "_"
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
MsgBox orng.Text ' your clean filename
End With
Selection.Paste ' restore the text
Resetsearch
End Sub
'---
Sub Resetsearch()
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute
End With
End Sub
 
N

Nick Transier

Thanks for the help! Your code accomplishes what I needed with one problem
though. Once I paste the selection back in, it looses track of what "para" I
am on and then my whole loop stops working. Recall that I am looping through
a documents with an outer loops that executes:
For each para in Activedocument.Paragraphs
If something then
'do something
elseif something then
'do something
else
'do something else
end if
next para

Any ideas?

Thanks- Nick
 
H

Helmut Weber

Hi Nick,
in case you select whole paragraphs,
you might exclude the paragraph mark from the selection,
so that the paragraphs collection remains untouched.
Sometimes I prefer a loop of kind "for i = 1 to x",
where x would be activedocument.paragraphs.count, here,
to obtain additional information on which paragraph
is beeing processed.
HTH
Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 

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