PDA

View Full Version : Solved: Question on StoryType Case



Ice-Tea-Jan
09-04-2010, 09:13 PM
Hello,

I have a very general (newbie) understanding of a document’s 11 stories.

However, the code “Case 6, 7, 8, 9, 10, 11” in the sub below confuses me because it seems to exclude the stories below 6.

Is the code executing only if text boxes are in StoryType “Case 6, 7, 8, 9, 10, 11”?

If so, why?

Is it because all the stories (5 and below) are automatically considered “ActiveDocument” story ranges anyway?


Sub myUpdateFields()
Dim rngStory As Word.Range
Dim oShp As Word.Shape


For Each rngStory In ActiveDocument.StoryRanges
Do
On Error Resume Next
rngStory.Fields.Update

Select Case rngStory.StoryType
Case 6, 7, 8, 9, 10, 11
If rngStory.ShapeRange.Count > 0 Then

For Each oShp In rngStory.ShapeRange
If oShp.TextFrame.HasText Then
oShp.TextFrame.TextRange.Fields.Update
End If
Next oShp
End If

Case Else
'Do Nothing
End Select

On Error GoTo 0

'Get next linked story (if any)
Set rngStory = rngStory.NextStoryRange
Loop Until rngStory Is Nothing
Next rngStory
End Sub


As always, thanks for any assistance!
Janet

Edited 9-Sep-10 by geekgirlau. Reason: insert vba tags

gmaxey
09-04-2010, 10:05 PM
Ice-Tea-Jan,

I haven't checked lately but I believe that there are up to 17 storytypes in some Word versions.

This line of code which comes before the select case statement is doing the bulk of the work:

rngStory.Fields.Update

It updates the fields in each storyrange.

Storyranges 6-11 are the header and footer stories. For whatever reason if these storyranges contain one or more shapes (could be textboxes or any other shape) that contain a field these fields are not updated with the statement above and each shapes shaperange, textframe, textrange must be process individually.

I hope that clears it up.


Sub myUpdateFields()
Dim rngStory As Word.Range
Dim oShp As Word.Shape
For Each rngStory In ActiveDocument.StoryRanges
Do
On Error Resume Next
rngStory.Fields.Update 'This update all fields in storyranges 1 - 5, 12 - 17 _
and the fields in the 6 header/footer text ranges
Select Case rngStory.StoryType
Case 6, 7, 8, 9, 10, 11
'If the header or footer storyrange has one or more shapes then the shapeRange _
TextFrame.TextRange has to be checked individually.
If rngStory.ShapeRange.Count > 0 Then
For Each oShp In rngStory.ShapeRange
If oShp.TextFrame.HasText Then
oShp.TextFrame.TextRange.Fields.Update
End If
Next oShp
End If
Case Else
'Do Nothing
End Select
On Error GoTo 0
'Get next linked story (if any)
Set rngStory = rngStory.NextStoryRange
Loop Until rngStory Is Nothing
Next rngStory
End Sub

Paul_Hossler
09-05-2010, 05:14 PM
As Greg says, in 2007 and 2010 there are 17 different types of stories that can be part of a document,



' wdMainTextStory
' wdTextFrameStory
' wdPrimaryFooterStory, wdFirstPageFooterStory , wdEvenPagesFooterStory
' wdPrimaryHeaderStory, wdFirstPageHeaderStory, wdEvenPagesHeaderStory
' wdFootnotesStory, wdFootnoteContinuationNoticeStory, wdFootnoteContinuationSeparatorStory, wdFootnoteSeparatorStory
' wdCommentsStory
' wdEndnotesStory, wdEndnoteContinuationNoticeStory, wdEndnoteContinuationSeparatorStory, wdEndnoteSeparatorStory


but depending on what you're doing, some code will only act upon the first story for each story type in the document. (The first Header, the first Text Box, and so on). If your document contains sections with un-linked headers and footers in them, or, for example, contains more than one Text Box, the code might not act upon the second and subsequent occurrences of each type of story. So to make sure that the code does act on each occurrence of the text, no matter where it appears, you have to make use of the NextStoryRange method to loop within each type of Story

http://word.mvps.org/FAQs/Customization/ReplaceAnywhere.htm

has a good write up on NextStoryRange

Paul

Ice-Tea-Jan
09-06-2010, 07:43 PM
Greg & Paul,

Thanks to both of you for your quick replies, and for the clarification on the amount of stories.

Greg:
I hope Hurricane Earl did not strike your area! :(
Thank you for putting notes directly into the VB code area for clarity – that was very helpful.

Please tell me why the header/footers story ranges are handled differently than other story layers (for shapes)?


Paul:
Thanks for the “heads up” reminder on the NextStoryRange method. I remember reading somewhere about how it tends to execute on the first story for each story type, and that it must be handled with care (looped) to get all instances.

I saw the “FootnoteSeparatorStory” somewhere in my travels, but I’ll be darned if I know what it is. Can you give me an example? :doh:

Janet

gmaxey
09-06-2010, 08:03 PM
Janet,

The reason they are handled differently is if not handled that way they wouldn't be picked up and processed. I don't know if it a bug (though I suspect it is).

You can easily test it yourself. Bookmark a bit text in the main text story. Create a few REF fields in the several of the various storyranges (e.g., header, footer, textbox in mainstory, comments etc.). Also add a textbox with a REF field in the header or footer.

STET out the entire Select Case statement and run the code. You will see that the Textbox field in the header or footer is not updated.

Paul_Hossler
09-07-2010, 05:04 PM
Janet -- the FootNoteSeperator story 'appears to be' the line seperating Footnotes from the text on that page

Paul

Ice-Tea-Jan
09-10-2010, 06:37 PM
Geekgirlau:
Thank you for inserting VBA tags onto my first post.

Paul:
The FootNoteSeparator story seemed odd at first since I would consider it to hold only lines.
However, after tinkering with it in Word 2010, I realized YOU CAN place text (and other items) into this story, and I should indeed think of it as a true story layer.

Greg:
I understand: Basically, the Textbox field is updated everywhere else EXCEPT in the header and footers (for whatever reason), and therefore they must be addressed separately.



I’m marking this thread solved, and making a well-deserved donation.

The technical assistance received from this forum is very unique and appreciated.

Thanks to all for your answers.
Janet