Consulting

Page 1 of 2 1 2 LastLast
Results 1 to 20 of 22

Thread: Solved: find and replace in all documents

  1. #1
    Moderator VBAX Master austenr's Avatar
    Joined
    Sep 2004
    Location
    Maine
    Posts
    2,033
    Location

    Solved: find and replace in all documents

    I need to find a way to find the word "student" and replace all occurances with "client"in all documents inside a folder. Is there a way to do this? Never wrote a Word macro so I could use some help if this can be done. Thanks
    Peace of mind is found in some of the strangest places.

  2. #2
    Moderator VBAX Master austenr's Avatar
    Joined
    Sep 2004
    Location
    Maine
    Posts
    2,033
    Location
    I have this much:

    [VBA]Sub replaceclient()
    With ActiveDocument.Content.Find
    .ClearFormatting
    .Text = "Student"
    With .Replacement
    .ClearFormatting
    .Text = "Client"
    End With
    .Execute Replace:=wdReplaceAll
    End With

    End Sub[/VBA]

    Now I need help with looping through each document in the folder..
    Peace of mind is found in some of the strangest places.

  3. #3
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Itmay be faster, if you have a LOT of folders to work with, to use FileSystemObject.

    However, the following will work.
    [vba]Sub ReplaceText()
    Dim myFile
    myFile = Dir("c:\test\*.doc")
    Do While myFile <> ""
    replace_client
    myFile = Dir
    Loop
    End Sub[/vba]
    What this does is, check each file in the folder C:Test, if it has the extension ".doc", it call the sub replace_client. When it is done, it goes and processes the next .doc file.

  4. #4
    Moderator VBAX Master austenr's Avatar
    Joined
    Sep 2004
    Location
    Maine
    Posts
    2,033
    Location
    Fumei -- I tried to use your code but cant get it to work. Could you please take a look below. Thanks

    [VBA] Sub makechanges()
    Dim myFile As Variant
    myFile = Dir("C:\Documents and Settings\ahr1267\Desktop\NewFolder\*.doc")
    Do While myFile <> ""
    replaceclient
    myFile = Dir
    Loop
    End Sub

    Sub replaceclient()
    With ActiveDocument.Content.Find
    .ClearFormatting
    .Text = "Student"
    With .Replacement
    .ClearFormatting
    .Text = "Client"
    End With
    .Execute Replace:=wdReplaceAll
    End With
    End Sub
    [/VBA]
    Peace of mind is found in some of the strangest places.

  5. #5
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    That's because the code has to open the documents as well like:[VBA]
    Option Explicit

    Sub ReplaceText()
    Dim myFile As String

    myFile = Dir("c:\test\*.doc")
    Do While myFile <> ""
    replace_client myFile
    myFile = Dir
    Loop
    End Sub

    Sub replace_client(sPath As String)
    With Application
    .Documents.Open (sPath)

    With ActiveDocument.Content.Find
    .ClearFormatting
    .Text = "Student"
    With .Replacement
    .ClearFormatting
    .Text = "Client"
    End With
    .Execute Replace:=wdReplaceAll
    End With

    .ActiveDocument.Close True
    End With
    End Sub
    [/VBA]

    Gerry must have mist that part..

    Later..
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

  6. #6
    Moderator VBAX Master austenr's Avatar
    Joined
    Sep 2004
    Location
    Maine
    Posts
    2,033
    Location
    Does not like the line in bold:

    [VBA] Sub replace_client(sPath As String)
    With Application
    .Documents.Open (sPath)

    With ActiveDocument.Content.Find
    .ClearFormatting
    .Text = "Student"
    With .Replacement
    .ClearFormatting
    .Text = "Client"
    End With
    .Execute Replace:=wdReplaceAll
    End With

    .ActiveDocument.Close True
    End With
    End Sub [/VBA]

    can not find test1.doc but it is in the folder named exactly that...
    Peace of mind is found in some of the strangest places.

  7. #7
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    No problems over here what's your Word version?

    Run this sub and tell me what the msgbox says:[VBA]
    Sub replace_client(sPath As String)
    On Error GoTo strange
    With Application
    .Documents.Open (sPath)

    With ActiveDocument.Content.Find
    .ClearFormatting
    .Text = "Student"
    With .Replacement
    .ClearFormatting
    .Text = "Client"
    End With
    .Execute Replace:=wdReplaceAll
    End With

    .ActiveDocument.Close True
    End With
    strange:
    MsgBox Err.Description & vbCr & sPath
    Resume Next
    End Sub
    [/VBA]

    Later..
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

  8. #8
    Moderator VBAX Master austenr's Avatar
    Joined
    Sep 2004
    Location
    Maine
    Posts
    2,033
    Location
    2003
    Peace of mind is found in some of the strangest places.

  9. #9
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Quote Originally Posted by austenr
    can not find test1.doc but it is in the folder named exactly that...
    I did notice this one but the file path could be unqualified so please run the new macro.

    Oh and check are there strange signs in front of the file name like: ~ or $
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

  10. #10
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Quote Originally Posted by austenr
    2003
    Me to please run the sub the filepath must be wrong some how.
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

  11. #11
    Moderator VBAX Master austenr's Avatar
    Joined
    Sep 2004
    Location
    Maine
    Posts
    2,033
    Location
    I got it. Evidently it does not like the default file names. Changed the file the macro is in from Doc1.doc to testreplace.doc. Worked fine after that. Solved
    Peace of mind is found in some of the strangest places.

  12. #12
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    been away...glad that Joost is hovering like a momma bird....

  13. #13
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Quote Originally Posted by austenr
    I got it. Evidently it does not like the default file names. Changed the file the macro is in from Doc1.doc to testreplace.doc. Worked fine after that. Solved
    The explanation seams really strange to me. Please do try my latest code and let me see what the msgbox produces.

    Go to windows explorer first and turn of hide extensions for known file types to see if you don't have any double extensions..

    I'm really glad it's working for you but the logic of default file names doesn't make sence!
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

  14. #14
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Quote Originally Posted by fumei
    been away...glad that Joost is hovering like a momma bird....
    Well with you arround I could hover to ......

    Really Austen change back the name to Doc1.doc

    And try this new more sollid code:[VBA]
    Const sRoot As String = "C:\Documents and Settings\Joost Verdaasdonk\Bureaublad\testing\"
    Sub ReplaceText()
    Dim myFile As String

    myFile = Dir(sRoot & "*.doc")
    Do While myFile <> ""
    replace_client myFile
    myFile = Dir
    Loop
    End Sub
    Sub replace_client(sPath As String)
    Dim oDoc As Word.Document
    With Application
    Set oDoc = .Documents.Open(sRoot & sPath)

    With oDoc.Content.Find
    .ClearFormatting
    .Text = "Student"
    With .Replacement
    .ClearFormatting
    .Text = "Client"
    End With
    .Execute Replace:=wdReplaceAll
    End With

    oDoc.Close True
    End With

    Set oDoc = Nothing
    End Sub
    [/VBA]
    Change the path of sRoot to the one your using it should work now.
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

  15. #15
    Moderator VBAX Master austenr's Avatar
    Joined
    Sep 2004
    Location
    Maine
    Posts
    2,033
    Location
    works fine. Thanks to you both.
    Peace of mind is found in some of the strangest places.

  16. #16
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Quote Originally Posted by austenr
    works fine. Thanks to you both.
    Ok now I can go and eat something..your welcome!
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

  17. #17
    Moderator VBAX Master austenr's Avatar
    Joined
    Sep 2004
    Location
    Maine
    Posts
    2,033
    Location
    Another question. I need to put a prompt for the text to find and a prompt for the text to replace. Where is the best place to put that so it does not pop up for every document that is opened?
    Peace of mind is found in some of the strangest places.

  18. #18
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Like:[VBA]
    Option Explicit
    Const sRoot As String = "C:\Documents and Settings\Joost Verdaasdonk\Bureaublad\testing\"
    Sub ReplaceText()
    Dim sSearch As String
    Dim sReplace As String
    Dim myFile As String

    sSearch = InputBox("Which word are you looking for")
    sReplace = InputBox("The replacement word please")

    myFile = Dir(sRoot & "*.doc")
    Do While myFile <> ""
    replace_client myFile, sSearch, sReplace
    myFile = Dir
    Loop
    End Sub
    Sub replace_client(sPath As String, sSearch As String, sReplace As String)
    Dim oDoc As Word.Document
    With Application
    Set oDoc = .Documents.Open(sRoot & sPath)

    With oDoc.Content.Find
    .ClearFormatting
    .Text = sSearch
    With .Replacement
    .ClearFormatting
    .Text = sReplace
    End With
    .Execute Replace:=wdReplaceAll
    End With

    oDoc.Close True
    End With

    Set oDoc = Nothing
    End Sub
    [/VBA]
    Enjoy!
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

  19. #19
    Moderator VBAX Master austenr's Avatar
    Joined
    Sep 2004
    Location
    Maine
    Posts
    2,033
    Location
    I was using a Prompt in the first sub. What I did not sue was define them in the second sub). Thanks
    Peace of mind is found in some of the strangest places.

  20. #20
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Quote Originally Posted by austenr
    I was using a Prompt in the first sub. What I did not sue was define them in the second sub). Thanks
    Well defining them isn't enough..you have to pass them!

    Again your most welcome..please do visit us again in the wonderful world of Word!
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

Posting Permissions

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