Group:  Microsoft Word ยป microsoft.public.word.vba.userforms
Thread: Listbox: need help creating one

DotNetBag
.NET Development Newsgroups

HTVi
TV Discussion Newsgroups

Our Hot Pick: Rising Antivirus 2006 - Certified by TUV & Checkmark! Get 10% discount by entering this coupon code: ONDISCOUNT10
Rising Antivirus 2006

Listbox: need help creating one
Michael F 19.09.2006 19:49:02
Hi,

As a technical writer, I dabble with macros and VBA. I created a userform
with a textbox in which you can enter a bookmark name (if you know what it
is) and a command button that selects that bookmark.

How do I code a listbox to present a set of choices? I saw the following in
the forum but there must be more code I'm missing.

ListBox1.List = Array("Apples", "Oranges", "Pears", "Apricots", "Plums")

For the command button, is the following sufficient for it to work?

Private Sub CommandButton1_Click()
ActiveDocument.Bookmarks(ListBox1).Select
UserForm1.hide
End Sub

Thank you.

Michael F.
Re: Listbox: need help creating one
Jay Freedman <jay.freedman[ at ]verizon.net> 20.09.2006 01:11:10
To load the listbox with the names of the bookmarks in the document,
use code in the userform like this:

Private Sub UserForm_Initialize()
Dim bkmk As Bookmark
For Each bkmk In ActiveDocument.Bookmarks
ListBox1.AddItem bkmk.Name
Next
ListBox1.ListIndex = 0
End Sub

For the command button, use something like this:

Private Sub CommandButton1_Click()
ActiveDocument.Bookmarks(ListBox1.List(ListBox1.ListIndex)).Select
Me.Hide
End Sub

That assumes that the macro in the regular module that launches the
userform looks something like this:

Sub DisplayUserForm()
Dim uf As UserForm1
Set uf = New UserForm1
uf.Show
Set uf = Nothing
End Sub

--
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 Tue, 19 Sep 2006 12:49:02 -0700, Michael F
<MichaelF[ at ]discussions.microsoft.com> wrote:

[Quoted Text]
>Hi,
>
>As a technical writer, I dabble with macros and VBA. I created a userform
>with a textbox in which you can enter a bookmark name (if you know what it
>is) and a command button that selects that bookmark.
>
>How do I code a listbox to present a set of choices? I saw the following in
>the forum but there must be more code I'm missing.
>
>ListBox1.List = Array("Apples", "Oranges", "Pears", "Apricots", "Plums")
>
>For the command button, is the following sufficient for it to work?
>
>Private Sub CommandButton1_Click()
>ActiveDocument.Bookmarks(ListBox1).Select
>UserForm1.hide
>End Sub
>
>Thank you.
>
>Michael F.
Re: Listbox: need help creating one
Michael F 20.09.2006 15:23:01
Jay,

I finally got it working so thank you for the code that got me to this
point. I would like to get some other questions cleared up if you don't mind.
Below are the contents of my userform as it now stands.

For the UserForm_Initialize macro, I tried your code (For Each bkmk...) and
it presented two sets of all the user defined bookmarks and did not go to the
selected bookmark. I replaced it with the ListBox1.List array that I found
elsewhere in this forum and that worked after I added your code to command
button. I wanted to present only selected bookmarks in the listbox so as to
limit the choices.

Is Listbox.List a special name for a variable or could it be called
antything else?

I was wondering why do I need a UserForm_Initialize macro? When I created
the listbox, the macro for it was named ListBox1_Click. I thought that was
where you placed the code for the listbox, but it didn't work when I put the
array in that macro. What would you usually place in ListBox1_Click?

For the command button, could you write a sentence that states what the
statement is saying beyond "select a bookmark in the active document..."

Finally, is "me.hide" explained in the online help anywhere? I take it that
"me" is some shorthand for the current userform.

Yours,

Michael F
------------------------------------

