wdSelectionNormal with more than one Style?

J

Jon Borel

Hi,

I have the following Word 2003 code to check the current cursor insertion
point (or selection) for the style. If the style is "Bold Word" then do
something, otherwise, if it is any other style, do something else. This code
runs automatically in the background through an application event procedure
class module.

The problem is if multiple paragraphs (of two or more styles) are selected
at any time (Say a Heading 1 paragraph followed by a Body Text paragraph) the
code errors out: "Object variable or With Block Variable not set." It seems
I need a way to turn off this IF THEN statement if more than one style is
currently selected. Can this be done?

If Selection.Type = wdSelectionIP Or _
Selection.Type = wdSelectionNormal Then

If Selection.Style = ActiveDocument.Styles("Bold Word") Then
Do This
Else
Do This
End If

End If

Thanks,
Jon
 
J

Jay Freedman

Hi Jon,

This is what error handling, using the On Error statement, is for. Try something
like this:

If Selection.Type = wdSelectionIP Or _
Selection.Type = wdSelectionNormal Then

On Error GoTo ErrHdl
If Selection.Style = ActiveDocument.Styles("Bold Word") Then
MsgBox "yes"
Else
MsgBox "no"
End If

Exit Sub

ErrHdl:
If Err.Number = 91 Then
MsgBox "Mixed styles are selected"
Else
MsgBox "Error " & Err.Number & vbCr & Err.Description
End If
End If

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so all
may benefit.
 
J

Jon Borel

Hi Jay,

I'm sorry, I wasn't clear about my problem (and my direction has changed
somewhat). If a user selects two or more paragraphs, I don't want to
generate an error message (it's often necessary to select multiple
paragraphs).

I'm trying to figure out a way to exit the application_event_procedure if
more than one paragraph is selected. [Note: All the app_event_procedure does
is monitor the style of the current selection or IP to toggle custom toobar
buttons on/off.]

Something along the lines of:

If [the total number of paragraphs currently selected is greater than 1] then
Exit Sub
End If

Thanks AGAIN,
Jon
 
J

Jon Borel

Hi again,

After thinking more about it, I'm dealing with character styles, so if even
a single paragraph mark is selected, I don't need to run the
app_event_procedure to toggle the character style button on/off.

So maybe what I need is:

If [any part of the selection includes a Chr(13)] Then
Exit Sub
End If

Jon Borel said:
Hi Jay,

I'm sorry, I wasn't clear about my problem (and my direction has changed
somewhat). If a user selects two or more paragraphs, I don't want to
generate an error message (it's often necessary to select multiple
paragraphs).

I'm trying to figure out a way to exit the application_event_procedure if
more than one paragraph is selected. [Note: All the app_event_procedure does
is monitor the style of the current selection or IP to toggle custom toobar
buttons on/off.]

Something along the lines of:

If [the total number of paragraphs currently selected is greater than 1] then
Exit Sub
End If

Thanks AGAIN,
Jon

Jay Freedman said:
Hi Jon,

This is what error handling, using the On Error statement, is for. Try something
like this:

If Selection.Type = wdSelectionIP Or _
Selection.Type = wdSelectionNormal Then

On Error GoTo ErrHdl
If Selection.Style = ActiveDocument.Styles("Bold Word") Then
MsgBox "yes"
Else
MsgBox "no"
End If

Exit Sub

ErrHdl:
If Err.Number = 91 Then
MsgBox "Mixed styles are selected"
Else
MsgBox "Error " & Err.Number & vbCr & Err.Description
End If
End If

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so all
may benefit.
 
J

Jon Borel

I think I answered my question:

If Selection.Paragraphs.Count > 1 Then
Exit Sub
End If

So far, no errors!

Jon Borel said:
Hi again,

After thinking more about it, I'm dealing with character styles, so if even
a single paragraph mark is selected, I don't need to run the
app_event_procedure to toggle the character style button on/off.

So maybe what I need is:

If [any part of the selection includes a Chr(13)] Then
Exit Sub
End If

Jon Borel said:
Hi Jay,

I'm sorry, I wasn't clear about my problem (and my direction has changed
somewhat). If a user selects two or more paragraphs, I don't want to
generate an error message (it's often necessary to select multiple
paragraphs).

I'm trying to figure out a way to exit the application_event_procedure if
more than one paragraph is selected. [Note: All the app_event_procedure does
is monitor the style of the current selection or IP to toggle custom toobar
buttons on/off.]

Something along the lines of:

If [the total number of paragraphs currently selected is greater than 1] then
Exit Sub
End If

Thanks AGAIN,
Jon

Jay Freedman said:
Hi Jon,

This is what error handling, using the On Error statement, is for. Try something
like this:

If Selection.Type = wdSelectionIP Or _
Selection.Type = wdSelectionNormal Then

On Error GoTo ErrHdl
If Selection.Style = ActiveDocument.Styles("Bold Word") Then
MsgBox "yes"
Else
MsgBox "no"
End If

Exit Sub

ErrHdl:
If Err.Number = 91 Then
MsgBox "Mixed styles are selected"
Else
MsgBox "Error " & Err.Number & vbCr & Err.Description
End If
End If

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so all
may benefit.

On Wed, 31 Dec 2008 13:01:02 -0800, Jon Borel

Hi,

I have the following Word 2003 code to check the current cursor insertion
point (or selection) for the style. If the style is "Bold Word" then do
something, otherwise, if it is any other style, do something else. This code
runs automatically in the background through an application event procedure
class module.

The problem is if multiple paragraphs (of two or more styles) are selected
at any time (Say a Heading 1 paragraph followed by a Body Text paragraph) the
code errors out: "Object variable or With Block Variable not set." It seems
I need a way to turn off this IF THEN statement if more than one style is
currently selected. Can this be done?

If Selection.Type = wdSelectionIP Or _
Selection.Type = wdSelectionNormal Then

If Selection.Style = ActiveDocument.Styles("Bold Word") Then
Do This
Else
Do This
End If

End If

Thanks,
Jon
 
J

Jay Freedman

I'm just glad I was away from the newsgroups and didn't have to watch you
struggle through this. :)

One more thought to chew on, though it may not make a difference in your
final decision: It's certainly possible to apply a character style to two or
more consecutive paragraphs, including their paragraph marks. In that case
my original suggested code would display "yes". So your test for more than
one paragraph may or may not give "proper" behavior -- but the definition of
"proper" in this context is up to you.

Jon said:
I think I answered my question:

If Selection.Paragraphs.Count > 1 Then
Exit Sub
End If

So far, no errors!

Jon Borel said:
Hi again,

After thinking more about it, I'm dealing with character styles, so
if even a single paragraph mark is selected, I don't need to run the
app_event_procedure to toggle the character style button on/off.

So maybe what I need is:

If [any part of the selection includes a Chr(13)] Then
Exit Sub
End If

Jon Borel said:
Hi Jay,

I'm sorry, I wasn't clear about my problem (and my direction has
changed somewhat). If a user selects two or more paragraphs, I
don't want to generate an error message (it's often necessary to
select multiple paragraphs).

I'm trying to figure out a way to exit the
application_event_procedure if more than one paragraph is selected.
[Note: All the app_event_procedure does is monitor the style of the
current selection or IP to toggle custom toobar buttons on/off.]

Something along the lines of:

If [the total number of paragraphs currently selected is greater
than 1] then Exit Sub
End If

Thanks AGAIN,
Jon

:

Hi Jon,

This is what error handling, using the On Error statement, is for.
Try something like this:

If Selection.Type = wdSelectionIP Or _
Selection.Type = wdSelectionNormal Then

On Error GoTo ErrHdl
If Selection.Style = ActiveDocument.Styles("Bold Word") Then
MsgBox "yes"
Else
MsgBox "no"
End If

Exit Sub

ErrHdl:
If Err.Number = 91 Then
MsgBox "Mixed styles are selected"
Else
MsgBox "Error " & Err.Number & vbCr & Err.Description
End If
End If

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.

On Wed, 31 Dec 2008 13:01:02 -0800, Jon Borel

Hi,

I have the following Word 2003 code to check the current cursor
insertion point (or selection) for the style. If the style is
"Bold Word" then do something, otherwise, if it is any other
style, do something else. This code runs automatically in the
background through an application event procedure class module.

The problem is if multiple paragraphs (of two or more styles) are
selected at any time (Say a Heading 1 paragraph followed by a
Body Text paragraph) the code errors out: "Object variable or
With Block Variable not set." It seems I need a way to turn off
this IF THEN statement if more than one style is currently
selected. Can this be done?

If Selection.Type = wdSelectionIP Or _
Selection.Type = wdSelectionNormal Then

If Selection.Style = ActiveDocument.Styles("Bold Word") Then
Do This
Else
Do This
End If

End If

Thanks,
Jon
 

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