Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / September 2006

Tip: Looking for answers? Try searching our database.

help with story ranges needed

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
andreacarini - 12 Sep 2006 17:42 GMT
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
Helmut Weber - 12 Sep 2006 18:10 GMT
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.

Signature

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

Greg Maxey - 12 Sep 2006 18:33 GMT
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.

> 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
[quoted text clipped - 106 lines]
>     'Call Misspell("""*""", """", True)
> End Sub
andreacarini - 12 Sep 2006 19:32 GMT
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

> You can evaluate the storytype using a Select statement.
>
[quoted text clipped - 127 lines]
> >     'Call Misspell("""*""", """", True)
> > End Sub
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.