Private Sub CommandButton1_Click()
ActiveDocument.Bookmarks(ListBox1.List(ListBox1.ListIndex)).Select
Me.Hide

'ActiveDocument.Bookmarks(ListBox1.List).Select
'UserForm1.Hide

End Sub

Private Sub Label1_Click()
'label for the userform
End Sub

Private Sub ListBox1_Click()

End Sub

Private Sub UserForm_Initialize()

ListBox1.List = Array("chapter1", "chapter2", "chapter3", "chapter4", _
"chapter5", "chapter6", "chapter7", "chapter8", "chapter9", "chapter10", _
"appendixA", "appendixB")

' Dim bkmk As Bookmark
' For Each bkmk In ActiveDocument.Bookmarks
' ListBox1.AddItem bkmk.Name
' Next
' ListBox1.ListIndex = 0

End Sub

Private Sub UserForm_Click()

End Sub
---------------------------------------------------------------

"Jay Freedman" wrote:

[Quoted Text]
> To load the listbox with the names of the bookmarks in the document,
> use code in the userform like this:
>
> Private Sub UserForm_Initialize()
> Dim bkmk As Bookmark
> For Each bkmk In ActiveDocument.Bookmarks
> ListBox1.AddItem bkmk.Name
> Next
> ListBox1.ListIndex = 0
> End Sub
>
> For the command button, use something like this:
>
> Private Sub CommandButton1_Click()
> ActiveDocument.Bookmarks(ListBox1.List(ListBox1.ListIndex)).Select
> Me.Hide
> End Sub
>
> That assumes that the macro in the regular module that launches the
> userform looks something like this:
>
> Sub DisplayUserForm()
> Dim uf As UserForm1
> Set uf = New UserForm1
> uf.Show
> Set uf = Nothing
> End Sub
>
> --
> 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 Tue, 19 Sep 2006 12:49:02 -0700, Michael F
> <MichaelF[ at ]discussions.microsoft.com> wrote:
>
> >Hi,
> >
> >As a technical writer, I dabble with macros and VBA. I created a userform
> >with a textbox in which you can enter a bookmark name (if you know what it
> >is) and a command button that selects that bookmark.
> >
> >How do I code a listbox to present a set of choices? I saw the following in
> >the forum but there must be more code I'm missing.
> >
> >ListBox1.List = Array("Apples", "Oranges", "Pears", "Apricots", "Plums")
> >
> >For the command button, is the following sufficient for it to work?
> >
> >Private Sub CommandButton1_Click()
> >ActiveDocument.Bookmarks(ListBox1).Select
> >UserForm1.hide
> >End Sub
> >
> >Thank you.
> >
> >Michael F.
>
Re: Listbox: need help creating one
"Jay Freedman" <jay.freedman[ at ]verizon.net> 20.09.2006 16:55:53
Hi Michael,

1. I don't know what would have caused two copies of the bookmark names to
appear in the list -- I'd have to see the code of the userform as it was
when that happened.

2. Yes, ListBox1.List is a special name for the collection of items shown in
the box. There are two ways to put items into the list: either add them
one-by-one with the AddItem method (my code) or assign an array to the .List
property (your code). You wouldn't use both methods in the same code. (Maybe
that's how you got two copies?) If you want only selected bookmark names in
the list, the array method is easier to program.

3. Each of the subroutines in the userform is what's known as an "event
handler"; that is, when something happens, VBA automatically calls the code
that handles that something (event). The name of the subroutine tells you
which event it handles. The Userform_Initialize event handler gets called
when the userform is starting up but hasn't been displayed yet. That's the
time to load the list into the list box.

The ListBox1_Click event handler gets called when the user clicks somewhere
inside the list box, but you want the list to be in place already before
that could happen. You might use that click event to load data into another
box in the userform, or to enable/disable a command button, or something
else like that.

4. Analyze the statement

ActiveDocument.Bookmarks(ListBox1.List(ListBox1.ListIndex)).Select

