Results 1 to 12 of 12

Thread: Replacing a text with another in multiple documents.

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2013
    Posts
    7

    Lightbulb Replacing a text with another in multiple documents.

    I'm sure it has been discussed before and I am really sorry to bring it up. At my job I have to edit about 400 documents every month about 2-3 times for each document. So far I've figured out how to replace text with the same texts (ex.dates and times) but I have to change names for every document so far I've just used the find and replace for every document but my job load is getting bigger and bigger.

    So point is I am trying to change the documents who all have <names> in it with the person's name whose document belongs to. In one simple click. For example one document i want replaced with Mr. Henriquez another with Mr. Johnson so on and so on. A point towards the right direction would be greatly appreciated. or any help.



    Ps. I have the most minimum experience with VBA but am always willing to learn.

    THank you!

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Replacing a text with another in multiple documents.

    is it all documents in one folder, or all documents with some standard naming?

    how do you tell which documents you want to edit?

    you can use DIR to find matching documents, open the document, do the find /replace, close and save changes, DIR to find next matching
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2013
    Posts
    7

    Re: Replacing a text with another in multiple documents.

    yeah, they're all in one folder, for example every document represents a person and what he/she did throughout the day. I have a set of pre written notes with "NAMES" in it to represent the person for example (Throughout the day I saw 'NAMES' interacting with others and socializing.) the 'NAMES' is where the persons actual name would go. I copy and paste into each individual document the names, so i have to do this about 1300 times(amount of monthly documents I am in charge of) so I've become a little crazy doing this month in an out. So myquestion is, is there a way to have the document automatically replace 'NAMES' with the person whose document it belongs to?

    Thanks for your attention and time.

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Replacing a text with another in multiple documents.

    is there a way to have the document automatically replace 'NAMES'
    yes, but i am not sure i fully comprehend how you determine which name to replace with, there is nothing very complicated about the code, just which name to replace with i do not follow
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  5. #5

    Thread Starter
    New Member
    Join Date
    Jan 2013
    Posts
    7

    Re: Replacing a text with another in multiple documents.

    That is exactly where I've hit a wall. the 'names' would have to be replaced with whoevers document it is in. or am I just out of luck on this one?

    I need to find out if there is a method where I can get it to determine it on its own. to avoid having to use the find and replace every time I have to switch NAMES out with the persons name.

  6. #6
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Replacing a text with another in multiple documents.

    I need to find out if there is a method where I can get it to determine it on its own
    you still have given no indication how you can determine the name to switch, how am i supposed to guess?
    you can have some sort of lookup table or whatever once you can provide some parameter to indicate the correct name
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  7. #7

    Thread Starter
    New Member
    Join Date
    Jan 2013
    Posts
    7

    Re: Replacing a text with another in multiple documents.

    Hmmm... again thanks for giving this your attention. I determine whose name to write in by the file name; Doe,John.doc is the file so i would write 'Mr. Doe' where 'Names' goes. Does that answer your question?

    and how would this look up table work? and I wouldn't know how to indicate parameters so that the correct name is used.

    Sorry again for the frustration this has caused.

  8. #8
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Replacing a text with another in multiple documents.

    Does that answer your question?
    i guess i could work with that

    you can try like
    Code:
    mypath = "c:\test\"     'change to suit
    fname = dir(mypath & "*.doc")
    do while len(fname) > 0 
      set doc = documents.open(mypath & fname)
      pos =  instr(fname, ",")
      if pos > 0 then
        repl = "Mr " & left(fname, pos - 1)
        doc.Content.Find.Execute "<names>", , , , , , , , , repl, wdReplaceAll
        doc.close true
      end if
      fname = dir
    loop
    this would assume that all clients are male, that all doc file names in the folder are in the format "Doe,Fred.doc"
    filenames not containing "," will be ignored
    i did not test this
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  9. #9

    Thread Starter
    New Member
    Join Date
    Jan 2013
    Posts
    7

    Re: Replacing a text with another in multiple documents.

    Nothing has been changed after placing this code? Am I doing something wrong? could it be possible that everything is on a network drive? I've been at it for hours and came up to zip.
    Code:
    [mypath = "Z:\SOCIAL SERVICE DEPARTMENT\Social Work Summary Notes\2013 Social Work Summary Notes\January 2013 SW Notes"     'change to suit
    fname = Dir(mypath & "*.doc")
    Do While Len(fname) > 0
      Set doc = Documents.Open(mypath & fname)
      pos = InStr(fname, ",")
      If pos > 0 Then
        repl = "Mr. " & Left(fname, pos - 1)
        doc.Content.Find.Execute "<Mr.Ms.>", , , , , , , , , repl, wdReplaceAll
        doc.Close True
      End If
      fname = Dir
    Loop
    End Sub]

  10. #10

    Thread Starter
    New Member
    Join Date
    Jan 2013
    Posts
    7

    Re: Replacing a text with another in multiple documents.

    clients are both male and female but I can seperate them into different folder and just change the 'Mr.' to 'Ms.' and precisely all files are format'Johnson, Max.doc'

  11. #11

    Thread Starter
    New Member
    Join Date
    Jan 2013
    Posts
    7

    Re: Replacing a text with another in multiple documents.

    I receive an outside invalid procedure error message.

  12. #12
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Replacing a text with another in multiple documents.

    the code snippet i posted would need to be placed inside a procedure (SUB)
    possibly the click event for a command button, depending what you want, to start the code running
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

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



Click Here to Expand Forum to Full Width