Strange StoryType Behavior

G

Greg Maxey

Can someone please explain this strange behavior:

Open a new blank document.
Put cursor in the header.

Run the following code:

Sub TestStoryType()
Dim oRng As Range

Set oRng = Selection.Range
oRng.WholeStory
MsgBox oRng.StoryType 'Will correctly display "7"
ValidateParagraphs oRng
MsgBox oRng.StoryType 'Story type has changed to "12" !!!!!????
End Sub
Private Sub ValidateParagraphs(ByRef oRng As Range)
With oRng.Find
.ClearFormatting
.Replacement.ClearFormatting
.Forward = True
.Wrap = wdFindContinue
.Text = "^13"
.Replacement.Text = "^p"
.Execute Replace:=wdReplaceAll
End With
End Sub

The ValidateParagraphs routine will of course insert result in a second
paragraph mark being created. If you run the above proceeds a second type
the msgbox will then report "7" both before and after the ValidateParagraphs
routine. Why is the strorytype changing to 12 on the first run??
 
J

Jezebel

I can't recreate the problem. I tried it with several permutations of header
layout (different first page/odd-even etc) and the second message was always
the same as the first.

StoryType 12 is FootNoteSeparator -- so I tried add a footnote to the
document, but that made no difference either.
 
J

Jean-Guy Marcil

Greg Maxey was telling us:
Greg Maxey nous racontait que :
Can someone please explain this strange behavior:

Open a new blank document.
Put cursor in the header.

Run the following code:

Sub TestStoryType()
Dim oRng As Range

Set oRng = Selection.Range
oRng.WholeStory
MsgBox oRng.StoryType 'Will correctly display "7"
ValidateParagraphs oRng
MsgBox oRng.StoryType 'Story type has changed to "12" !!!!!????
End Sub
Private Sub ValidateParagraphs(ByRef oRng As Range)
With oRng.Find
.ClearFormatting
.Replacement.ClearFormatting
.Forward = True
.Wrap = wdFindContinue
.Text = "^13"
.Replacement.Text = "^p"
.Execute Replace:=wdReplaceAll
End With
End Sub

The ValidateParagraphs routine will of course insert result in a
second paragraph mark being created. If you run the above proceeds a
second type the msgbox will then report "7" both before and after the
ValidateParagraphs routine. Why is the strorytype changing to 12 on
the first run??

I am not sure why, but it only occurs when you start off with a single ¶ in
the header. If you add a space , another ¶ or any other text, it will behave
as expected, even on first runs.

In fact it is not really surprising considering the bugs that are related to
the first ¶ in headers when manipulating the header with VBA...

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
J

Jean-Guy Marcil

Jezebel was telling us:
Jezebel nous racontait que :
I can't recreate the problem. I tried it with several permutations of
header layout (different first page/odd-even etc) and the second
message was always the same as the first.

StoryType 12 is FootNoteSeparator -- so I tried add a footnote to the
document, but that made no difference either.

I got the same thing as Greg on Word 2003 SP1.

What is your version?

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
G

Greg Maxey

Jezebel,

Thanks for trying. Yes I know 12 is a Footnote separator. I don't have any
of those in the document that I was working with either. I just shut down
my computer, restarted, opened a new blank document, copied the code, and
again the first pass the message box displays 7 then 12. If I then continue
to run the code it is 7 and 7. If I go in the header and delete all the
paragraphs but 1 and run again it is back to 7 and 12.

This is freaky!!

I have to have the right StoryType following the ValidateParagraphs
procedure. If you would like to see see what I am working on and how I am
working around this problem you can pull the template file off of this web
page.

http://gregmaxey.mvps.org/Clean_Up_Text.htm
 
G

Greg Maxey

JGM,

Yep I have 2003 SP1 as well. So it is not my unique document or system.
That is good at least.
 
J

Jean-Guy Marcil

Greg Maxey was telling us:
Greg Maxey nous racontait que :
JGM,

Yep I have 2003 SP1 as well. So it is not my unique document or
system. That is good at least.

Yep! That's what we want, consistence in inept programming! ;-)

ps: ...and I am not writing about your programming...

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
J

Jezebel

Fascinating ... the critical point is that the header must be empty when you
run it. For more entertainment, try this --

ValidateParagraphs oRng
oRng.Select
Selection.TypeText "HERE WE ARE...."
MsgBox oRng.StoryType

You'll see no change. But now, add a footnote to your document ....

And if you run it when your document has a footnote, the separator is indeed
selected, even in Print display mode -- which is interesting, because you
can't normally select the separator directly if you're in Print mode.
 