starting with the innermost pair of parentheses. The property
ListBox1.ListIndex is the number of the currently selected item in the list,
starting with 0 at the top of the list and increasing as you go downward.
Taking your example's array of items, let's say the user clicked on the
bookmark name "chapter2" and then clicked the OK button. At that point,
ListBox1.ListIndex is equal to 1.

The expression ListBox1.List(ListBox1.ListIndex) means "find the entry in
the list whose number is ListBox1.ListIndex, and return the string value you
find there". In the example, when ListBox1.ListIndex is equal to 1, that
value is the string "chapter2".

Now that string is used to look in the collection ActiveDocument.Bookmarks,
find the bookmark whose name is "chapter2", and select it.

5. Yes, in a userform the term Me refers to the current userform. That's
especially useful if you're in the business of writing generic userforms
that can be called for various purposes from various other places. It's good
practice to use "Me" instead of the UserForm1 name, because the current
userform may not be named UserForm1 at run time.

--
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.

Michael F wrote:
[Quoted Text]
> Jay,
>
> I finally got it working so thank you for the code that got me to this
> point. I would like to get some other questions cleared up if you
> don't mind. Below are the contents of my userform as it now stands.
>
> For the UserForm_Initialize macro, I tried your code (For Each
> bkmk...) and it presented two sets of all the user defined bookmarks
> and did not go to the selected bookmark. I replaced it with the
> ListBox1.List array that I found elsewhere in this forum and that
> worked after I added your code to command button. I wanted to
> present only selected bookmarks in the listbox so as to limit the
> choices.
>
> Is Listbox.List a special name for a variable or could it be called
> antything else?
>
> I was wondering why do I need a UserForm_Initialize macro? When I
> created the listbox, the macro for it was named ListBox1_Click. I
> thought that was where you placed the code for the listbox, but it
> didn't work when I put the array in that macro. What would you
> usually place in ListBox1_Click?
>
> For the command button, could you write a sentence that states what
> the statement is saying beyond "select a bookmark in the active
> document..."
>
> Finally, is "me.hide" explained in the online help anywhere? I take
> it that "me" is some shorthand for the current userform.
>
> Yours,
>
> Michael F
> ------------------------------------
>
> Private Sub CommandButton1_Click()
> ActiveDocument.Bookmarks(ListBox1.List(ListBox1.ListIndex)).Select
> Me.Hide
>
> 'ActiveDocument.Bookmarks(ListBox1.List).Select
> 'UserForm1.Hide
>
> End Sub
>
> Private Sub Label1_Click()
> 'label for the userform
> End Sub
>
> Private Sub ListBox1_Click()
>
> End Sub
>
> Private Sub UserForm_Initialize()
>
> ListBox1.List = Array("chapter1", "chapter2", "chapter3", "chapter4",
> _ "chapter5", "chapter6", "chapter7", "chapter8", "chapter9",
> "chapter10", _ "appendixA", "appendixB")
>
> ' Dim bkmk As Bookmark
> ' For Each bkmk In ActiveDocument.Bookmarks
> ' ListBox1.AddItem bkmk.Name
> ' Next
> ' ListBox1.ListIndex = 0
>
> End Sub
>
> Private Sub UserForm_Click()
>
> End Sub
> ---------------------------------------------------------------
>
> "Jay Freedman" wrote:
>
>> To load the listbox with the names of the bookmarks in the document,
>> use code in the userform like this:
>>
>> Private Sub UserForm_Initialize()
>> Dim bkmk As Bookmark
>> For Each bkmk In ActiveDocument.Bookmarks
>> ListBox1.AddItem bkmk.Name
>> Next
>> ListBox1.ListIndex = 0
>> End Sub
>>
>> For the command button, use something like this:
>>
>> Private Sub CommandButton1_Click()
>> ActiveDocument.Bookmarks(ListBox1.List(ListBox1.ListIndex)).Select
>> Me.Hide
>> End Sub
>>
>> That assumes that the macro in the regular module that launches the
>> userform looks something like this:
>>
>> Sub DisplayUserForm()
>> Dim uf As UserForm1
>> Set uf = New UserForm1
>> uf.Show
>> Set uf = Nothing
>> End Sub
>>
>> --
>> 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 Tue, 19 Sep 2006 12:49:02 -0700, Michael F
>> <MichaelF[ at ]discussions.microsoft.com> wrote:
>>
>>> Hi,
>>>
>>> As a technical writer, I dabble with macros and VBA. I created a
>>> userform with a textbox in which you can enter a bookmark name (if
>>> you know what it is) and a command button that selects that
>>> bookmark.
>>>
>>> How do I code a listbox to present a set of choices? I saw the
>>> following in the forum but there must be more code I'm missing.
>>>
>>> ListBox1.List = Array("Apples", "Oranges", "Pears", "Apricots",
>>> "Plums")
>>>
>>> For the command button, is the following sufficient for it to work?
>>>
>>> Private Sub CommandButton1_Click()
>>> ActiveDocument.Bookmarks(ListBox1).Select
>>> UserForm1.hide
>>> End Sub
>>>
>>> Thank you.
>>>
>>> Michael F.


Re: Listbox: need help creating one
Michael F 20.09.2006 18:22:02
Jay,

That was extremely helpful, especially the explanation of the ActiveDocument
statement. At this point, I feel confident that I can create a userform using
either listbox or textbox.

Thanks.

Michael F.


"Jay Freedman" wrote:

[Quoted Text]
> Hi Michael,
>
> 1. I don't know what would have caused two copies of the bookmark names to
> appear in the list -- I'd have to see the code of the userform as it was
> when that happened.
>
> 2. Yes, ListBox1.List is a special name for the collection of items shown in
> the box. There are two ways to put items into the list: either add them
> one-by-one with the AddItem method (my code) or assign an array to the .List
> property (your code). You wouldn't use both methods in the same code. (Maybe
> that's how you got two copies?) If you want only selected bookmark names in
> the list, the array method is easier to program.
>
> 3. Each of the subroutines in the userform is what's known as an "event
> handler"; that is, when something happens, VBA automatically calls the code
> that handles that something (event). The name of the subroutine tells you
> which event it handles. The Userform_Initialize event handler gets called
> when the userform is starting up but hasn't been displayed yet. That's the
> time to load the list into the list box.
>
> The ListBox1_Click event handler gets called when the user clicks somewhere
> inside the list box, but you want the list to be in place already before
> that could happen. You might use that click event to load data into another
> box in the userform, or to enable/disable a command button, or something
> else like that.
>
> 4. Analyze the statement
>
> ActiveDocument.Bookmarks(ListBox1.List(ListBox1.ListIndex)).Select
>
> starting with the innermost pair of parentheses. The property
> ListBox1.ListIndex is the number of the currently selected item in the list,
> starting with 0 at the top of the list and increasing as you go downward.
> Taking your example's array of items, let's say the user clicked on the
> bookmark name "chapter2" and then clicked the OK button. At that point,
> ListBox1.ListIndex is equal to 1.
>
> The expression ListBox1.List(ListBox1.ListIndex) means "find the entry in
> the list whose number is ListBox1.ListIndex, and return the string value you
> find there". In the example, when ListBox1.ListIndex is equal to 1, that
> value is the string "chapter2".
>
> Now that string is used to look in the collection ActiveDocument.Bookmarks,
> find the bookmark whose name is "chapter2", and select it.
>
> 5. Yes, in a userform the term Me refers to the current userform. That's
> especially useful if you're in the business of writing generic userforms
> that can be called for various purposes from various other places. It's good
> practice to use "Me" instead of the UserForm1 name, because the current
> userform may not be named UserForm1 at run time.
>
> --
> 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.
>
> Michael F wrote:
> > Jay,
> >
> > I finally got it working so thank you for the code that got me to this
> > point. I would like to get some other questions cleared up if you
> > don't mind. Below are the contents of my userform as it now stands.
> >
> > For the UserForm_Initialize macro, I tried your code (For Each
> > bkmk...) and it presented two sets of all the user defined bookmarks
> > and did not go to the selected bookmark. I replaced it with the
> > ListBox1.List array that I found elsewhere in this forum and that
> > worked after I added your code to command button. I wanted to
> > present only selected bookmarks in the listbox so as to limit the
> > choices.
> >
> > Is Listbox.List a special name for a variable or could it be called
> > antything else?
> >
> > I was wondering why do I need a UserForm_Initialize macro? When I
> > created the listbox, the macro for it was named ListBox1_Click. I
> > thought that was where you placed the code for the listbox, but it
> > didn't work when I put the array in that macro. What would you
> > usually place in ListBox1_Click?
> >
> > For the command button, could you write a sentence that states what
> > the statement is saying beyond "select a bookmark in the active
> > document..."
> >
> > Finally, is "me.hide" explained in the online help anywhere? I take
> > it that "me" is some shorthand for the current userform.
> >
> > Yours,
> >
> > Michael F
> > ------------------------------------
> >
> > Private Sub CommandButton1_Click()
> > ActiveDocument.Bookmarks(ListBox1.List(ListBox1.ListIndex)).Select
> > Me.Hide
> >
> > 'ActiveDocument.Bookmarks(ListBox1.List).Select
> > 'UserForm1.Hide
> >
> > End Sub
> >
> > Private Sub Label1_Click()
> > 'label for the userform
> > End Sub
> >
> > Private Sub ListBox1_Click()
> >
> > End Sub
> >
> > Private Sub UserForm_Initialize()
> >
> > ListBox1.List = Array("chapter1", "chapter2", "chapter3", "chapter4",
> > _ "chapter5", "chapter6", "chapter7", "chapter8", "chapter9",
> > "chapter10", _ "appendixA", "appendixB")
> >
> > ' Dim bkmk As Bookmark
> > ' For Each bkmk In ActiveDocument.Bookmarks
> > ' ListBox1.AddItem bkmk.Name
> > ' Next
> > ' ListBox1.ListIndex = 0
> >
> > End Sub
> >
> > Private Sub UserForm_Click()
> >
> > End Sub
> > ---------------------------------------------------------------
> >
> > "Jay Freedman" wrote:
> >
> >> To load the listbox with the names of the bookmarks in the document,
> >> use code in the userform like this:
> >>
> >> Private Sub UserForm_Initialize()
> >> Dim bkmk As Bookmark
> >> For Each bkmk In ActiveDocument.Bookmarks
> >> ListBox1.AddItem bkmk.Name
> >> Next
> >> ListBox1.ListIndex = 0
> >> End Sub
> >>
> >> For the command button, use something like this:
> >>
> >> Private Sub CommandButton1_Click()
> >> ActiveDocument.Bookmarks(ListBox1.List(ListBox1.ListIndex)).Select
> >> Me.Hide
> >> End Sub
> >>
> >> That assumes that the macro in the regular module that launches the
> >> userform looks something like this:
> >>
> >> Sub DisplayUserForm()
> >> Dim uf As UserForm1
> >> Set uf = New UserForm1
> >> uf.Show
> >> Set uf = Nothing
> >> End Sub
> >>
> >> --
> >> 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 Tue, 19 Sep 2006 12:49:02 -0700, Michael F
> >> <MichaelF[ at ]discussions.microsoft.com> wrote:
> >>
> >>> Hi,
> >>>
> >>> As a technical writer, I dabble with macros and VBA. I created a
> >>> userform with a textbox in which you can enter a bookmark name (if
> >>> you know what it is) and a command button that selects that
> >>> bookmark.
> >>>
> >>> How do I code a listbox to present a set of choices? I saw the
> >>> following in the forum but there must be more code I'm missing.
> >>>
> >>> ListBox1.List = Array("Apples", "Oranges", "Pears", "Apricots",
> >>> "Plums")
> >>>
> >>> For the command button, is the following sufficient for it to work?
> >>>
> >>> Private Sub CommandButton1_Click()
> >>> ActiveDocument.Bookmarks(ListBox1).Select
> >>> UserForm1.hide
> >>> End Sub
> >>>
> >>> Thank you.
> >>>
> >>> Michael F.
>
>
>
RE: Listbox - trapping error
Michael F 22.09.2006 17:34:01
Hi,

I have a userform that takes you to specific bookmarks. I tried adding error
handling to the comamnd buttton after I got an error because I did not select
a bookmark. I added some code I found in the Word VBA online help (the
example for On Error had way too much code in one example for me to make
sense out of it).

The code below reports the error via a message box but displays the error
box again even after I select a bookmark. It does go to that bookmark. I also
get the error every time I select a bookmark from the list. And the userform
does not close after going to the selected bookmark.

What should the error handler look like? I wanted to report the error, then
leave the userform displayed so I could select a bookmark and go on my merry
way.

Michael F.

------------------------------------------
Private Sub CommandButton1_Click()

On Error GoTo ErrorHandler
ActiveDocument.Bookmarks(ListBox1.List(ListBox1.ListIndex)).Select

ErrorHandler:
'Check for likely errors.
If Err.Number = 381 Then
' Tell user what happened. Then clear the Err object.
Msg = "Hey! You need to select a bookmark!"
MsgBox Msg, , "Error detected"
Err.Clear ' Clear Err object fields
Exit Sub
End If

Me.hide

End Sub



Re: Listbox - trapping error
Jay Freedman <jay.freedman[ at ]verizon.net> 23.09.2006 00:29:50
Hi Michael,

The problem is not really with the error handler itself. It's just
that you haven't provided any way to _not_ execute the error handler
if there isn't any error. After the .Select executes, the code just
continues to the next line, which is the error handler.

