When does Selection.StoryType fail given a valid Selection?

L

Legal Beagle 2

I have a VBA macro running in Word 2003 on XP. One of my users is seeing this:

Template error 0, App_WindowSelectionChange method 'StoryType' of object
'Selection' failed

The line in question is:

/===========
57620 If Selection.StoryType = wdMainTextStory Then
\===========

I know that there is a Selection and that it is valid because there are
numerous references to Selection in the preceding code. A few examples:

/===========
57530 numMarks = Selection.Bookmarks.Count
57550 Set mark = Selection.Bookmarks(i)
57590 BMArray(LastBMCount) = Selection.Bookmarks(Index).Name
\===========

Under what conditions would Selection.StoryType fail given that there is a
valid Selection?

Thank you.
 
P

Pesach Shelnitz

In every document, there is always one and only one valid Selection object
that represents the insertion point or selected text. Its StoryType property
is a read-only property that can hold one of the following values, depending
on the location of the insertion point (cursor) or selection in your doc:

wdCommentsStory
wdEndnotesStory
wdEvenPagesFooterStory
wdEvenPagesHeaderStory
wdFirstPageFooterStory
wdFirstPageHeaderStory
wdFootnotesStory
wdMainTextStory
wdPrimaryFooterStory
wdPrimaryHeaderStory
wdTextFrameStory

From the names of these values, you can see the locations where the
StoryType property will not have the value wdMainTextStory. The line that you
suspect to be the problem contains a condition that tests whether the
StoryType property holds the value wdMainTextStory. If the result is False,
you shouldn't get this error. This error may be generated by a line that
attempts to change the StoryType property.
 
T

Tony Jollans

This is not a standard VBA error message - and, FWIW, StoryType is a
Property, not a Method - and is apparently reporting an error code of 0.

It looks to me like it might be an error in the error handling in the
WindowSelectionChange event code.
 
L

Legal Beagle 2

Thank you for your response. You are correct that this is not a standard VBA
error message. I oversimplified the question. The relevant code is:

/=========
Private Sub App_WindowSelectionChange(ByVal Sel As Selection)
...
57180 On Error GoTo ErrorHandler
...
57620 If Selection.StoryType = wdMainTextStory Then
...
ErrorHandler:
59000 MsgBox prompt:="Template error " & Err.Number & Err.Source & " line
" & Erl() & _
" " & "App_WindowSelectionChange" & " " & Err.Description
\=========

This produces:

Template error 0 line 57620 AppWindow_SelectionChange Method 'StoryType' of
object 'Selection' failed :

I'm seeing approximately the same error (same message, different line
number) in a few other places also. As with this case, Selection has been
successfully used in a nearby line but fails when StoryType is used.
 
M

Manfred F

Legal Beagle 2 said:
Thank you for your response. You are correct that this is not a standard VBA
error message. I oversimplified the question. The relevant code is:

/=========
Private Sub App_WindowSelectionChange(ByVal Sel As Selection)
...
57180 On Error GoTo ErrorHandler
...
57620 If Selection.StoryType = wdMainTextStory Then
...
ErrorHandler:
59000 MsgBox prompt:="Template error " & Err.Number & Err.Source & " line
" & Erl() & _
" " & "App_WindowSelectionChange" & " " & Err.Description
\=========

I can't explain your error, either. But one thing looks suspicious in your
code: If you want to access the (context-) selection in
WindowSelectionChange, you _should_ use the parameter "sel" handed in and not
grab it somewhere else. Maybe this would prevent the error from showing up?

Regards,
Manfred
 
L

Legal Beagle 2

Ugh, I must be an idiot! Even if that's not the problem, using the Sel
parameter is clearly better than doing what I'm doing.

Thank you, I will try that.

- Mike
 
M

Manfred F

There's one more thing about the WindowSelectionChange event, that you have
to know:
In W2002 and W2003, under certain conditions it may happen, that it stops
firing (this has been discussed in this forum before). So you can't rely too
much on it..

Regards,

Manfred
 
L

Legal Beagle 2

Wow! That is most unwelcome behavior. Thank you for the pointer. I will
search the old forum posts to see if they contain information about why/when
it stops firing, how to detect that it has happened, and what I can do to
start it firing again.

Thank you again for taking the time to answer my initial question and give
me this very useful follow-up information.

Very best regards,

Mike
 

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