+ Reply to Thread
Results 1 to 33 of 33

Find/Replace Macro to Word

  1. #1
    Registered User
    Join Date
    09-28-2011
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    24

    Find/Replace Macro to Word

    Hi,

    I have the following Macro;

    Please Login or Register  to view this content.
    .Text = "NAME1"
    .Replacement.Text = Range("C2")

    This works for all NAME1 entries in the Word document but I want to have multiple searches for example;

    .Text = "NAME1" & "NAME2"
    .Replacement.Text = Range("C2") & Range("C3")

    So that it will replace NAME1 and 2 with what is in C2 and C3. I want to do about a hundred of these.

    Also I would like some assistance on how to modify this macro so the user cannot interact with it and once the changes are complete it Prints and Closes the word document.

    Many thanks.
    Last edited by macronoob123; 10-01-2011 at 09:28 AM.

  2. #2
    Registered User
    Join Date
    09-28-2011
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    24

    Re: Struggling with a Find/Replace Macro to Word

    BUMP, thanks.

  3. #3
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Struggling with a Find/Replace Macro to Word

    Please Login or Register  to view this content.



  4. #4
    Registered User
    Join Date
    09-28-2011
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    24

    Re: Struggling with a Find/Replace Macro to Word

    Ok thanks very much but I don't quite get your modification;

    Will this replace my entire code?

    "Name1" & "Name2", , , , , , , , , c01, 2

    Are all the commas there telling me I can replace like;

    "Name1" & "Name2" & "Name3" & "Other" and son on. And I guess the 2 is the amount of find replaces it should do.

    I also get Object does not support this method error on line;

    .Selection.Find.Execute "Name1" & "Name2", , , , , , , , , c01, 2

    Thanks very much.

    EDIT: Added +rep for you.
    Last edited by macronoob123; 09-28-2011 at 04:31 PM.

  5. #5
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

  6. #6
    Registered User
    Join Date
    09-28-2011
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    24

    Re: Struggling with a Find/Replace Macro to Word

    Kenneth i have viewed that topic but I am stuck at this point;

    Please Login or Register  to view this content.
    This only makes use for one change, I want to do about 50 different changes in the Word Doc for example;

    Please Login or Register  to view this content.
    How do I achieve this.

  7. #7
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

    Re: Struggling with a Find/Replace Macro to Word

    The routines can be easily adapted to handle arrays or ranges for the Find and Replace strings. If you post a simple example xlsm and docx, I can show you more easily or just explain where the data for Find and Replace strings comes from.

    Find and replace needs are not always simple. Some structures are more challenging so example files to help you is usually best.
    Last edited by Kenneth Hobson; 09-28-2011 at 05:13 PM.

  8. #8
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Struggling with a Find/Replace Macro to Word

    Please use the Word VBEditor's helpfiles to improve your VBA knowledge (e.g. find.execute ). Do not 'suppose' things, but check them.

    A slight modification; this is all the code you'll need.

    Please Login or Register  to view this content.

  9. #9
    Registered User
    Join Date
    09-28-2011
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    24

    Re: Struggling with a Find/Replace Macro to Word

    I have a portable version of 2003 so the help files don't work, please see attached files and it will make more sense. I would learn but I have been given a deadline for tomorrow and its taken 6 hours already.

    You will see the excel spreadsheet is an Income & Expenditure Form pre-filled out as a test on how it will look.

    The Word document is filled with characters like;

    Applicant One Name
    Applicant Two Name

    The Macro would find C2 and C3 contents in this case and replace Applicant One and Applicant Two text in the document.

    And so on for the rest, once I see a working example of the above I can complete it.
    Attached Files Attached Files

  10. #10
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Struggling with a Find/Replace Macro to Word

    I posted a working example....

  11. #11
    Registered User
    Join Date
    09-28-2011
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    24

    Re: Struggling with a Find/Replace Macro to Word

    Yes yours works it seems but with .Close -1 I don't see the changes it just leave a blank word process open and not the document.

    From here I would need to apply more changes than just NAME1 and 2, unsure how I add to that code with all those , , , , , , ,

    Also would need to keep the document open in the background and disable events/user input while it performs and print then close.

    We are on the right tracks though, thanks very much.

    EDIT:

    snb I removed the close function and it remained open, the Macro does not replace anything though but runs without error.
    Last edited by macronoob123; 09-28-2011 at 05:57 PM.

  12. #12
    Registered User
    Join Date
    09-28-2011
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    24

    Re: Struggling with a Find/Replace Macro to Word

    I have now tried things like;

    Please Login or Register  to view this content.
    Still giving invalid argument error. Someone look at my attachment and it will make more sense.

  13. #13
    Registered User
    Join Date
    09-28-2011
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    24

    Re: Struggling with a Find/Replace Macro to Word

    BUMP, can anyone help?

  14. #14
    Registered User
    Join Date
    09-28-2011
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    24

    Re: Struggling with a Find/Replace Macro to Word

    BUMP, Thanks.

  15. #15
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

    Re: Struggling with a Find/Replace Macro to Word

    Your example XLS contains merged cells. These take special handling or I would have posted back earlier. Ergo, examples help us help you. This should get you off high center.

    Please Login or Register  to view this content.

  16. #16
    Registered User
    Join Date
    09-28-2011
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    24

    Re: Struggling with a Find/Replace Macro to Word

    Fantastic, this works. Thanks so much.

    Just a last question before I add to this;

    It is dealing with Ranges, Range("B2:B12"), Range("C2:C12"), True, False

    I can't think how to amend it so I can add more ranges or a single cell find/replace.

    After thats done I think I can complete it. To think ive been doing this for around 13 hours.

    Alternatively if I wanted to manually set all the findable words in the macro instead of using range, how would I do that;

    .Text = "Applicant One Name"
    .Replace = "Range("C2")
    .Text = "Applicant Two Name"
    .Replace = "Range("C3")
    .Text = "New Payment"
    .Replace = "Range("B71")

    I know this will make the macro massive but using Ranges will cause problems when it comes to things like B15:B24.

    Thanks.
    Last edited by macronoob123; 09-28-2011 at 08:58 PM.

  17. #17
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

    Re: Struggling with a Find/Replace Macro to Word

    You can use Union() or just use discontinuous ranges like this below. One cell is just Range("B2") for example. You may want to check for the case where the Replace cell value is "". IsEmpty() can be used for that as you may not want that deleted but then maybe you do. The find string being empty might be something to check as well.

    Please Login or Register  to view this content.


    For single cells to Find/Replace using this same Sub routine:
    Please Login or Register  to view this content.


    I like the bookmark method better or a mail merge better than Find/Replace. Howsoever, when deadlines approach, any method will suffice.
    Last edited by Kenneth Hobson; 09-28-2011 at 09:05 PM.

  18. #18
    Registered User
    Join Date
    09-28-2011
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    24

    Re: Struggling with a Find/Replace Macro to Word

    The first example you showed then seem more like what I need but how does that long set of ranges know what text to look for?

    Range("B2:B12, B15:B24, B27:B35, B38:B63, B66:B70")

    How will each of these know how to look for different text?

    Can we not get rid of this and use the manual way;

    .Text = "Applicant One Name" + "Applicant Two Name" + Address Line One + Address Line Two
    .Replacement.Text = Range("C2") + Range("C3") + RangeC8 + Range C9 and so on.

    So I can define what text to find in the macro not be looking at a range in the worksheet first. If we can apply this to the current macro then I am all set.
    Last edited by macronoob123; 09-28-2011 at 09:56 PM.

  19. #19
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

    Re: Struggling with a Find/Replace Macro to Word

    It does not matter whether you pass one cell or many cells, the .Value property is used to get the value. In the sub that does the work, it gets the first cell in the passed Range of cells by:
    Please Login or Register  to view this content.

    Since your replace range had cells that were merged, one has to do this sort of thing. I just did it for the Find range as well just to be on the safe side. Had I been sure that it would never be a merged cell, it could have been:
    Please Login or Register  to view this content.
    If you are going to concatenate strings, use "&" rather than "+". If you want to set just one find and one replace string, my linked example will do the job. You would need to get the first merged cell if you are getting a merged cell value as I did of course. Use the Immediate window in VBE and type your concatenation string with a ? as the first character and press enter. It is a quick debugging method.

    If you want to Find/Replace multiple times and not use my SearchMReplaceInDoc() routine, then you need another method as I explained early in this thread. Arrays, Dictionary methods, Collections, Delimited strings are just a few of the methods one can use. Of course that would require a modification to SearchMReplaceInDoc().
    Last edited by Kenneth Hobson; 09-28-2011 at 09:58 PM.

  20. #20
    Registered User
    Join Date
    09-28-2011
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    24

    Re: Struggling with a Find/Replace Macro to Word

    Ok I understand, the first solution worked best for my needs but nobody explained how I do a second or third or fourth find/replace with that code;

    Please Login or Register  to view this content.
    This enabled me to not using another range to verify what should be replace, it allowed me to customise it per cell but I do not know how to add on another search onto this as I will be doing about 50 so could you help with this?

    Many thanks,

  21. #21
    Registered User
    Join Date
    09-28-2011
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    24

    Re: Struggling with a Find/Replace Macro to Word

    Ok so I have modified the macro from the first post which is the one im going to stick with;

    Please Login or Register  to view this content.
    This now works correctly, and I am learning bit by bit here, but still need advice on how I add more than one find instance.

    Thanks,

  22. #22
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

    Re: Struggling with a Find/Replace Macro to Word

    The routine that I first sent you to could do one at a time. This is similar but I also show how to use an array with it.

    Please Login or Register  to view this content.

  23. #23
    Registered User
    Join Date
    09-28-2011
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    24

    Re: Find/Replace Macro to Word

    Ok thanks again, but this is much different to what I posted and for a macro beginner makes no sense at all;

    Who is Tom, Ken, Harry, ******

    a(1, 1) = "****"
    a(2, 1) = 2
    a(1, 2) = "Harry"
    a(2, 2) = 3
    For i = 1 To 2

    This i don't understand either.

    I wanted the code I posted to be adjusted to support multiple find/replaces as a long loop;

    Please Login or Register  to view this content.
    I understand what your trying to do, your new code is probably much better for me but I cannot understand it one but nor attempt to amend it.

    If you can just adjust the code in this post I would be grateful.

  24. #24
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

    Re: Find/Replace Macro to Word

    I have given you 3 solutions. I don't know how many solutions you want. This last Test sub showed how to send one Find/Replace at a time. It also showed how to setup an array so that it could do many in a "loop". In the array a(), the first dimension 1 or the x in a(x,y). It is the Find string. Think of it as a column. The second dimension of a() is the Replace string y in the array, a(x, y). Think of it as a row though some think opposite but arrays don't care. This is the same as if we set a range of 2 columns and 2 rows.

    The first solution that I gave is the more robust. It took care of the merged cell issue. You will have to do that yourself if you use most any other method.

    The forum changed the work DDIICCKK (less duplicated letters) to "****".
    The search replace was:

    Search Replace
    Tom 1

    By array a() For Loop:
    DDIICCKK 2
    Harry 3

    Ken 4.

    So you see: Tom, DDIICCKK, Harry, Ken; are the search strings. The replace strings were: 1, 2, 3, 4. The first dimension x in a(x, y) will be either 1 for the search string or 2 for the replace string.

    Notice that by the parameters sent to the sub, the first call did one search/replace group. The array method looped and did 2 calls to the sub. In all 3 calls, the parameters were set to not close the MSWord application nor the DOC.

    For the last call, it did close MSWord, if it was not opened, and then closed the DOC. Of course when it does, it will ask you to save it. You can record a macro to do the save and print as needed just after "If closeDoc".
    Last edited by Kenneth Hobson; 09-29-2011 at 04:44 PM.

  25. #25
    Registered User
    Join Date
    09-28-2011
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    24

    Re: Find/Replace Macro to Word

    I have updated the Macro and this is now the solution;

    Please Login or Register  to view this content.
    Can you provide your feedback on this?

  26. #26
    Registered User
    Join Date
    09-28-2011
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    24

    Re: Find/Replace Macro to Word

    Another update, I would like to know which one will be better for speed and efficiency use;

    Please Login or Register  to view this content.

  27. #27
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Find/Replace Macro to Word

    Almost there...

    Please Login or Register  to view this content.
    PS. If I were you I would save the result. In your code you don't.

    NB. the wordconstant 2 is equivalent to wdreplaceall.
    the argument 'forward' is redundant if using wdreplaceall.
    Last edited by snb; 09-30-2011 at 03:34 AM.

  28. #28
    Registered User
    Join Date
    09-28-2011
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    24

    Re: Find/Replace Macro to Word

    Very good snb, nice and clean but the examples I showed you work perfect but I would like you to choose one of these which you feel is better and i know how to disable events and screen updating but where to add here so while the macro runs the user just receives a prompt or some sort of progress bar.

    I do like your version its much cleaner and probably faster but again more confusing than mine so if we can work from mine that would be great.

    Thanks again and + rep.

  29. #29
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Find/Replace Macro to Word

    No progressbar needed when using my code, nor screenupdating =false nor....

  30. #30
    Registered User
    Join Date
    09-28-2011
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    24

    Re: Find/Replace Macro to Word

    Ok no problem but this one which is updated again;

    Please Login or Register  to view this content.
    Works perfectly, what is the advantage of yours except cleaner code? It seems to run at the same speed.

    Also I don't see in your code where Cell C is stated... You keep posting your code and I would use this but I see no difference as yet but im probably missing the obvious.

  31. #31
    Registered User
    Join Date
    09-28-2011
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    24

    Re: Find/Replace Macro to Word

    Last question, why is the above code pasting everything in CAPITALS and not as it is seen in the Cell;

    C2 = Mr Joe Bloggs

    The letter pastes this;

    MR JOE BLOGGS

    Thanks,

  32. #32
    Registered User
    Join Date
    09-28-2011
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    24

    Re: Find/Replace Macro to Word

    BUMP

    Thanks,

  33. #33
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

    Re: Find/Replace Macro to Word

    You assigned the string value to the sInput(0) as the value "C3". Normally, I would get the value of cell C3 by
    Please Login or Register  to view this content.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1