To fix it, rearrange the lines this way:

Private Sub CommandButton1_Click()

On Error GoTo ErrorHandler
ActiveDocument.Bookmarks(ListBox1.List(ListBox1.ListIndex)).Select

Me.hide
Exit Sub

ErrorHandler:
'Check for likely errors.
If Err.Number = 381 Then
' Tell user what happened. Then clear the Err object.
Msg = "Hey! You need to select a bookmark!"
MsgBox Msg, , "Error detected"
Err.Clear ' Clear Err object fields
End If

End Sub

Now, if there's no error, you execute the Me.Hide command and jump out
of the subroutine.

If there is an error in the .Select, execution skips over the Me.Hide
and the Exit Sub, and jumps to the error handler. When that finishes,
the next line is the End Sub. Since the userform hasn't been hidden,
the user will get another chance to select a bookmark.

By the way, if you include the line

ListBox1.ListIndex = 0

that I showed earlier in the thread, you wouldn't be able to have no
selection in the listbox. If you omit that, the userform starts with
..ListIndex -1 and there is no selection.

--
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 Fri, 22 Sep 2006 10:34:01 -0700, Michael F
<MichaelF[ at ]discussions.microsoft.com> wrote:

[Quoted Text]
>Hi,
>
>I have a userform that takes you to specific bookmarks. I tried adding error
>handling to the comamnd buttton after I got an error because I did not select
>a bookmark. I added some code I found in the Word VBA online help (the
>example for On Error had way too much code in one example for me to make
>sense out of it).
>
>The code below reports the error via a message box but displays the error
>box again even after I select a bookmark. It does go to that bookmark. I also
>get the error every time I select a bookmark from the list. And the userform
>does not close after going to the selected bookmark.
>
>What should the error handler look like? I wanted to report the error, then
>leave the userform displayed so I could select a bookmark and go on my merry
>way.
>
>Michael F.
>
>------------------------------------------
>Private Sub CommandButton1_Click()
>
>On Error GoTo ErrorHandler
>ActiveDocument.Bookmarks(ListBox1.List(ListBox1.ListIndex)).Select
>
>ErrorHandler:
>'Check for likely errors.
>If Err.Number = 381 Then
> ' Tell user what happened. Then clear the Err object.
> Msg = "Hey! You need to select a bookmark!"
> MsgBox Msg, , "Error detected"
> Err.Clear ' Clear Err object fields
> Exit Sub
>End If
>
>Me.hide
>
>End Sub
>
>
Re: Listbox - trapping error
Michael F 26.09.2006 13:52:02
Jay,

