PDA

View Full Version : Solved: Find and Replace limited to a Range



Adamski
06-11-2009, 10:52 AM
I susspect theis has been asked before, but if it has I can't find it.

How do I limit a find and replace to a specific range?

I am dumping records from a database to word with that code returning a range for each record. I want to replace all paragraph marks (^p) in this range with linefeeds (^l). This is so each record is a single paragraph and my numbering style works.

Thanks

Oorang
06-11-2009, 12:08 PM
Are you doing the dump manually? It might be less painful to make the word doc query the database, and remove the breaks in the SQL.

Adamski
06-12-2009, 03:24 AM
I am running code in the database applications (DOORS) scriting language (DOORS DXL). I use application automation to open word and do a bunch of stuff from there.

I am not actually running vba code in word, but it is easy enough to translate it to DXL. So in DXL I have a function to write things in word, and this function returns a word range object of the stuff it put in. I want to limit the find/replace to this range.

Basically, all I require is a vba function which takes a Range object as an argument and does a find replace only in the range. I will be able to translate it to DXL to add to my existing exporter easily.

Adamski
06-12-2009, 03:37 AM
Found how to do it now. Test code below. I alway thought wdReplaceAll would replace all in the document...



Sub replacerange()
Set myRng = Selection
With myRng.Find
.Text = "^p"
.Replacement.Text = "^l"
.Forward = True
.Format = True
.Wrap = wdFindStop
.Execute Replace:=wdReplaceAll
End With
End Sub