Results 1 to 2 of 2

Thread: search/replace script

  1. #1
    Join Date
    Apr 2004
    Posts
    2,093

    Default search/replace script

    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?
    Michael Hanel
    PhD candidate Classics Univ. of Cincinnati
    MDiv Concordia Seminary
    MA Classics Washington University
    Unofficial BibleWorks Blog
    LibraryThing!

  2. #2

    Default

    Quote Originally Posted by pasquale View Post
    ....ciao pasquale

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

    PHP Code:
    option explicit 
    dim arr
    () 
    dim location as long 
    dim ends 
    as longas 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:=wdcharactercount:=
        with selection
    .find 
            
    .text first 
            
    .matchwildcards false 
            
    .execute 
        end with 
        selection
    .homekey unit:=wdline 
        selection
    .typeparagraph 
        selection
    .typeparagraph 
        selection
    .moveup unit:=wdlinecount:=
        location 
    selection.end 
        ends 
    paras(selection
        
    with activedocument.bookmarks 
            
    .add range:=selection.rangename:="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 stringlast as string 
        dim rg 
    as range 
        dim i 
    as long 
         
        redim arr
    (2500
        
    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 
                

                arr
    (0i) = paras(selection
                
    arr(1i) = selection 
            end with 
        loop 
        k 

        redim preserve arr
    (2i
    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
                

                 
    '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:=0end:=0
        
    myrange1.setrange start:=myrange1.start
        end
    :=selection.range.end 
        paras 
    myrange1.paragraphs.count 
    end 
    function 
    Wow! 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.

    Anyways, good job on all this code stuff, IMO it's hard and makes way less sense than Greek and Hebrew.
    Last edited by SCSaunders; 02-09-2009 at 07:51 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •