I'm Back and still need help with word 2007 macro

J

joepayne66

If I am in the wrong forum, please advise.
After getting totally frustrated and not being able to find the
solution for a word macro I am trying to write, I'm back to try again.
Some of you might remember my earlier posts and I want to thank those
of you who tried to help but none of the code accomplished what I was
trying to do. I will try and explain, as best I can, what I am trying
to do. Below is a macro I recorded in Word 2007, which is the version
I am now using, that does the basic thing but I need a little, or
maybe a lot more. Please remember that I am a beginner with only a
basic understanding of writing macros so please explain any replys if
it contains code for me to try.

Sub Macro1()
'
' Macro1 Macro
'
'
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "G"
.Replacement.Text = " "
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub

As you can see, this is simply a find and replace macro. Here is what
I need the help with. I need the code to do the following:

Move the cursor to the beginning of the document so that it searches
the entire document without me telling it to return to the beginning
and continue the search.

Be able to adjust the macro to search for multiple letters, numbers or
symbols in the same procedure. In my macro above I searched for the
letter "G" and replaced it with a blank space. I would like to be able
to adjust the macro code to find and replace any number of characters.
like "G" and "Y" just by adjusting the code.

Finally, after the search and replace has finished I would like for
the macro or another sub routine to remove all blank spaces between
all remaining characters left in the document. The exception being
that it will leave two blank spaces after any puncuation marks,
keeping the sentences seperated.

Maybe I am asking for too much, and if so, I appoligize. This has
frustated me for over two years now. Because of this I plan on, after
retireing this summer, attending school to learn how to do what I am
asking now. Thanks to all. Joe Payne
 
M

meatshield

If I am in the wrong forum, please advise.
After getting totally frustrated and not being able to find the
solution for a word macro I am trying to write, I'm back to try again.
Some of you might remember my earlier posts and I want to thank those
of you who tried to help but none of the code accomplished what I was
trying to do. I will try and explain, as best I can, what I am trying
to do. Below is a macro I recorded in Word 2007, which is the version
I am now using, that does the basic thing but I need a little, or
maybe a lot more. Please remember that I am a beginner with only a
basic understanding of writing macros so please explain any replys if
it contains code for me to try.

Sub Macro1()
'
' Macro1 Macro
'
'
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "G"
.Replacement.Text = " "
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub

As you can see, this is simply a find and replace macro. Here is what
I need the help with. I need the code to do the following:

Move the cursor to the beginning of the document so that it searches
the entire document without me telling it to return to the beginning
and continue the search.

Be able to adjust the macro to search for multiple letters, numbers or
symbols in the same procedure. In my macro above I searched for the
letter "G" and replaced it with a blank space. I would like to be able
to adjust the macro code to find and replace any number of characters.
like "G" and "Y" just by adjusting the code.

Finally, after the search and replace has finished I would like for
the macro or another sub routine to remove all blank spaces between
all remaining characters left in the document. The exception being
that it will leave two blank spaces after any puncuation marks,
keeping the sentences seperated.

Maybe I am asking for too much, and if so, I appoligize. This has
frustated me for over two years now. Because of this I plan on, after
retireing this summer, attending school to learn how to do what I am
asking now. Thanks to all. Joe Payne

Sorry this doesn't look nicer, I don't write many macros for Word and
I'm not used to the objects that it uses. This is using Word 2002, I
hope it works in word 2007.
Sub Replace_strings()

Dim strReplace
Dim i As Integer
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting

strReplace = Array("G", "Y", "q") 'add as many strings as you like
For i = LBound(strReplace) To UBound(strReplace)

Selection.WholeStory

With Selection.Find
.Text = strReplace(i)
.Replacement.Text = "" 'you're just going to get rid of this
space later on
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Next i
End Sub

I changed the line for the replacement text, if you're going to
replace that space with "" later on, you can save a step.

Sub Replace_punctuation()

Dim strReplace
Dim i As Integer
Selection.WholeStory

With Selection.Find
.Text = " "
.Replacement.Text = " "
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll

strReplace = Array("!", ".", "?") 'add as many strings as you like
For i = LBound(strReplace) To UBound(strReplace)

Selection.WholeStory

With Selection.Find
.Text = strReplace(i)
.Replacement.Text = strReplace(i) & " "
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Next i
End Sub

The second one might cause problems if you use the punctuation within
the sentence but it isn't the end of the sentence (For example -
"Hello Mr. So-and-so" will end up as "Hello Mr. So-and-so.)
 

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