Link to home
Start Free TrialLog in
Avatar of Cindy Giovanetti
Cindy GiovanettiFlag for United States of America

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.

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

Open in new window

Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

The 'Execute'  code lines are missing for many of the replacement code blocks

( I have edited the question text to put the code inro a snippet window)
Avatar of Cindy Giovanetti

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?
Oh, wait.  Do you mean "Selection.Find.Execute  Replace:=wdReplaceAll"?
Bumping this.  I still need help with this macro code.
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

Open in new window

Paul


Wow. Thank you, Paul!  I will try that in the morning.
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?  

  1. Sub stripoutjunkfrommachinetranscription()
  2. '
  3. ' stripoutjunkfrommachinetranscription Macro
  4. '
  5. '
  6.    Selection.WholeStory
  7.    Selection.Style = ActiveDocument.Styles("Normal")
  8.    CommandBars("Styles").Visible = False
  9.    Selection.Font.Reset
  10. 'FindAndReplaceMultiItems()
  11.  Dim strOldText As String
  12.  Dim strNewText As String
  13.  Dim lEachWord As Long
  14.  Application.ScreenUpdating = False
  15.  ' List of items to be replaced
  16.  strOldText = "gonna,gotta,sorta,kinda,wanna,vs,cuz,ya,"
  17.  strNewText = "going to,got to,sort of,kind of,want to,versus,because,you,"
  18.  lEachWord = UBound(Split(strOldText, ","))
  19.  ' Loop to find and replace each word
  20.  For lEachWord = 0 To lEachWord
  21.    With Selection
  22.      .HomeKey Unit:=wdStory
  23.      With Selection.Find
  24.        .ClearFormatting
  25.        .Replacement.ClearFormatting
  26.        .Text = Split(strOldText, ",")(lEachWord)
  27.        .Replacement.Text = Split(strNewText, ",")(lEachWord)
  28.        .Format = False
  29.        .MatchWholeWord = False
  30.      End With
  31.    Selection.Find.Execute Replace:=wdReplaceAll
  32.  End With
  33. Next lEachWord
  34.  Application.ScreenUpdating = True
  35. End Sub

Can you provide a paragraph where this is not working? I ran your code against your initial post and everything converted.

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
Oh wait!  I know what's wrong!  It's because "gonna" is capitalized!
Some of the replacements will be case sensitive, and some will not.  Can you help me with the difference?
ASKER CERTIFIED SOLUTION
Avatar of Flyster
Flyster
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I want to thank Flyster for working with me on this.  Very much appreciated!