G

Greg Maxey

ps: ...and I am not writing about your programming...

You could be :). If not my programming, then certainly my spelling. I
have got to pay more attention and connect my independent fingers with my
brain.
 
G

Greg Maxey

Jezebel,

I take it that you are now been able to replicate the anomaly. Really
weird isn't it ? I worked around the resulting problem by setting a
variable to the value StoryType before the first pass to ValidateParagraphs
and then used that variable later in my routine.
 
K

Klaus Linke

J

Jezebel

The first time I tried it, the header contained some text, and the behaviour
doesn't appear.
 
G

Greg Maxey

Klaus,

Yes it appears that several heads have been scratched over this one :). I
thought that it was only happening because I was making a change to the
empty header or footer (the same thing happens in the footer except 9 and 12
is returned). Looking at your code apparently the mystery runs deeper.
 
G

Greg Maxey

Jezebel,

Yes you are right. Like JGM mentioned any text in the header or even a
second PM and you won't see the problem. The same thing happens in the
footer as well except 9 and 12 occurs. I haven't checked with first page,
odd or even headers and footers, but I suspect they would be problematic as
well :-(
 
J

Jezebel

It appears with first page headers and footers. But not with other
storyranges as far as I've tested -- empty footnote separator, empty textbox
..... hmmm
 
K

Klaus Linke

Jezebel said:
It appears with first page headers and footers. But not with other
storyranges as far as I've tested -- empty footnote separator, empty
textbox .... hmmm

You wouldn't notice if it happened with the footnote separator, since that's the StoryRange it jumps to.
If you search in the footnote continuation separator story, endnote separator, or endnote continuation separator, you end up in the footnote separator, too.

The story range you're searching doesn't need to be empty. For the bug to occur, the text you are searching must be equal to the range's text. So if your stoty range contains "abcdef¶", your myRange contains "bc" and you search for myRange.Find.Text="bc", myRange would go to the footnote separator after the replacement has been made.

Pretty weird!
Klaus
 
G

Greg Maxey

Klaus,

I don't seem to be seeing the same thing here.

You are right, the range doesn't have to be empty. I have abcde in the
header.

If I run the following I get the odd behaviour:

Option Explicit
Sub TestStoryType()
Dim oRng As Range

Set oRng = Selection.Range
oRng.WholeStory
MsgBox oRng.StoryType
ValidateParagraphs oRng
MsgBox oRng.StoryType
End Sub
Private Sub ValidateParagraphs(ByRef oRng As Range)
With oRng.Find
.ClearFormatting
.Replacement.ClearFormatting
.Forward = True
.Wrap = wdFindContinue
.Text = "abcde^13"
.Replacement.Text = "aaaaa"
.Execute Replace:=wdReplaceAll
End With
End Sub

I use just .Text = "abcde" without the ^13 then I get the expected
replacement and the storytype returned remains 7. It seems that if you the
..Find range "equals" the storyrange (or if you are finding and replacing the
entire range) then the odd behaviour occurs.
 
G

Greg Maxey

Klaus,

Disregard. I just saw your post in the PNG and now I see what you mean
about the the ranges being equal.
 
K

Klaus Linke

Sorry, should have posted it here, too.

~~~~~~~~~~~~~~~~~~~~~
If you use myRange.Find.Execute Replace:=wdReplaceAll in some story ranges, myRange ends up in the footnote separator.
This happens for the main header story (7), main footer story (9), footnote continuation separator (13), endnote separator (15), endnote continuation separator (16).
For the problem to occur, the text you are searching for needs to be the same as the text of the range you are searching in.
See demo code below.

Is this a known issue?

Greetings,
Klaus


' Demo code:
' select some text in one of the affected story ranges first
Dim myRange As Range
Dim str As String
Set myRange = Selection.Range.Duplicate
str = myRange.Text
If str = "" Then
MsgBox "No text selected... Macro sets myRange to the next character"
myRange.MoveEnd Unit:=wdCharacter, Count:=1
str = myRange.Text
End If
With myRange.Find
.ClearFormatting
.Text = str
.Replacement.Text = "^&"
.Forward = True
.Wrap = wdFindStop
' other .Wrap settings also lead to the bug
End With
myRange.Find.Execute Replace:=wdReplaceAll
' I don't get what happens with wdReplaceOne:
' Nothing seems to get replaced -- another bug?
If myRange.StoryType = 12 Then
MsgBox "myRange now is in the footnote separator"
End If
 

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