Cindy Giovanetti
asked on
Why does my macro not work?
I have a fairly long Word macro. Parts of it work; parts of it don't.
What works: It applies Normal style to the entire document. It applies plain text to the entire document. It removes punctuation.
What doesn't work: It is supposed to replace gonna with going to, gotta with got to, sorta with sort of, kinda with kind of, wanna with want to, vs with versus, cuz with because, ya with you, and ad with A.D.
I created this macro by recording all these search-and-replaces, not by programming it.
Would somebody kindly look at the code and tell me why the replacements don't work? Thanks in advance.
What works: It applies Normal style to the entire document. It applies plain text to the entire document. It removes punctuation.
What doesn't work: It is supposed to replace gonna with going to, gotta with got to, sorta with sort of, kinda with kind of, wanna with want to, vs with versus, cuz with because, ya with you, and ad with A.D.
I created this macro by recording all these search-and-replaces, not by programming it.
Would somebody kindly look at the code and tell me why the replacements don't work? Thanks in advance.
Sub stripoutjunkfrommachinetranscription()
'
' stripoutjunkfrommachinetranscription Macro
'
'
Selection.WholeStory
Selection.Style = ActiveDocument.Styles("Normal")
CommandBars("Styles").Visible = False
Selection.Font.Reset
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
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
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
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
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
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "^p"
.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
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "^l^l"
.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
With Selection.Find
.Text = "okay"
.Replacement.Text = "OK"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
With Selection.Find
.Text = "ok"
.Replacement.Text = "OK"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = True
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
With Selection.Find
.Text = "gonna"
.Replacement.Text = "going to"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "gotta"
.Replacement.Text = "got to"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "judgement"
.Replacement.Text = "judgment"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
With Selection.Find
.Text = "judgements"
.Replacement.Text = "judgments"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
With Selection.Find
.Text = "Herman"
.Replacement.Text = "Hermann"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
With Selection.Find
.Text = "Hermannn"
.Replacement.Text = "Hermann"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
With Selection.Find
.Text = "sorta"
.Replacement.Text = "sort of"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
With Selection.Find
.Text = "kinda"
.Replacement.Text = "kind of"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
With Selection.Find
.Text = "wanna"
.Replacement.Text = "want to"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
With Selection.Find
.Text = "vs."
.Replacement.Text = "versus"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
With Selection.Find
.Text = "vs"
.Replacement.Text = "versus"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "cuz"
.Replacement.Text = "because"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
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
With Selection.Find
.Text = "ya"
.Replacement.Text = "you"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
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
With Selection.Find
.Text = "bc"
.Replacement.Text = "B.C."
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
With Selection.Find
.Text = "ad"
.Replacement.Text = "A.D."
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
End Sub
ASKER
Graham, thank you so much. But I don't know what "the 'Execute code lines" mean. Can you give me an example of where it is right?
ASKER
Oh, wait. Do you mean "Selection.Find.Execute Replace:=wdReplaceAll"?
ASKER
Bumping this. I still need help with this macro code.
ASKER
I'm going to close this question because I've changed the macro a lot since I posted the original question. But parts of the macro still don't work, so I'll ask again in a new question tomorrow.
Here's a little different approach to your find and replace solution. This code contains two lists of words. strOldText contains the words you want to replace and strNewText contains the words that will replace them. You can add more words to the lists if needed, just make sure you place them in the same order in each list and separate them with a comma.
Sub FindAndReplaceMultiItems()
Dim strOldText As String
Dim strNewText As String
Dim lEachWord As Long
Application.ScreenUpdating = False
' List of items to be replaced
strOldText = "gonna,gotta,sorta,kinda,wanna,vs,cuz,ya,ad ,"
strNewText = "going to,got to,sort of,kind of,want to,versus,because,you,A.D. ,"
lEachWord = UBound(Split(strOldText, ","))
' Loop to find and replace each word
For lEachWord = 0 To lEachWord
With Selection
.HomeKey Unit:=wdStory
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = Split(strOldText, ",")(lEachWord)
.Replacement.Text = Split(strNewText, ",")(lEachWord)
.Format = False
.MatchWholeWord = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End With
Next lEachWord
Application.ScreenUpdating = True
End Sub
Paul
ASKER
Wow. Thank you, Paul! I will try that in the morning.
ASKER
OK, Paul, I converted my macro to your format. Everything worked but the first item in the string, which is "gonna" to "going to." Can you help me understand why gonna didn't work?
- Sub stripoutjunkfrommachinetra
nscription () - '
- ' stripoutjunkfrommachinetra
nscription Macro - '
- '
- Selection.WholeStory
- Selection.Style = ActiveDocument.Styles("Nor
mal") - CommandBars("Styles").Visi
ble = False - Selection.Font.Reset
- 'FindAndReplaceMultiItems(
) - Dim strOldText As String
- Dim strNewText As String
- Dim lEachWord As Long
- Application.ScreenUpdating
= False - ' List of items to be replaced
- strOldText = "gonna,gotta,sorta,kinda,w
anna,vs,cu z,ya," - strNewText = "going to,got to,sort of,kind of,want to,versus,because,you,"
- lEachWord = UBound(Split(strOldText, ","))
- ' Loop to find and replace each word
- For lEachWord = 0 To lEachWord
- With Selection
- .HomeKey Unit:=wdStory
- With Selection.Find
- .ClearFormatting
- .Replacement.ClearFormatti
ng - .Text = Split(strOldText, ",")(lEachWord)
- .Replacement.Text = Split(strNewText, ",")(lEachWord)
- .Format = False
- .MatchWholeWord = False
- End With
- Selection.Find.Execute Replace:=wdReplaceAll
- End With
- Next lEachWord
- Application.ScreenUpdating
= True - End Sub
Can you provide a paragraph where this is not working? I ran your code against your initial post and everything converted.
ASKER
Yes, thank you. This is just a junk paragraph, and only the last line matters:
Thanks to the efforts of the National Association of Insurance Commissioners (NAIC) families who are left scrambling to find life insurance policy paperwork after the loss of a loved one now have an easier way to go about the process RELATED: How life insurance became a safety net for this family Free life insurance locator service Losing a life insurance policy is a common thing After you’ve checked the closet the dresser the shoebox and the safe… where in the
Gonna gotta sorta kinda wanna vs cuz ya
When I ran this, the last line looks like this:
Gonna got to sort of kind of want to versus because you
Thanks to the efforts of the National Association of Insurance Commissioners (NAIC) families who are left scrambling to find life insurance policy paperwork after the loss of a loved one now have an easier way to go about the process RELATED: How life insurance became a safety net for this family Free life insurance locator service Losing a life insurance policy is a common thing After you’ve checked the closet the dresser the shoebox and the safe… where in the
Gonna gotta sorta kinda wanna vs cuz ya
When I ran this, the last line looks like this:
Gonna got to sort of kind of want to versus because you
ASKER
Oh wait! I know what's wrong! It's because "gonna" is capitalized!
ASKER
Some of the replacements will be case sensitive, and some will not. Can you help me with the difference?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I want to thank Flyster for working with me on this. Very much appreciated!
( I have edited the question text to put the code inro a snippet window)