newbie needs help with 'REPLACE'

Z

z

Hi;I've searched web and group and can't find an answer to this
question, or if I did I don't recognize it as the answer.... I think
it's simple, but it's beyond my siimple "record a macro and edit it"
skill set.

Anyway, I've got a bunch of files to process. All want to do is copy
the top line of each file (which changes from file to file) plus a
terminating slash, and then replace every instance of a target string
(always the same in every file although the number of times the target
appears may be different ) with that copied text.

I recorded a macro which does fine, except that where I put the
replacement text into the dialog box using "paste", of course the
macro hardcodes that string, for instance "firstlineoffirstfile/"
verbatim into the VBA code, so when I run it on another file with,
say, a first line of "firstlineofsecondfile", it copies it nicely but
then replaces the target string with "firstlineoffirstfile/" which it
hardcoded when i recorded it on the first file.


so, simply enough I hope; where the recorded macro code has

Selection.Find.Execute
Selection.TypeParagraph
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.PasteAndFormat (wdPasteDefault)
Selection.TypeText Text:="/"
Selection.HomeKey Unit:=wdLine, Extend:=wdExtend
Selection.Copy
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "target"
.Replacement.Text = _
"firstlineofthisfile/"
.Forward = True
.Wrap = wdFindAsk
.Format = False
.MatchCase = True
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll

I just want it to replace "firstlineofthisfile/" with the current
contents of
the clipboard which got copied up in line 7


TIA!!!
 
D

Doug Robbins - Word MVP on news.microsoft.com

Use

Dim replacementtext As Range
Selection.HomeKey wdStory
Set replacementtext = ActiveDocument.Bookmarks("\line").Range
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "target"
.Replacement.Text = replacementtext.Text & "/"
.Forward = True
.Wrap = wdFindAsk
.Format = False
.MatchCase = True
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll

If you have to do this on a batch of files, see the article "Find &
ReplaceAll on a batch of documents in the same folder" at:

http://www.word.mvps.org/FAQs/MacrosVBA/BatchFR.htm

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 

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