Word VBA >> Macro with Replace in it locks up.

by Simon I » Tue, 10 Aug 2004 00:49:19 GMT

I have created a macro to replace one phrase with another.

Generally it runs correctly, but when I run it with the
last occurrence of the phrase in the document the macro
hangs. I have to press Esc to get back to the document.

If I then insert the phrase a few more times then run the
macro again it does nothing (as if it has been disabled).
Closing and opening the document again - and the macro
runs OK (until I get to the last occurrence again).

I note that if you do the same Replace from the menus,
Word tells you it has reached the end of the document and
awaits an OK. Is this my problem? - if so how do I deal
with it in the macro.?

Thanks for you help on this.

Here is the macro text:
+++++++++++++++++++++++++++++++++++

Sub noSig()
'
' noSig Macro
' Macro recorded 8/5/2004 by Simon
'
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "*quote "
.Replacement.Text = "=removed by SI ="
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.Execute
With Selection
If .Find.Forward = True Then
.Collapse Direction:=wdCollapseStart
Else
.Collapse Direction:=wdCollapseEnd
End If
.Find.Execute Replace:=wdReplaceOne
If .Find.Forward = True Then
.Collapse Direction:=wdCollapseEnd
Else
.Collapse Direction:=wdCollapseStart
End If
.Find.Execute
End With
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.MoveRight Unit:=wdCharacter, Count:=1
End Sub



Word VBA >> Macro with Replace in it locks up.

by Klaus Linke » Tue, 10 Aug 2004 05:21:51 GMT


Hi Simon,

See if Bill Coan's articles help:
http://word.mvps.org/FAQs/MacrosVBA/PartialWcrdMatch.htm
http://word.mvps.org/FAQs/MacrosVBA/FlushFR.htm

Regards,
Klaus





Similar Threads

1. VS2003 IDE frequent lock-ups - VB.Net

2. WORD 2000 printing lock-ups

Once in awhile, when trying to print a document in WORD 
2000, it will say, "WORD is preparing to background print 
the document." in the lower left of the screen.  It then 
seems to just lock-up and will not print or do anything.  
I have to Ctl/Alt/Del and shut down.  Usually have to 
totally shut down the computer and re-start.  What 
does "background print" the document mean and how can i 
fix this?  It can realy be a hassle until I shut down and 
re-start.  Sometimes that does not work the first time 
either.  

3. Microsoft Word 2007 I Cannot Turn Off Macros Warning Pop Ups - MS Word Support

4. Using find and replace or macros to replace page ranges

I am using Word 2003, but if I can successfully figure out how to do this, I 
would also like to be able to do the same on Word 2004 for Mac. 

I am trying to figure out a way to automate replacing numbers in page ranges 
to match the Chicago Manual of Style. For instance, I need to change 149-167 
to 149-67, but not 149-249 to 149-49 or not 106-108 to 106-08 (instead that 
should go to 106-8). I'm not sure I have thought out all the exceptions, 
though these are the most basic. I am mainly looking for the methodology to 
use, whether through find and replace or macros. I don't want to spend too 
much time trying to figure it out, because I'm not sure the return will be 
worth a lot of time invested. 

I know that by using wildcard search and replace I can do most of this. For 
instance, I could search for 
([!0-9])([0-9])([0-9])([0-9])[0-9])([0-9])([0-9]) and replace with 
\1\2\3\4\5\6, replacing something like 12526to "1256.But that 
doesn't help me with all because I would still have to make adjustments for 
the above-mentioned exceptions. 

If automating using macros is the best choice, I would also like to figure 
out a way to double-check that changes were made correctly and there were no 
mistakes. So is it possible to write into the macro (if doing a macro) to 
copy all replacements (or all things being replaced) into a blank word 
document to be able to quickly scan all instances of replacements? Or to 
highlight all changes made in the document to be able to quickly pick up what 
was changed? 

Does this make sense? Can anyone help? Thanks. 

5. Macro using Replace does not replace

6. replace field in primary header using find and replace in a macro

OK this is my situation

I can record a macro and as I am recording it the process works fine.
I replace the field function Time with a Createdate one and it does this in 
a Primary header's fields aswell as the main document area.

all the fields in question are inside primary headers

upon re-running the macro it fails.

regardless of where I place the cursor prior to running the macro.

please help me - I am in danger of  becoming insane over this !

This is the macro 


Sub Replacer()
'
' Replacer Macro
' Macro recorded 05/08/2008 by Simun
'
    ActiveWindow.View.ShowFieldCodes = Not ActiveWindow.View.ShowFieldCodes
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = "TIME"
        .Replacement.Text = "CREATEDATE"
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
    ActiveWindow.View.ShowFieldCodes = Not ActiveWindow.View.ShowFieldCodes
