A
andreacarini
Hi there I'm trying to create a macro that will find and replace
misspelled words in the main text of a word file (wdMainTextStory) but
also in the endnotes (wdEndnotesStory). this macro will be called from
other sub macros to find and replace different list of words or
"spaces" combinations.
I'm no VBA nor macros wizard. I surfed the web collected some ideas for
the vba script and tested them.
I'm asking for some help I'm looking for someone who could rewrite the
macro in a way that works on those two storyranges only. this due to
speed issue: a macro that run trough all story ranges takes several
minutes to find and replace my set of words.
i think the problem with the current macro stays here :
If story <> wdMainTextStory And story <> wdEndnotesStory Then
Set rngStory = rngStory.NextStoryRange
I basically don't know how to write a piece of code that says "work
only on maintext and endnotes, or alternatively skip all storyranges
that differ (<>) from main text and endnotes.
I also wrote another version that was fully working. My work around was
repeating twice the code with this line to set the proper storyrange to
work on
Set rngStory = ActiveDocument.StoryRanges(wdXXXXStory)
the problem with this is that the code will work only on document that
has both story ranges. sometimes I have document without endnotes and
the macro won't work because can not find the wdendnotestory.
I would be very grateful for some help
here my code:
Sub Misspell(FindText As String, ReplaceText As String, _
bMatchCase As Boolean)
' screen update turned off to improve speed
Application.ScreenUpdating = False
Dim rngStory As Range
'following notes lines shows some code colllected on other macros might
be suefull in this case but seems to be not strictly necessary to run
the macro
'Dim lngJunk As Long
'lngJunk = ActiveDocument.Sections(1).Headers(1).Range.StoryType
'Iterate through all story types in the current document
'Iterate through all linked stories
For Each rngStory In ActiveDocument.StoryRanges
' Work only with the main body and endnotes
story = rngStory
If story <> wdMainTextStory And story <> wdEndnotesStory Then
'Set rngStory = rngStory.NextStoryRange
End If
With rngStory.Find
.Text = FindText
.Replacement.Text = ReplaceText
.Wrap = wdFindContinue
.MatchCase = bMatchCase
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
'Set rngStory = rngStory.NextStoryRange
'Loop Until rngStory Is Nothing
Next rngStory
Application.ScreenUpdating = True
End Sub
Sub errors()
Call Misspell("website", "Web site", True)
Call Misspell("Website", "Web site", True)
Call Misspell("web-site", "Web site", True)
Call Misspell("Web-site", "Web site", True)
Call Misspell("web site", "Web site", True)
Call Misspell("lay-out", "layout", True)
Call Misspell("internet", "Internet", True)
Call Misspell("towards", "toward", True)
Call Misspell(".Net", ".NET", True)
Call Misspell("on-line", "online", True)
Call Misspell("On-line", "Online", True)
Call Misspell(" Online", " online", True)
Call Misspell(" Email", "email", True)
Call Misspell("e-mail", "email", True)
Call Misspell("E-mail", "Email", True)
Call Misspell(" E-mail", " email", True)
Call Misspell("U.K.", "UK", True)
Call Misspell("WIFI", "Wi-Fi", True)
Call Misspell("WI-FI", "Wi-Fi", True)
End Sub
Sub ReplaceEmdash()
Call Misspell(" - ", " - ", True)
Call Misspell(" -- ", " - ", True)
Call Misspell(" --", " - ", True)
Call Misspell(" - ", " - ", True)
Call Misspell("-- ", " - ", True)
End Sub
Sub ReplaceSmartQuotes()
Call Misspell(" """, " "", True)
Call Misspell(""" ", "" ", True)
Call Misspell(".""", "."", True)
'Call Misspell("""*""", """", True)
End Sub
misspelled words in the main text of a word file (wdMainTextStory) but
also in the endnotes (wdEndnotesStory). this macro will be called from
other sub macros to find and replace different list of words or
"spaces" combinations.
I'm no VBA nor macros wizard. I surfed the web collected some ideas for
the vba script and tested them.
I'm asking for some help I'm looking for someone who could rewrite the
macro in a way that works on those two storyranges only. this due to
speed issue: a macro that run trough all story ranges takes several
minutes to find and replace my set of words.
i think the problem with the current macro stays here :
If story <> wdMainTextStory And story <> wdEndnotesStory Then
Set rngStory = rngStory.NextStoryRange
I basically don't know how to write a piece of code that says "work
only on maintext and endnotes, or alternatively skip all storyranges
that differ (<>) from main text and endnotes.
I also wrote another version that was fully working. My work around was
repeating twice the code with this line to set the proper storyrange to
work on
Set rngStory = ActiveDocument.StoryRanges(wdXXXXStory)
the problem with this is that the code will work only on document that
has both story ranges. sometimes I have document without endnotes and
the macro won't work because can not find the wdendnotestory.
I would be very grateful for some help
here my code:
Sub Misspell(FindText As String, ReplaceText As String, _
bMatchCase As Boolean)
' screen update turned off to improve speed
Application.ScreenUpdating = False
Dim rngStory As Range
'following notes lines shows some code colllected on other macros might
be suefull in this case but seems to be not strictly necessary to run
the macro
'Dim lngJunk As Long
'lngJunk = ActiveDocument.Sections(1).Headers(1).Range.StoryType
'Iterate through all story types in the current document
'Iterate through all linked stories
For Each rngStory In ActiveDocument.StoryRanges
' Work only with the main body and endnotes
story = rngStory
If story <> wdMainTextStory And story <> wdEndnotesStory Then
'Set rngStory = rngStory.NextStoryRange
End If
With rngStory.Find
.Text = FindText
.Replacement.Text = ReplaceText
.Wrap = wdFindContinue
.MatchCase = bMatchCase
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
'Set rngStory = rngStory.NextStoryRange
'Loop Until rngStory Is Nothing
Next rngStory
Application.ScreenUpdating = True
End Sub
Sub errors()
Call Misspell("website", "Web site", True)
Call Misspell("Website", "Web site", True)
Call Misspell("web-site", "Web site", True)
Call Misspell("Web-site", "Web site", True)
Call Misspell("web site", "Web site", True)
Call Misspell("lay-out", "layout", True)
Call Misspell("internet", "Internet", True)
Call Misspell("towards", "toward", True)
Call Misspell(".Net", ".NET", True)
Call Misspell("on-line", "online", True)
Call Misspell("On-line", "Online", True)
Call Misspell(" Online", " online", True)
Call Misspell(" Email", "email", True)
Call Misspell("e-mail", "email", True)
Call Misspell("E-mail", "Email", True)
Call Misspell(" E-mail", " email", True)
Call Misspell("U.K.", "UK", True)
Call Misspell("WIFI", "Wi-Fi", True)
Call Misspell("WI-FI", "Wi-Fi", True)
End Sub
Sub ReplaceEmdash()
Call Misspell(" - ", " - ", True)
Call Misspell(" -- ", " - ", True)
Call Misspell(" --", " - ", True)
Call Misspell(" - ", " - ", True)
Call Misspell("-- ", " - ", True)
End Sub
Sub ReplaceSmartQuotes()
Call Misspell(" """, " "", True)
Call Misspell(""" ", "" ", True)
Call Misspell(".""", "."", True)
'Call Misspell("""*""", """", True)
End Sub