help with story ranges needed

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
 
H

Helmut Weber

Hi Andrea,

you may check the type of the storyrange.

Sub test00001()
Dim rngStory As Range
For Each rngStory In ActiveDocument.StoryRanges
Debug.Print rngStory.StoryType
Next
End Sub

But there seems to be much confusion in this.

The return values are:
1
7
12
13
15
16

after I did nothing else but insert a primaryheaderfooter.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
G

Greg Maxey

You can evaluate the storytype using a Select statement.

Sub Misspell(FindText As String, ReplaceText As String, _
bMatchCase As Boolean)
Application.ScreenUpdating = False
Dim rngStory As Range
For Each rngStory In ActiveDocument.StoryRanges
Select Case rngStory.StoryType
Case Is = 1, 3
MsgBox "Do your deed here."
Case Else
'Do nothing
End Select
Next rngStory
End Sub

1 corresponds to wdmaintext, 3 to endnotes story

Word2003 has a few other funky storytypes associated with endnotes:
15, 16, and 17 that you may need to include.
 
A

andreacarini

Greg -

You really made my day.

it works seamlessly now, thanks a bunch.

If I may, i would be grateful for another help: would you be able to
point me to a website or book to learn more about VBA and macros. I
know it's something that you learn by doing but have some references
wouldn't be that bad

thanks again

Andrea
 

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