End Sub



Thanks in advance for any help !

Steve

7. Using find and replace to replace text in macros - Visual Basic/VB

8. Replace methode, Replace Function, Stringbuilder replace, Regex Replace, Split

Hi Newsgroup,

I have given an answer in this newsgroup about a "Replace".

There came an answer on that I did not understand, so I have done some
tests.

I got the idea that someone said, that the split method and the
regex.replace method was better than the string.replace method and replace
function. I did not believe that.

I have tested this in two ways: with iteration of small strings and with a
long string. (Because that I myself use often the Stringbuilder replace,
have I added that too).

My results where comparative in spended time (not very scientific done) and
the 1 as basis.

Small strings
VB replace 4
String replace 1
Stringbuilder replace 2
Split  6
Regex 25
Long string
VB replace 170
String replace 1
Stringbuilder 2
Split 160
Regex 16

Who will check if my test program is right and get the same results?

Cor

Public Module Main
    Public Sub Main()
        Dim max As Integer = 20000
        Dim oldstring As String = "**item1%%**item2%%**item3%%"
        Dim newstring As String
        Dim myarray() As String
        Dim sb As New System.Text.StringBuilder
        Dim StartTick As Integer = Environment.TickCount
        For i As Integer = 0 To max
        Next
        Dim rest As Integer = Environment.TickCount - StartTick
        StartTick = Environment.TickCount
        For i As Integer = 0 To max
            newstring = Replace(oldstring, "%%", "")
        Next
        Debug.WriteLine((Environment.TickCount - StartTick - rest).ToString
_
        & " " & newstring, "Replace")
        StartTick = Environment.TickCount
        For i As Integer = 0 To max
            newstring = oldstring.Replace("%%", "")
        Next
        Debug.WriteLine((Environment.TickCount - StartTick - rest).ToString
& _
        " " & newstring, "String.Replace")
        StartTick = Environment.TickCount
        For i As Integer = 0 To max
            sb = New System.Text.StringBuilder(oldstring)
            newstring = sb.Replace("%%", "").ToString
        Next
        Debug.WriteLine((Environment.TickCount - StartTick - rest).ToString
& _
        " " & newstring, "Stringbuilder.Replace")
        StartTick = Environment.TickCount
        For i As Integer = 0 To max
            myarray = Split(oldstring, "%%", , CompareMethod.Text)
            newstring = String.Join("", myarray)

        Next
        Debug.WriteLine((Environment.TickCount - StartTick - rest).ToString
& _
        " " & newstring, "Split")

        StartTick = Environment.TickCount
        For i As Integer = 0 To max
            newstring =
System.Text.RegularExpressions.Regex.Replace(oldstring, "%%", "")
        Next
        Debug.WriteLine((Environment.TickCount - StartTick - rest).ToString
& _
        " " & newstring, "Regex.Replace")
        Debug.WriteLine("----------------- now with long
string -------------")
        sb = New System.Text.StringBuilder("")
        For i As Integer = 0 To max
            sb.Append(oldstring)
        Next
        oldstring = sb.ToString
        StartTick = Environment.TickCount
        newstring = Replace(oldstring, "%%", "")
        Debug.WriteLine((Environment.TickCount - StartTick).ToString _
        & " " & newstring.Substring(0, 20), "Replace")
        StartTick = Environment.TickCount
        newstring = oldstring.Replace("%%", "")
        Debug.WriteLine((Environment.TickCount - StartTick).ToString & _
        " " & newstring.Substring(0, 20), "String.Replace")
        StartTick = Environment.TickCount
        sb = New System.Text.StringBuilder(oldstring)
        newstring = sb.Replace("%%", "").ToString
        Debug.WriteLine((Environment.TickCount - StartTick).ToString & _
        " " & newstring.Substring(0, 20), "Stringbuilder.Replace")
        StartTick = Environment.TickCount
        myarray = Split(oldstring, "%%", , CompareMethod.Text)
        newstring = String.Join("", myarray)
        Debug.WriteLine((Environment.TickCount - StartTick).ToString & _
" " & newstring.Substring(0, 20), "Split")
        StartTick = Environment.TickCount
        newstring = System.Text.RegularExpressions.Regex.Replace(oldstring,
"%%", "")
        Debug.WriteLine((Environment.TickCount - StartTick).ToString & _
        " " & newstring.Substring(0, 20), "Regex.Replace")
    End Sub