+ Reply to Thread
Results 1 to 12 of 12

Sub for replacement: how to get feeback

Hybrid View

  1. #1
    Registered User
    Join Date
    10-10-2018
    Location
    Greece, Athens
    MS-Off Ver
    2007
    Posts
    10

    Sub for replacement: how to get feeback

    Hello guys,

    I searched the Internet in the past and I found the following piece of code.

    Sub FindAndReplaceFirstStoryOfEachType(OldText As String, NewText As String)
    Dim rngStory As Range
      
      For Each rngStory In ActiveDocument.StoryRanges
        With rngStory.Find
          .Text = OldText
          .Replacement.Text = NewText
          .Wrap = wdFindContinue
          .Execute Replace:=wdReplaceAll
        End With
      Next rngStory
    End Sub
    I do not understand this Range thing (despite my experience in VBA in Excel), however this code does work.

    I need an upgrade to that code, to convert the Sub (the procedure) into a Function that would return the number of replacements.

    This code has proved to be excellent in replacing two consecutive spaces with one but ... ... here comes the big Bee Yu Tee. What about having three consecutive spaces or four?

    I could make a loop and run it many times e.g. 12 times. It would be rather unlikely to have more than 12 consecutive spaces in a document. But that sounds stupid and also increases the time required.


    If I could get the number of replacement I could run it in a repeat-until loop and run it until the number of replacements becomes zero.

    Any idea?

  2. #2
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,863

    Re: Sub for replacement: how to get feeback

    I'd not change it into function, but built in looping into the procedure.
    find.Execute returns true if it finds searched term

    so we shall loop until nothing is found.

    First I wrote it as:


    Sub FindAndReplaceAllOccurences(OldText As String, NewText As String)
    Dim rngStory As Range, replaceresult As Boolean
    Do
      replaceresult = False
      For Each rngStory In ActiveDocument.StoryRanges
        With rngStory.Find
          .Text = OldText
          .Replacement.Text = NewText
          .Wrap = wdFindContinue
          replaceresult = replaceresult Or .Execute(Replace:=wdReplaceAll)
        End With
      Next rngStory
    Loop While replaceresult
    End Sub
    But then thought that looping inside each story will be quicker - so try also on a large document this possibly faster version:
    Sub FindAndReplaceAllOccurences_Quick(OldText As String, NewText As String)
    Dim rngStory As Range, replaceresult As Boolean
    For Each rngStory In ActiveDocument.StoryRanges
      Do
        replaceresult = False
        With rngStory.Find
          .Text = OldText
          .Replacement.Text = NewText
          .Wrap = wdFindContinue
          replaceresult = replaceresult Or .Execute(Replace:=wdReplaceAll)
        End With
      Loop While replaceresult
    Next rngStory
    End Sub
    Best Regards,

    Kaper

  3. #3
    Registered User
    Join Date
    10-10-2018
    Location
    Greece, Athens
    MS-Off Ver
    2007
    Posts
    10

    Re: Sub for replacement: how to get feeback

    Quote Originally Posted by Kaper View Post
    But then thought that looping inside each story will be quicker - so try also on a large document this possibly faster version:
    Sub FindAndReplaceAllOccurences_Quick(OldText As String, NewText As String)
    Dim rngStory As Range, replaceresult As Boolean
    For Each rngStory In ActiveDocument.StoryRanges
      Do
        replaceresult = False
        With rngStory.Find
          .Text = OldText
          .Replacement.Text = NewText
          .Wrap = wdFindContinue
          replaceresult = replaceresult Or .Execute(Replace:=wdReplaceAll)
        End With
      Loop While replaceresult
    Next rngStory
    End Sub
    It works, thank you very much.

  4. #4
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,835

    Re: Sub for replacement: how to get feeback

    Try:
    Sub Demo()
    Application.ScreenUpdating = False
    Dim Rng As Range, i As Long
    For Each Rng In ActiveDocument.StoryRanges
      With Rng
        With .Find
          .ClearFormatting
          .Replacement.ClearFormatting
          .Text = "[ ^s]{2,}"
          .Replacement.Text = ""
          .Forward = True
          .Wrap = wdFindStop
          .Format = False
          .MatchWildcards = True
          .Execute
        End With
        Do While .Find.Found = True
          i = i + 1
          .Text = .Characters.First.Text
          .Collapse (wdCollapseEnd)
          .Find.Execute
        Loop
      End With
    Next
    Application.ScreenUpdating = True
    MsgBox i & " replacements made."
    End Sub
    Note that the above code processes both ordinary spaces and non-breaking spaces. However many spaces are replaced at a given location are treated as a single replacement.
    Cheers,
    Paul Edstein
    [Fmr MS MVP - Word]

  5. #5
    Registered User
    Join Date
    10-10-2018
    Location
    Greece, Athens
    MS-Off Ver
    2007
    Posts
    10

    Re: Sub for replacement: how to get feeback

    Quote Originally Posted by macropod View Post
    Try:
    Sub Demo()
    Application.ScreenUpdating = False
    Dim Rng As Range, i As Long
    For Each Rng In ActiveDocument.StoryRanges
      With Rng
        With .Find
          .ClearFormatting
          .Replacement.ClearFormatting
          .Text = "[ ^s]{2,}"
          .Replacement.Text = ""
          .Forward = True
          .Wrap = wdFindStop
          .Format = False
          .MatchWildcards = True
          .Execute
        End With
        Do While .Find.Found = True
          i = i + 1
          .Text = .Characters.First.Text
          .Collapse (wdCollapseEnd)
          .Find.Execute
        Loop
      End With
    Next
    Application.ScreenUpdating = True
    MsgBox i & " replacements made."
    End Sub
    Note that the above code processes both ordinary spaces and non-breaking spaces. However many spaces are replaced at a given location are treated as a single replacement.
    I got an erroe message.


    Run-time error '5560';

    The Find What text contains a Pattern Match expression which is not valid.

    PS: thanks for your help anyway.

  6. #6
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,835

    Re: Sub for replacement: how to get feeback

    Quote Originally Posted by Archangelos View Post
    I got an erroe message.


    Run-time error '5560';

    The Find What text contains a Pattern Match expression which is not valid.
    That indicates you're using a system with non-English-language regional settings. Change:
    .Text = "[ ^s]{2,}"
    to:
    .Text = "[ ^s]{2;}"

  7. #7
    Registered User
    Join Date
    10-10-2018
    Location
    Greece, Athens
    MS-Off Ver
    2007
    Posts
    10

    Re: Sub for replacement: how to get feeback

    Hello again,

    I have another question.

    The code includes the following structure.

    For Each Rng In ActiveDocument.StoryRanges
              With Rng
       
              End With
    I suppose that a word document consists of ... Stories? Is it right?

    The code above passes through all Stories and does the replacement.

    Is it possible to alter the code? Insted of doing replacements across the whole document to do them to a part of the document I have selected.


    Is anything like the following?

    With Selection
       
          End With

  8. #8
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,835

    Re: Sub for replacement: how to get feeback

    To limit execution to a selected range, you'd use code like:
    Sub Demo()
    Application.ScreenUpdating = False
    Dim Rng As Range, i As Long
    With Selection
      Set Rng = .Range
      With .Range
        With .Find
          .ClearFormatting
          .Replacement.ClearFormatting
          .Text = "[ ^s]{2,}"
          .Replacement.Text = ""
          .Forward = True
          .Wrap = wdFindStop
          .Format = False
          .MatchWildcards = True
          .Execute
        End With
        Do While .Find.Found = True
          If .InRange(Rng) = False Then Exit Do
          i = i + 1
          .Text = .Characters.First.Text
          .Collapse (wdCollapseEnd)
          .Find.Execute
        Loop
      End With
    End With
    Application.ScreenUpdating = True
    MsgBox i & " replacements made."
    End Sub

  9. #9
    Registered User
    Join Date
    10-10-2018
    Location
    Greece, Athens
    MS-Off Ver
    2007
    Posts
    10

    Re: Sub for replacement: how to get feeback

    Yeeaaaaah!!!

    This is what I needed. You see, I have a MS Word document in which I copy every day's reports (three per day). Each file has a full year's report. EVery time I run my code it passes through the whole document. I just need to have it done for the last days, not the all days.

    Thanks buddy.

  10. #10
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,835

    Re: Sub for replacement: how to get feeback

    Well, if you copied the reports into a separate document first, then ran the original macro against that, you wouldn't need to ensure any particular range was selected. You could then copy & paste the edited result into the main document.

  11. #11
    Registered User
    Join Date
    10-10-2018
    Location
    Greece, Athens
    MS-Off Ver
    2007
    Posts
    10

    Re: Sub for replacement: how to get feeback

    I had thought of that but I rejected the idea. It seemed a little bit counter productive.

  12. #12
    Registered User
    Join Date
    10-10-2018
    Location
    Greece, Athens
    MS-Off Ver
    2007
    Posts
    10

    Re: Sub for replacement: how to get feeback

    macropod,

    thanks for your code.

    I worked on your code and the first work was successful. I had to alter the code a little in order to fit my needs (although further work is required).

    Sub Demo(OldText As String, NewText As String)’ORIGINAL CODE: Sub Demo()
        Application.ScreenUpdating = False
        Dim Rng As Range, i As Long
        With Selection
             Set Rng = .Range
             With .Range
                  With .Find
                       .ClearFormatting
                       .Replacement.ClearFormatting
                       .Text = OldText ’ORIGINAL CODE:      .Text = "[ ^s]{2,}"
                       .Replacement.Text = NewText’ORIGINAL CODE:       .Replacement.Text = ""
                       .Forward = True
                       .Wrap = wdFindStop
                       .Format = False
                       .MatchWildcards = True
                       .Execute
                  End With
                  Do While .Find.Found = True
                     If .InRange(Rng) = False Then Exit Do
                     i = i + 1
                     .Text = NewText
                     .Collapse (wdCollapseEnd)
                     .Find.Execute
                     Loop
             End With
        End With
        Application.ScreenUpdating = True
        ‘MsgBox i & " replacements made."‘ORIGINAL CODE: MsgBox i & " replacements made."
    End Sub
    One question, what is this "Application.ScreenUpdating" thing?

+ 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. If / Then Replacement
    By dsg0110 in forum Excel General
    Replies: 1
    Last Post: 07-18-2012, 10:43 PM
  2. Row() replacement...
    By vanguru in forum Excel Formulas & Functions
    Replies: 10
    Last Post: 04-15-2009, 10:40 PM
  3. Replacement
    By T De Villiers in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-21-2006, 10:52 AM
  4. Replacement
    By mowen in forum Excel General
    Replies: 1
    Last Post: 09-07-2005, 04:05 PM
  5. Replacement
    By rasik in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 01-07-2005, 08:24 AM

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