+ Reply to Thread
Results 1 to 12 of 12

Find and replace a new sentence or paragraph in word doc

  1. #1
    Forum Contributor
    Join Date
    09-09-2012
    Location
    Manila,Philippines
    MS-Off Ver
    Excel 2003
    Posts
    153

    Find and replace a new sentence or paragraph in word doc

    Hi guys,

    Is it possible to find a sentence/paragraph in a word doc and replace it with a new one?

    The old and revised sentence/paragraph is in excel file. So if for example the old is in A1 then the macro will find the exact content of A1 in the word doc and then if B1 is not empty then the macro will copy the content of B1 and replace the found sentence in word doc.

  2. #2
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: Find and replace a new sentence or paragraph in word doc

    Please Login or Register  to view this content.
    Be aware it replaces all occurrances of a string, not just particular ones... and, as coded, runs from a worksheet class module using data on that sheet.

  3. #3
    Forum Contributor
    Join Date
    09-09-2012
    Location
    Manila,Philippines
    MS-Off Ver
    Excel 2003
    Posts
    153

    Re: Find and replace a new sentence or paragraph in word doc

    I got this error on .Text = c.value

    Run-time error '5854':
    String parameter too long.

  4. #4
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: Find and replace a new sentence or paragraph in word doc

    I suppose the 'paragraph' should have warned me...

    Word has a max of 255 characters in a Find/Replace operation. The workaround is rather long and involved, unfortunately.

    As an example, it would be possible to search for a string (shorter than 255 characters). If found, delete the entire paragraph containing the string and then simply type in the replacement...

    You need to give example of your Search and Replace strings. And explain if any are replacing complete paragraphs, or if just replacing text within a paragraph.

  5. #5
    Forum Contributor
    Join Date
    09-09-2012
    Location
    Manila,Philippines
    MS-Off Ver
    Excel 2003
    Posts
    153

    Re: Find and replace a new sentence or paragraph in word doc

    I tried a shorter sentence or word but it doesn't replace the word in excel...

  6. #6
    Forum Contributor
    Join Date
    09-09-2012
    Location
    Manila,Philippines
    MS-Off Ver
    Excel 2003
    Posts
    153

    Re: Find and replace a new sentence or paragraph in word doc

    Is there an alternative solution to the number of characters? Maybe I can use Left(c.Value,255) for the find but for the replacement, I cannot limit it to 255 characters only.

    Can we apply the Goto Rng for the replacement?

    And I am trying to execute the code but it didn't replace even a single word in the document...

  7. #7
    Forum Contributor
    Join Date
    09-09-2012
    Location
    Manila,Philippines
    MS-Off Ver
    Excel 2003
    Posts
    153

    Re: Find and replace a new sentence or paragraph in word doc

    FYI I am placing the code excel not in word. I already have a code in excel to open the word file and just copied the code you provided.

  8. #8
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: Find and replace a new sentence or paragraph in word doc

    ..and just copied the code you provided
    Post your current code.

  9. #9
    Forum Contributor
    Join Date
    09-09-2012
    Location
    Manila,Philippines
    MS-Off Ver
    Excel 2003
    Posts
    153

    Re: Find and replace a new sentence or paragraph in word doc

    Please Login or Register  to view this content.

  10. #10
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: Find and replace a new sentence or paragraph in word doc

    A slight twist.

    THis code searches for a string - this can be any length (up to 255 characters). If found, it selects the entire paragraph the text was found in and 'types' any text in the cell to the right - so replacing the current paragraph.

    This is probably the simplest way to get around the 255 character limitation.

    The disadvantages are only the first occurrance will be replaced and you have to determine a unique piece of text in existing paragraphs that will be used as the search string.

    Between this and my first post (which does work) you should be able to work around any other issues.

    Please Login or Register  to view this content.
    (Given some cursory testing and works - at least it finds text, selects entire paragraphs and types the alternate text)

  11. #11
    Forum Contributor
    Join Date
    09-09-2012
    Location
    Manila,Philippines
    MS-Off Ver
    Excel 2003
    Posts
    153

    Re: Find and replace a new sentence or paragraph in word doc

    What is the return value of the If WordRange.Find.Execute?

    I assume that if the macro found the word it will go through the if statement but it did'nt proceed... The procedure goes like if then end if. And what does the Paragraphs(1) says? I tried to remove the if and it proceeds to replacing my letter head and not actually selecting the range of the found word.

  12. #12
    Forum Contributor
    Join Date
    09-09-2012
    Location
    Manila,Philippines
    MS-Off Ver
    Excel 2003
    Posts
    153

    Re: Find and replace a new sentence or paragraph in word doc

    I got it... It does not read special characters like bulleted list.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. to find a word in a sentence in acell
    By erickmcburger in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-23-2014, 08:37 AM
  2. Replace word in a sentence
    By dmarop in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-14-2014, 02:50 PM
  3. [SOLVED] How can I replace a word from a sentence in a cell & substitute word from another cell
    By rionoah in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 09-11-2012, 08:44 AM
  4. Find word in sentence
    By WCE_123 in forum Excel General
    Replies: 2
    Last Post: 09-21-2011, 06:59 AM
  5. How to find and replace a word in a sentence with VBA
    By Gunilla in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 04-20-2010, 09:52 PM

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