PDA

View Full Version : search/replace script



Michael Hanel
01-28-2009, 05:02 PM
I have a Word document I'm working with and I'm trying to line up footnotes. The text I has notes marked "[Note]" and then I have another document with the footnotes in order. What I want to do is to do a search/replace so that each time a hit ([Note]) is found an replaced it is replaced as [Note x+1], so that it goes from
[Note]
[Note]
[Note]
[Note]

to

[Note 1]
[Note 2]
[Note 3]
[Note 4]

Anyone know how to write a quick script for something like that?

SCSaunders
02-09-2009, 08:49 AM
....ciao pasquale

in my pc i have found this macro to merge two documents with foornotes (but i don't remember how it works!!!):


option explicit
dim arr()
dim location as long
dim ends as long, k as long
sub macro1() 'find where indexes start to repeat at end of document
'insert a bookmark at this location
dim first as string
selection.homekey unit:=wdstory
with selection.find
.text = "\[*\]"
.matchwildcards = true
.execute
end with
first = selection
selection.moveright unit:=wdcharacter, count:=1
with selection.find
.text = first
.matchwildcards = false
.execute
end with
selection.homekey unit:=wdline
selection.typeparagraph
selection.typeparagraph
selection.moveup unit:=wdline, count:=2
location = selection.end
ends = paras(selection)
with activedocument.bookmarks
.add range:=selection.range, name:="xxx"
.defaultsorting = wdsortbyname
.showhidden = false
end with
selection.homekey unit:=wdstory
macro2
macro3
macro4
end sub
sub macro2()
'check for indexes and record paragraph number
'and index in array. Exit at bookmark location
dim first as string, last as string
dim rg as range
dim i as long

redim arr(2, 500)
selection.homekey unit:=wdstory
first = ""
do
with selection.find
.text = "\[*\]"
.matchwildcards = true
.forward = true
.wrap = false
.execute
if first = "" then first = selection
last = selection
if selection.end > location then exit do
i = i + 1
arr(0, i) = paras(selection)
arr(1, i) = selection
end with
loop
k = i
redim preserve arr(2, i)
end sub
sub macro3()
'read footnotes into array
dim r as range
dim i as long, j as long
on error goto exits
selection.goto what:=wdgotobookmark, name:="xxx"
do
with selection.find
.text = "\[*\]"
.matchwildcards = true
.forward = true
.wrap = false
.execute
'if selection.end > location then exit do
i = i + 1
'debug.print selection
set r = activedocument.paragraphs(paras(selection)).range
arr(2, i) = r.text
end with
loop
exits:
For j = 1 to i - 1
debug.print arr(0, j) & " - " & arr(1, j) & " - " & arr(2, j)
next
end sub
sub macro4()
dim i as long, j as long
dim strt as boolean
j = 1
for i = ends to 1 step -1
activedocument.paragraphs(i + 1).range.select
selection.collapse direction:=wdcollapseend
j = 1
for j = 1 to k - 1
if arr(0, j) = i then
if strt = true then
selection.typeparagraph
strt = false
end if
selection.typetext arr(2, j)
end if
next
selection.typetext "__________________________________________________ _____"
selection.typeparagraph
next
end sub
function paras(selection)
'return selection paragraph number
dim myrange1 as range set myrange1 = activedocument.range(start:=0, end:=0)
myrange1.setrange start:=myrange1.start, _
end:=selection.range.end
paras = myrange1.paragraphs.count
end functionWow! I'm in total awe.

Hey Pasquale, I don't know how you get all this stuff, but amazing it is.

I think this is VBA. (If not, stop here and go get a java.) I tried to learn VBA a little bit more so that I could better control some of the on-screen interactive doodads & doo-hickeys I wanted to do during my teaching with the "Developer" tab stuff in PowerPoint.

Last time I code-tweaked a "combox box," everytime I entered text my neighbor's upstairs toilet flushed. :mad:

Anyways, good job on all this code stuff, IMO it's hard and makes way less sense than Greek and Hebrew.