Group:  Microsoft Word ยป microsoft.public.word.vba.general
Thread: wdSelectionNormal with more than one Style?

wdSelectionNormal with more than one Style?
Jon Borel 12/31/2008 9:01:02 PM
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
Re: wdSelectionNormal with more than one Style?
Jay Freedman <jay.freedman[ at ]verizon.net> 12/31/2008 10:56:06 PM
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 FAQ: http://word.mvps.org
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
<JonBorel[ at ]discussions.microsoft.com> wrote:

[Quoted Text]
>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

Home | Search | Terms | Imprint
Newsgroups Reader