Thanks again. That was a simple fix for a silly mistake. It works just fine
with the the Me.Hide and Exit Sub moved after the ActiveDocument statement
instead of in the error handler.

Michael F.


"Jay Freedman" wrote:

[Quoted Text]
> Hi Michael,
>
> The problem is not really with the error handler itself. It's just
> that you haven't provided any way to _not_ execute the error handler
> if there isn't any error. After the .Select executes, the code just
> continues to the next line, which is the error handler.
>
> To fix it, rearrange the lines this way:
>
> Private Sub CommandButton1_Click()
>
> On Error GoTo ErrorHandler
> ActiveDocument.Bookmarks(ListBox1.List(ListBox1.ListIndex)).Select
>
> Me.hide
> Exit Sub
>
> ErrorHandler:
> 'Check for likely errors.
> If Err.Number = 381 Then
> ' Tell user what happened. Then clear the Err object.
> Msg = "Hey! You need to select a bookmark!"
> MsgBox Msg, , "Error detected"
> Err.Clear ' Clear Err object fields
> End If
>
> End Sub
>
> Now, if there's no error, you execute the Me.Hide command and jump out
> of the subroutine.
>
> If there is an error in the .Select, execution skips over the Me.Hide
> and the Exit Sub, and jumps to the error handler. When that finishes,
> the next line is the End Sub. Since the userform hasn't been hidden,
> the user will get another chance to select a bookmark.
>
> By the way, if you include the line
>
> ListBox1.ListIndex = 0
>
> that I showed earlier in the thread, you wouldn't be able to have no
> selection in the listbox. If you omit that, the userform starts with
> ..ListIndex -1 and there is no selection.
>
> --
> 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 Fri, 22 Sep 2006 10:34:01 -0700, Michael F
> <MichaelF[ at ]discussions.microsoft.com> wrote:
>
> >Hi,
> >
> >I have a userform that takes you to specific bookmarks. I tried adding error
> >handling to the comamnd buttton after I got an error because I did not select
> >a bookmark. I added some code I found in the Word VBA online help (the
> >example for On Error had way too much code in one example for me to make
> >sense out of it).
> >
> >The code below reports the error via a message box but displays the error
> >box again even after I select a bookmark. It does go to that bookmark. I also
> >get the error every time I select a bookmark from the list. And the userform
> >does not close after going to the selected bookmark.
> >
> >What should the error handler look like? I wanted to report the error, then
> >leave the userform displayed so I could select a bookmark and go on my merry
> >way.
> >
> >Michael F.
> >
> >------------------------------------------
> >Private Sub CommandButton1_Click()
> >
> >On Error GoTo ErrorHandler
> >ActiveDocument.Bookmarks(ListBox1.List(ListBox1.ListIndex)).Select
> >
> >ErrorHandler:
> >'Check for likely errors.
> >If Err.Number = 381 Then
> > ' Tell user what happened. Then clear the Err object.
> > Msg = "Hey! You need to select a bookmark!"
> > MsgBox Msg, , "Error detected"
> > Err.Clear ' Clear Err object fields
> > Exit Sub
> >End If
> >
> >Me.hide
> >
> >End Sub
> >
> >
>

Home | Search | Terms | Imprint | Contact
Newsgroups Reader - provided by WiredBox.Net