Making Macro2 start running after Macro1 ends

R

Roy

Hi
I managed to record & edit the two simple macros showing below.


Sub Macro1()
'
' Macro1 Macro
' Macro created 9/7/2008 by me
'
Selection.Find.ClearFormatting
With Selection.Find
.Text = "[a-h]{3,}"
.Forward = True
.Wrap = wdFindContinue
.MatchWildcards = True
End With
Selection.Find.Execute
Selection.Find.ClearFormatting
With Selection.Find
.Text = ""
.MatchWildcards = False
End With
Selection.Find.Execute
End Sub


Sub Macro2()
'
' Macro2 Macro
' Macro recorded 9/7/2008 by me
'
Selection.Find.ClearFormatting
With Selection.Find
.Text = "[a-fh][a-h]"
.Forward = True
.Wrap = wdFindContinue
.MatchWildcards = True
End With
Selection.Find.Execute
Selection.Find.ClearFormatting
With Selection.Find
.Text = "g[gh]"
.Forward = True
.Wrap = wdFindContinue
.MatchWildcards = True
End With
Selection.Find.Execute
Selection.Find.ClearFormatting
With Selection.Find
.Text = ""
.MatchWildcards = False
End With
Selection.Find.Execute
End Sub


Their job is to find an erroneous sequence of letters (represented here with
the letters a to h). After creating toolbar buttons for each of them, they
should be used in the following manner. The user clicks on Macro1 button; if
text is found, he should correct it manually (the correction should be
composed of 2 letters only). He keeps clicking on Macro1 button and making
corrections till no more results are found. After that, he moves to Macro2
button and does the same thing, making any needed corrections manually. Now
my question is, Is it possible to make Macro2 automatically start running
when Macro1 finds no more results, maybe by merging the 2 macros into one or
anything else?

PS: I had to add the '...Text = "" MatchWildcards = False ...' part
to each macro because it's the only way I found to automatically uncheck Use
Wildcards. The user is supposed to make normal, non-wildcard searches after
using the macros. So the part was added only to avoid confusing him.

Thanks in advance for your help.
 
G

Graham Mayor

On the face of it if you simply remove

End Sub


Sub Macro2()
'
' Macro2 Macro
' Macro recorded 9/7/2008 by me
'

from between the macros you will create one macro, but it might be better to
simply get the macro to do the whole task - which is somewhat vague and
complicated by the fact that the user must make a choice at each find.

If you know before you start what the combinations and their changes are to
be you can log them in a two column table with the words to search in column
1 and their replacements in column 2. Save the document with the table (here
saved as D:\My Documents\Test\changes.doc) and change the document path in
the macro to match then run the macro on your document.
http://www.gmayor.com/installing_macro.htm

Sub ReplaceFromTableList()

Dim ChangeDoc As Document, RefDoc As Document
Dim cTable As Table
Dim oldPart As Range, newPart As Range
Dim i As Long
Dim sFname As String

sFname = "D:\My Documents\Test\changes.doc"
Set RefDoc = ActiveDocument
Set ChangeDoc = Documents.Open(sFname)
Set cTable = ChangeDoc.Tables(1)
RefDoc.Activate
For i = 1 To cTable.Rows.Count
Set oldPart = cTable.Cell(i, 1).Range
oldPart.End = oldPart.End - 1
Set newPart = cTable.Cell(i, 2).Range
newPart.End = newPart.End - 1
With Selection
.HomeKey wdStory
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Execute findText:=oldPart, _
ReplaceWith:=newPart, _
Replace:=wdReplaceAll, _
MatchWholeWord:=True, _
MatchWildcards:=False, _
Forward:=True, _
Wrap:=wdFindContinue
End With
End With
Next i
ChangeDoc.Close wdDoNotSaveChanges
End Sub


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Hi
I managed to record & edit the two simple macros showing below.


Sub Macro1()
'
' Macro1 Macro
' Macro created 9/7/2008 by me
'
Selection.Find.ClearFormatting
With Selection.Find
.Text = "[a-h]{3,}"
.Forward = True
.Wrap = wdFindContinue
.MatchWildcards = True
End With
Selection.Find.Execute
Selection.Find.ClearFormatting
With Selection.Find
.Text = ""
.MatchWildcards = False
End With
Selection.Find.Execute
End Sub


Sub Macro2()
'
' Macro2 Macro
' Macro recorded 9/7/2008 by me
'
Selection.Find.ClearFormatting
With Selection.Find
.Text = "[a-fh][a-h]"
.Forward = True
.Wrap = wdFindContinue
.MatchWildcards = True
End With
Selection.Find.Execute
Selection.Find.ClearFormatting
With Selection.Find
.Text = "g[gh]"
.Forward = True
.Wrap = wdFindContinue
.MatchWildcards = True
End With
Selection.Find.Execute
Selection.Find.ClearFormatting
With Selection.Find
.Text = ""
.MatchWildcards = False
End With
Selection.Find.Execute
End Sub


Their job is to find an erroneous sequence of letters (represented
here with the letters a to h). After creating toolbar buttons for
each of them, they should be used in the following manner. The user
clicks on Macro1 button; if text is found, he should correct it
manually (the correction should be composed of 2 letters only). He
keeps clicking on Macro1 button and making corrections till no more
results are found. After that, he moves to Macro2 button and does the
same thing, making any needed corrections manually. Now my question
is, Is it possible to make Macro2 automatically start running when
Macro1 finds no more results, maybe by merging the 2 macros into one
or anything else?

PS: I had to add the '...Text = "" MatchWildcards = False ...'
part to each macro because it's the only way I found to automatically
uncheck Use Wildcards. The user is supposed to make normal,
non-wildcard searches after using the macros. So the part was added
only to avoid confusing him.

Thanks in advance for your help.
 
R

Roy

Thanks Graham

The problem with "joining" those two macros into one is that it makes Search
jump to the first 2 letters of any 3+ occurrences of the letters, in other
words it will use Macro2 to find what should be only found with Macro1. Any
3+ occurrence is erroneous, it should be dealt with first before moving to
any 2-letter erroneous occurrences.

For Macro2 searches, one can know beforehand what the change should be, so
the macro you provided comes in handy indeed. It will save a lot of time.
Thank you. But it's impossible to use it for macro1 searches, first because
only a human can know what the corrrect change should be in each and every
case (AI?) Second, if I were going to include every possible erroneous
occurrence of those letters, I would have 8x8x8=512 rows for the sequences of
3 letters; 8x8x8x8= 4096 rows for the sequences of four letters...

Sorry it's vague. But believe me, "ignorance is bliss" in this very case.
Thanks anyway.

Graham Mayor said:
On the face of it if you simply remove

End Sub


Sub Macro2()
'
' Macro2 Macro
' Macro recorded 9/7/2008 by me
'

from between the macros you will create one macro, but it might be better to
simply get the macro to do the whole task - which is somewhat vague and
complicated by the fact that the user must make a choice at each find.

If you know before you start what the combinations and their changes are to
be you can log them in a two column table with the words to search in column
1 and their replacements in column 2. Save the document with the table (here
saved as D:\My Documents\Test\changes.doc) and change the document path in
the macro to match then run the macro on your document.
http://www.gmayor.com/installing_macro.htm

Sub ReplaceFromTableList()

Dim ChangeDoc As Document, RefDoc As Document
Dim cTable As Table
Dim oldPart As Range, newPart As Range
Dim i As Long
Dim sFname As String

sFname = "D:\My Documents\Test\changes.doc"
Set RefDoc = ActiveDocument
Set ChangeDoc = Documents.Open(sFname)
Set cTable = ChangeDoc.Tables(1)
RefDoc.Activate
For i = 1 To cTable.Rows.Count
Set oldPart = cTable.Cell(i, 1).Range
oldPart.End = oldPart.End - 1
Set newPart = cTable.Cell(i, 2).Range
newPart.End = newPart.End - 1
With Selection
.HomeKey wdStory
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Execute findText:=oldPart, _
ReplaceWith:=newPart, _
Replace:=wdReplaceAll, _
MatchWholeWord:=True, _
MatchWildcards:=False, _
Forward:=True, _
Wrap:=wdFindContinue
End With
End With
Next i
ChangeDoc.Close wdDoNotSaveChanges
End Sub


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Hi
I managed to record & edit the two simple macros showing below.


Sub Macro1()
'
' Macro1 Macro
' Macro created 9/7/2008 by me
'
Selection.Find.ClearFormatting
With Selection.Find
.Text = "[a-h]{3,}"
.Forward = True
.Wrap = wdFindContinue
.MatchWildcards = True
End With
Selection.Find.Execute
Selection.Find.ClearFormatting
With Selection.Find
.Text = ""
.MatchWildcards = False
End With
Selection.Find.Execute
End Sub


Sub Macro2()
'
' Macro2 Macro
' Macro recorded 9/7/2008 by me
'
Selection.Find.ClearFormatting
With Selection.Find
.Text = "[a-fh][a-h]"
.Forward = True
.Wrap = wdFindContinue
.MatchWildcards = True
End With
Selection.Find.Execute
Selection.Find.ClearFormatting
With Selection.Find
.Text = "g[gh]"
.Forward = True
.Wrap = wdFindContinue
.MatchWildcards = True
End With
Selection.Find.Execute
Selection.Find.ClearFormatting
With Selection.Find
.Text = ""
.MatchWildcards = False
End With
Selection.Find.Execute
End Sub


Their job is to find an erroneous sequence of letters (represented
here with the letters a to h). After creating toolbar buttons for
each of them, they should be used in the following manner. The user
clicks on Macro1 button; if text is found, he should correct it
manually (the correction should be composed of 2 letters only). He
keeps clicking on Macro1 button and making corrections till no more
results are found. After that, he moves to Macro2 button and does the
same thing, making any needed corrections manually. Now my question
is, Is it possible to make Macro2 automatically start running when
Macro1 finds no more results, maybe by merging the 2 macros into one
or anything else?

PS: I had to add the '...Text = "" MatchWildcards = False ...'
part to each macro because it's the only way I found to automatically
uncheck Use Wildcards. The user is supposed to make normal,
non-wildcard searches after using the macros. So the part was added
only to avoid confusing him.

Thanks in advance for your help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top