+ Reply to Thread
Results 1 to 17 of 17

How to find a specific word and replace the immediate next word

Hybrid View

sugaprasad How to find a specific word... 01-29-2013, 09:08 AM
sugaprasad Re: How to find a specific... 01-30-2013, 02:20 AM
OllieB Re: How to find a specific... 01-30-2013, 03:15 AM
sugaprasad Re: How to find a specific... 01-30-2013, 04:17 AM
OllieB Re: How to find a specific... 01-30-2013, 04:20 AM
sugaprasad Re: How to find a specific... 01-30-2013, 05:30 AM
OllieB Re: How to find a specific... 01-30-2013, 05:37 AM
sugaprasad Re: How to find a specific... 01-30-2013, 08:22 AM
OllieB Re: How to find a specific... 01-30-2013, 08:37 AM
sugaprasad Re: How to find a specific... 01-30-2013, 09:45 AM
OllieB Re: How to find a specific... 01-30-2013, 09:48 AM
sugaprasad Re: How to find a specific... 01-30-2013, 10:10 AM
OllieB Re: How to find a specific... 01-30-2013, 10:13 AM
sugaprasad Re: How to find a specific... 01-30-2013, 10:33 AM
OllieB Re: How to find a specific... 01-30-2013, 10:36 AM
OllieB Re: How to find a specific... 01-30-2013, 10:52 AM
sugaprasad Re: How to find a specific... 01-31-2013, 02:16 AM
  1. #1
    Forum Contributor
    Join Date
    11-29-2012
    Location
    Coimbatore, India
    MS-Off Ver
    Excel 2010
    Posts
    101

    How to find a specific word and replace the immediate next word

    Hi All,

    I'm searching for a particular word in a word document using a macro. Say for example i'm searching the word "Amount". I need to replace the very next word of the found word. Suppose there is a sentence like "Amount $5,000". I will search for the word "Amount" and have to replace the $5,000 with some other value.

    One can raise a doubt why need to search for "Amount" instead of "$5,000". I will tell the reason. $5,000 will not be same in all the documents. whereas "Amount" will be constant. So I'm invoking a search t find the word "Amount" and replace the very next word. I did some coding. Can you please let me know where should i have to correct.

    Set rng = objWord.ActiveDocument.Content
            rng.Find.Execute findText:="Amount", MatchCase:=True, Forward:=True
                If rng.Find.Found Then
                    If rng.Information(wdWithInTable) Then
                        rng.Next.Sentences(1) = "$7,500"               
                    End If
                Else
                        MsgBox "Not found"                    
                End If

  2. #2
    Forum Contributor
    Join Date
    11-29-2012
    Location
    Coimbatore, India
    MS-Off Ver
    Excel 2010
    Posts
    101

    Re: How to find a specific word and replace the immediate next word

    Hi.. anyone got idea on this??

  3. #3
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: How to find a specific word and replace the immediate next word

    You can use a single find & replace instruction for the entire document using wildcards

    pvt_obj_Document.StoryRanges(wdMainTextStory).Find.Execute _
                        FindText:="Amount $????????", MatchCase:=True, MatchWholeWord:=False, ReplaceWith:="Amount $ 7,500", MatchWildcards:=True, Replace:=wdReplaceAll
    If you like my contribution click the star icon!

  4. #4
    Forum Contributor
    Join Date
    11-29-2012
    Location
    Coimbatore, India
    MS-Off Ver
    Excel 2010
    Posts
    101

    Re: How to find a specific word and replace the immediate next word

    Quote Originally Posted by OllieB View Post
    You can use a single find & replace instruction for the entire document using wildcards

    pvt_obj_Document.StoryRanges(wdMainTextStory).Find.Execute _
                        FindText:="Amount $????????", MatchCase:=True, MatchWholeWord:=False, ReplaceWith:="Amount $ 7,500", MatchWildcards:=True, Replace:=wdReplaceAll
    Thank You so much for your reply. Your idea works fine but for onething. Here in this place the word will be "Amount $?????". it means the word Amount will be BOLD always. so when i replace that word with "Amount $ 7,500" $7,500 also getting bold. I dont want it to be bold. Any suggestion??

  5. #5
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: How to find a specific word and replace the immediate next word

    Yes:
    - set the format on the first find & replace to normal font (to remove the BOLD);
    - run a second find & replace for the "Amount" string only and set format to BOLD

  6. #6
    Forum Contributor
    Join Date
    11-29-2012
    Location
    Coimbatore, India
    MS-Off Ver
    Excel 2010
    Posts
    101

    Re: How to find a specific word and replace the immediate next word

    Quote Originally Posted by OllieB View Post
    Yes:
    - set the format on the first find & replace to normal font (to remove the BOLD);
    - run a second find & replace for the "Amount" string only and set format to BOLD
    I did this after replacing the word. But unfortunately its not working.

    rng.Find.Execute FindText:="$7,500", Forward:=True
             Selection.Font.Bold = False
    am i missing something?

  7. #7
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: How to find a specific word and replace the immediate next word

    I just recorded a MACRO with the actions and it worked without a problem.

    Please see the attached code

    Sub Macro1()
         Selection.Find.ClearFormatting
         Selection.Find.Replacement.ClearFormatting
         With Selection.Find.Replacement.Font
              .Bold = False
              .Italic = False
         End With
         With Selection.Find
              .Text = "Amount $????????"
              .Replacement.Text = "Amount # 7,500"
              .Forward = True
              .Wrap = wdFindContinue
              .Format = True
              .MatchCase = False
              .MatchWholeWord = False
              .MatchAllWordForms = False
              .MatchSoundsLike = False
              .MatchWildcards = True
         End With
         Selection.Find.Execute Replace:=wdReplaceAll
         Selection.Find.ClearFormatting
         Selection.Find.Replacement.ClearFormatting
         Selection.Find.Replacement.Font.Bold = True
         With Selection.Find
              .Text = "Amount"
              .Replacement.Text = "Amount"
              .Forward = True
              .Wrap = wdFindContinue
              .Format = True
              .MatchCase = False
              .MatchWholeWord = False
              .MatchWildcards = False
              .MatchSoundsLike = False
              .MatchAllWordForms = False
         End With
         Selection.Find.Execute Replace:=wdReplaceAll
         ShowVisualBasicEditor = True
    End Sub

  8. #8
    Forum Contributor
    Join Date
    11-29-2012
    Location
    Coimbatore, India
    MS-Off Ver
    Excel 2010
    Posts
    101

    Re: How to find a specific word and replace the immediate next word

    Quote Originally Posted by OllieB View Post
    I just recorded a MACRO with the actions and it worked without a problem.

    Please see the attached code

    Sub Macro1()
         Selection.Find.ClearFormatting
         Selection.Find.Replacement.ClearFormatting
         With Selection.Find.Replacement.Font
              .Bold = False
              .Italic = False
         End With
         With Selection.Find
              .Text = "Amount $????????"
              .Replacement.Text = "Amount # 7,500"
              .Forward = True
              .Wrap = wdFindContinue
              .Format = True
              .MatchCase = False
              .MatchWholeWord = False
              .MatchAllWordForms = False
              .MatchSoundsLike = False
              .MatchWildcards = True
         End With
         Selection.Find.Execute Replace:=wdReplaceAll
         Selection.Find.ClearFormatting
         Selection.Find.Replacement.ClearFormatting
         Selection.Find.Replacement.Font.Bold = True
         With Selection.Find
              .Text = "Amount"
              .Replacement.Text = "Amount"
              .Forward = True
              .Wrap = wdFindContinue
              .Format = True
              .MatchCase = False
              .MatchWholeWord = False
              .MatchWildcards = False
              .MatchSoundsLike = False
              .MatchAllWordForms = False
         End With
         Selection.Find.Execute Replace:=wdReplaceAll
         ShowVisualBasicEditor = True
    End Sub
    when i use this part of code i'm getting wrong number of arguments or invalid property assignment error. can u please let me know what should i do. Ive used Option Explicit in the top
         With Selection.Find
              .Text = "Amount $????????"
              .Replacement.Text = "Amount # 7,500"
              .Forward = True
              .Wrap = wdFindContinue
              .Format = True
              .MatchCase = False
              .MatchWholeWord = False
              .MatchAllWordForms = False
              .MatchSoundsLike = False
              .MatchWildcards = True
         End With

  9. #9
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: How to find a specific word and replace the immediate next word

    The "Selection" part does not work because you are working from Excel and not from within Word. I recorded the macro in Word so you will need the selection variable to rng like you have done yourself in the first post.

         Set rng = objWord.ActiveDocument.Content
         With rng.Find
              .ClearFormatting
              .Replacement.ClearFormatting
              .Replacement.Font.Bold = False
              .Replacement.Font.Italic = False
              .Text = "Amount $????????"
              .Replacement.Text = "Amount # 7,500"
              .Forward = True
              .Wrap = wdFindContinue
              .Format = True
              .MatchCase = False
              .MatchWholeWord = False
              .MatchAllWordForms = False
              .MatchSoundsLike = False
              .MatchWildcards = True
              .Execute Replace:=wdReplaceAll
         End With
         With rng.Find
              .ClearFormatting
              .Replacement.ClearFormatting
              .Replacement.Font.Bold = True
              .Text = "Amount"
              .Replacement.Text = "Amount"
              .Forward = True
              .Wrap = wdFindContinue
              .Format = True
              .MatchCase = False
              .MatchWholeWord = False
              .MatchWildcards = False
              .MatchSoundsLike = False
              .MatchAllWordForms = False
              .Execute Replace:=wdReplaceAll
         End With

  10. #10
    Forum Contributor
    Join Date
    11-29-2012
    Location
    Coimbatore, India
    MS-Off Ver
    Excel 2010
    Posts
    101

    Re: How to find a specific word and replace the immediate next word

    Quote Originally Posted by OllieB View Post
    The "Selection" part does not work because you are working from Excel and not from within Word. I recorded the macro in Word so you will need the selection variable to rng like you have done yourself in the first post.

         Set rng = objWord.ActiveDocument.Content
         With rng.Find
              .ClearFormatting
              .Replacement.ClearFormatting
              .Replacement.Font.Bold = False
              .Replacement.Font.Italic = False
              .Text = "Amount $????????"
              .Replacement.Text = "Amount # 7,500"
              .Forward = True
              .Wrap = wdFindContinue
              .Format = True
              .MatchCase = False
              .MatchWholeWord = False
              .MatchAllWordForms = False
              .MatchSoundsLike = False
              .MatchWildcards = True
              .Execute Replace:=wdReplaceAll
         End With
         With rng.Find
              .ClearFormatting
              .Replacement.ClearFormatting
              .Replacement.Font.Bold = True
              .Text = "Amount"
              .Replacement.Text = "Amount"
              .Forward = True
              .Wrap = wdFindContinue
              .Format = True
              .MatchCase = False
              .MatchWholeWord = False
              .MatchWildcards = False
              .MatchSoundsLike = False
              .MatchAllWordForms = False
              .Execute Replace:=wdReplaceAll
         End With
    Thanks that's working great.
    I just faced one problem.

    "Amount $????????"
    Here the dollar value will not be always same. It will change everytime. So it can't be fixed. So is there anyother way where i can extract the next word of the Amount.

    I'm sorry to bug you all the time when i need a help.

  11. #11
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: How to find a specific word and replace the immediate next word

    I am not sure what you mean. The Find "Amount $????????" will find every occurrence of the string Amount $.... irrespective of the actual amount shown after the $, just like you asked.

  12. #12
    Forum Contributor
    Join Date
    11-29-2012
    Location
    Coimbatore, India
    MS-Off Ver
    Excel 2010
    Posts
    101

    Re: How to find a specific word and replace the immediate next word

    Quote Originally Posted by OllieB View Post
    I am not sure what you mean. The Find "Amount $????????" will find every occurrence of the string Amount $.... irrespective of the actual amount shown after the $, just like you asked.
    yes you are right.. Let me tell you clearly. I have these two sentences with bullets.
    • Amount $5,000
    • Payment $8,500

    so now "Amount $????????" replacing some part of next sentence as well.(ie its getting replaced as Amount $7,500yment $8,500). And also $5,000 can be $15,000 at times. so we can't fix the number of Question marks. This is my concern here. Hope i'm clear

  13. #13
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: How to find a specific word and replace the immediate next word

    If this is your problem, the automated find & replace solution will only work if you know the length of the amount string you are replacing. If you do not this up front, then the current solution we are discussing will not work and you are back at square one.

  14. #14
    Forum Contributor
    Join Date
    11-29-2012
    Location
    Coimbatore, India
    MS-Off Ver
    Excel 2010
    Posts
    101

    Re: How to find a specific word and replace the immediate next word

    Quote Originally Posted by OllieB View Post
    If this is your problem, the automated find & replace solution will only work if you know the length of the amount string you are replacing. If you do not this up front, then the current solution we are discussing will not work and you are back at square one.
    OOPS.. my bad.. I know this is too much to ask you...you got any idea for my problem?

  15. #15
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: How to find a specific word and replace the immediate next word

    A dirty way to do this, is to consider that the contents of the document can also be read/updated as a string

    Activedocument.Content.Text
    So if your documents are not too large you could use a regular search loop in VBA to find Amount and isolate and replace the $value. However if your documents are very large I do not know how VBA will handle the size of the string. Word is not my speciality.

  16. #16
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: How to find a specific word and replace the immediate next word

    For "small" document the following might work (replace new_amount with actual value provided as a parameter or something like that)

    '#
    '# declare
    '#
         Dim lngFound As Long
         Dim lngStart As Long
         Dim lngAmountStart As Long
         Dim lngAmountEnd As Long
         Dim rngContent As Range
         
    '#
    '# initialise
    '#
         lngStart = 1
         Set rngContent = objWord.ActiveDocument.Content
    '#
    '# loop though the contents
    '#
         lngFound = InStr(lngStart, rngContent.Text, "Amount")
         While lngFound > 0
              '#
              '# find the currency symbol
              '#
                   lngStart = lngFound + 6
                   lngFound = InStr(lngStart, rngContent.Text, "$")
              '#
              '# determine the starting position of the amount taking into consideration
              '# that the currency symbol may be followed by a space
              '#
                   If Mid$(rngContent.Text, lngFound + 1, 1) = " " Then
                        lngAmountStart = lngFound + 2
                   Else
                        lngAmountStart = lngFound + 1
                   End If
              '#
              '# determine the ending position for the amount identified by a space
              '#
                   lngAmountEnd = InStr(lngAmountStart, rngContent.Text, " ")
              '#
              '# replace the existing amount value with the desired replacement
              '#
                   ActiveDocument.Content = Left$(rngContent.Text, lngAmountStart - 1) & "new_amount" & Mid$(rngContent.Text, lngAmountEnd)
              
              '#
              '# find the next occurrence of the text Amount
              '#
                   lngStart = lngAmountEnd
                   lngFound = InStr(lngStart, rngContent.Text, "Amount")
         Wend

  17. #17
    Forum Contributor
    Join Date
    11-29-2012
    Location
    Coimbatore, India
    MS-Off Ver
    Excel 2010
    Posts
    101

    Re: How to find a specific word and replace the immediate next word

    Quote Originally Posted by OllieB View Post
    For "small" document the following might work (replace new_amount with actual value provided as a parameter or something like that)

    '#
    '# declare
    '#
         Dim lngFound As Long
         Dim lngStart As Long
         Dim lngAmountStart As Long
         Dim lngAmountEnd As Long
         Dim rngContent As Range
         
    '#
    '# initialise
    '#
         lngStart = 1
         Set rngContent = objWord.ActiveDocument.Content
    '#
    '# loop though the contents
    '#
         lngFound = InStr(lngStart, rngContent.Text, "Amount")
         While lngFound > 0
              '#
              '# find the currency symbol
              '#
                   lngStart = lngFound + 6
                   lngFound = InStr(lngStart, rngContent.Text, "$")
              '#
              '# determine the starting position of the amount taking into consideration
              '# that the currency symbol may be followed by a space
              '#
                   If Mid$(rngContent.Text, lngFound + 1, 1) = " " Then
                        lngAmountStart = lngFound + 2
                   Else
                        lngAmountStart = lngFound + 1
                   End If
              '#
              '# determine the ending position for the amount identified by a space
              '#
                   lngAmountEnd = InStr(lngAmountStart, rngContent.Text, " ")
              '#
              '# replace the existing amount value with the desired replacement
              '#
                   ActiveDocument.Content = Left$(rngContent.Text, lngAmountStart - 1) & "new_amount" & Mid$(rngContent.Text, lngAmountEnd)
              
              '#
              '# find the next occurrence of the text Amount
              '#
                   lngStart = lngAmountEnd
                   lngFound = InStr(lngStart, rngContent.Text, "Amount")
         Wend
    Thanks much for your help. I will try this.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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