Results 1 to 10 of 10
  1. #1
    Lounger Splash's Avatar
    Join Date
    Aug 2002
    Location
    Sth Coast, New South Wales, Australia
    Posts
    47
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Find & Replace in multiple docs (Word 2k or XP)

    Hi all
    At my small office we have a number of templates which i have devised shared and saved for the workgroup
    They were all created from a similar base with fill-in fields and so on
    As such they all share some similar content

    I need to edit / replace this content in each of the templates in which it appears
    That's swap one line of text for a different one in each document

    I did a quick google search for a program to do find and replace over multiple documents and tried a couple
    The problem is that they corrupted the word documents so they would not load/open again
    They musn't save them in the proper word format

    Is there a way within word to do a find and repalce over multiple documents without doing it one by one?
    Either from within Word or with a separate program?

    I am sure others have had this small challenge before
    Thanks for your help
    NOBLEJOKER
    Sth Coast NSW Australia
    My Tech Help Site www.techhelpsite.com
    My Computer Club www.mulligrubs.miltonulladulla.com

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Find & Replace in multiple docs (Word 2k or XP)

    >> Is there a way within word to do a find and repalce over multiple documents without doing it one by one?

    The only way to do it is to open each document in turn and process it; there is no way to operate on a series of documents in one fell swoop. It is possible, however, to write VBA code to loop through all documents in a folder, and its subfolders if preferred.

    If you are interested, please provide some details:
    <UL><LI>Do you want to modify ALL documents/templates in a certain folder?
    <LI>If so, should documents/templates in subfolders be modified too?
    <LI>If not, how to determine which documents/templates should be modified?
    <LI>Is the text to be replaced always in the body of the document, or in a header/footer, or in a text box? Or is this variable?[/list]I may have forgotten some relevant aspects. Depending on the answers, the code will be more or less complicated.

  3. #3
    4 Star Lounger
    Join Date
    Aug 2003
    Location
    Stroud, United Kingdom
    Posts
    548
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Find & Replace in multiple docs (Word 2k or XP)

    Hi Hans!

    I was just going through a folder of documents and updating the table of contents settings one by one, and thought to myself, 'there must be a way of cycleing through these in VBA'.

    As i only had 10 to do i didnt bother trying to figure it out (kinda busy at the mo at work), but just saw this thread.

    Could you let me know the framework for doing this? Is is like

    For Each Document in CurrentFolder
    >>>
    Next

    or is it more complicated?

    hope its ok to ask
    Thanks,

    pmatz

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Find & Replace in multiple docs (Word 2k or XP)

    The idea is like this:

    <img src=/w3timages/blueline.gif width=33% height=2>

    Dim strPath As String
    Dim strFile As String
    Dim doc As Document

    ' Folder path - note the trailing backslash
    strPath = "C:MyDocs"
    ' Get first document name
    strFile = Dir(strPath & "*.doc")
    Do While Not (strFile = "")
    ' Open document
    Set doc = Documents.Open(strPath & strFile)
    ' do something with the document
    ...
    ' Close and save document
    doc.Close SaveChanges:=wdSaveChanges
    ' Get next document name
    strFile = Dir
    Loop

    ' Release object memory
    Set doc = Nothing

    <img src=/w3timages/blueline.gif width=33% height=2>

  5. #5
    4 Star Lounger
    Join Date
    Aug 2003
    Location
    Stroud, United Kingdom
    Posts
    548
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Find & Replace in multiple docs (Word 2k or XP)

    Thanks Hans! Thats great. I will try this later.
    Thanks,

    pmatz

  6. #6
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Find & Replace in multiple docs (Word 2k or XP)

    In addition to the framework Hans provided, if you search here, there was a thread for a guy whose company logo changed and he wanted to globally change this in a bunch of documents. (Not sure if it was a watermark or just a graphic in the header...) I think a detailed solution got posted for that one.

  7. #7
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Find & Replace in multiple docs (Word 2k or XP)

    You probably refer to the thread started by <!profile=bjoeng>bjoeng<!/profile> in <post#=274475>post 274475</post#>.

  8. #8
    Lounger Splash's Avatar
    Join Date
    Aug 2002
    Location
    Sth Coast, New South Wales, Australia
    Posts
    47
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Re: Find & Replace in multiple docs (Word 2k or XP)

    Thanks Hans V

    What i want to do with each doc in turn is find and replace a specific text string if it exists
    Eg search for "my big green pony" in each document and if found replace with "my little green horse" (well u get the idea)
    This should work even if the text to replace is not found
    That is, if not found it should do nothing and move on to the next file
    The text is always in the body of the document - not header footer watermark etc
    All the templates are saved in one directory - no sub directories

    I assume some additional code goes into your original post replacing "do something with the file"
    Any help would be appreciated

    thank you
    NOBLEJOKER
    Sth Coast NSW Australia
    My Tech Help Site www.techhelpsite.com
    My Computer Club www.mulligrubs.miltonulladulla.com

  9. #9
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Find & Replace in multiple docs (Word 2k or XP

    I have a Word97 Application Files.DOT which will process any set of files, e.g. All the (unlocked) templates in a folder or sub-tree.

    By default it will set up a "Process" macro; you can edit this or write your own.

    The Process will be applied to each file.

    Your Process (pseudocode) would read:
    <pre>do the find-and-replace in the open document/template
    ActiveDocument.Save
    ActiveDocument.Close.
    </pre>

    You can download a copy of a crude User Guide from here
    If you'd like to try a copy, let me know.

  10. #10
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Find & Replace in multiple docs (Word 2k or XP)

    Indeed, the code to find & replace should go where I put the ..., below the comment "do something with the document" in my reply to Matz. The macro recorder can generate the code you need; after a bit of cleaning up it looks like this:

    With doc.Range.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = "my big green pony"
    .Replacement.Text = "my little red herring"
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    .Execute Replace:=wdReplaceAll
    End With

    Substitute the correct strings, and insert the code into the code I posted earlier.

Posting Permissions

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