Userform and Bookmarks

T

tjbennett

I created a userform according to
http://www.word.mvps.org/FAQs/Userforms/CreateAUserForm.htm. Here is my code:
"Private Sub CommandButton1_Click()

With ActiveDocument.bookmarks("txtWorkNo").Range.InsertBefore(TextBox1)

End With

UserForm1.Hide
End Sub

When I run it I get a Compile error: Expected Function or variable, and the
".InsertBefore" is highlighted in the code.

What went wrong??
 
H

Helmut Weber

Hi,

try:

ActiveDocument.bookmarks("txtWorkNo").Range.InsertBefore(TextBox1.text)

--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
T

tjbennett

tThanks Helmut,
I actually figured it out, I now have it

With ActiveDocument.bookmarks("txtWorkNo").Range
..InsertBefore (TextBox1)

and it works. However, I do have another problem, in that I would like to
protect the document (forms) so that users will only be able to enter data
onto the document in a textform field that I have setup. The reason is that
after they enter data in that field and "TAB" off of it, the data is then
written to an access database table. So how do I leave the document
"unprotected" so the code can write the "txtWorkNo" into the document and
then protect it so the user can only fill in the textform field?
 
G

Gordon Bentley-Mix

tj,

I do this all the time, and it's pretty simple.

I'm assuming that TextBox1 is a TextBox control on a UserForm and that the
UserForm has some sort of CommandButton control on it that the user clicks to
insert the values from the UserForm into the document - an "OK" button or
some such. In the Click event for this button just use a variation of the
following:

Private Sub btnOK_Click()
ActiveDocument.Unprotect "password"
[do stuff to create the document like...]
ActiveDocument.Bookmarks("txtWorkNo").Range.InsertBefore(TextBox1)
[etc. ...]
ActiveDocument.Protect wdAllowOnlyFormFields, True, "password"
End Sub

See the VBA help topics on "protection" for more information.

And just a quick comment on your code if I may:

The line

ActiveDocument.Bookmarks("txtWorkNo").Range.InsertBefore(TextBox1)

relies on using the default property of the 'TextBox1' control, which just
happens to be the .Value property. IMHO it's best to explicitly specify the
property you want to use whenever possible - as Helmut did in his example,
although he used the .Text property. You never know when MS might decide to
change the default property of a particular type of control, and then your
code breaks without any real indication of why.

In addition, it's just a good habit to get into because you probably won't
always want to work with just the default property of a control. For example,
with a ListBox control you may want to work with the entire list (the .List
property) or the index of the selected item in the list (the .ListIndex
property) rather than the actual value the selected item. This is especially
true if you're adding items to the list through the UserForm itself - a list
of names or something - instead of using the ListBox to just present a list
for the users to select from. In this instance, the value that's selected is
of less interest than some of the other available properties. And even if you
are using the ListBox to provide a list for the users to pick from, the
..ListIndex property of the selected item is safer and easier to work with
than the .Value property. Using the .ListIndex lets you change the displayed
values (to make them more descriptive perhaps) without having to search your
code to find all the places where you might have used .Value to control the
flow of the code (i.e. in a Select Case statement). The value of the items
can be changed without touching the index.

Explicitly specifying the property to work with also makes your code easier
understand, which can be especially important if you come back to a project
after a few weeks or months and need to come up to speed quickly with what's
going on. (It's also a bit of kindness for the developer who gets tasked with
picking up your project after you've moved on - says the Voice of Experience.
;-D)

--
Cheers!
Gordon
The Kiwi Koder

Uninvited email contact will be marked as SPAM and ignored. Please post all
follow-ups to the newsgroup.
 
T

tjbennett

Thanks Gordon. Add also thank you for the comments on my code, I am still a
beginner and will take all the advise I can get.

Gordon Bentley-Mix said:
tj,

I do this all the time, and it's pretty simple.

I'm assuming that TextBox1 is a TextBox control on a UserForm and that the
UserForm has some sort of CommandButton control on it that the user clicks to
insert the values from the UserForm into the document - an "OK" button or
some such. In the Click event for this button just use a variation of the
following:

Private Sub btnOK_Click()
ActiveDocument.Unprotect "password"
[do stuff to create the document like...]
ActiveDocument.Bookmarks("txtWorkNo").Range.InsertBefore(TextBox1)
[etc. ...]
ActiveDocument.Protect wdAllowOnlyFormFields, True, "password"
End Sub

See the VBA help topics on "protection" for more information.

And just a quick comment on your code if I may:

The line

ActiveDocument.Bookmarks("txtWorkNo").Range.InsertBefore(TextBox1)

relies on using the default property of the 'TextBox1' control, which just
happens to be the .Value property. IMHO it's best to explicitly specify the
property you want to use whenever possible - as Helmut did in his example,
although he used the .Text property. You never know when MS might decide to
change the default property of a particular type of control, and then your
code breaks without any real indication of why.

In addition, it's just a good habit to get into because you probably won't
always want to work with just the default property of a control. For example,
with a ListBox control you may want to work with the entire list (the .List
property) or the index of the selected item in the list (the .ListIndex
property) rather than the actual value the selected item. This is especially
true if you're adding items to the list through the UserForm itself - a list
of names or something - instead of using the ListBox to just present a list
for the users to select from. In this instance, the value that's selected is
of less interest than some of the other available properties. And even if you
are using the ListBox to provide a list for the users to pick from, the
.ListIndex property of the selected item is safer and easier to work with
than the .Value property. Using the .ListIndex lets you change the displayed
values (to make them more descriptive perhaps) without having to search your
code to find all the places where you might have used .Value to control the
flow of the code (i.e. in a Select Case statement). The value of the items
can be changed without touching the index.

Explicitly specifying the property to work with also makes your code easier
understand, which can be especially important if you come back to a project
after a few weeks or months and need to come up to speed quickly with what's
going on. (It's also a bit of kindness for the developer who gets tasked with
picking up your project after you've moved on - says the Voice of Experience.
;-D)

--
Cheers!
Gordon
The Kiwi Koder

Uninvited email contact will be marked as SPAM and ignored. Please post all
follow-ups to the newsgroup.


tjbennett said:
tThanks Helmut,
I actually figured it out, I now have it

With ActiveDocument.bookmarks("txtWorkNo").Range
.InsertBefore (TextBox1)

and it works. However, I do have another problem, in that I would like to
protect the document (forms) so that users will only be able to enter data
onto the document in a textform field that I have setup. The reason is that
after they enter data in that field and "TAB" off of it, the data is then
written to an access database table. So how do I leave the document
"unprotected" so the code can write the "txtWorkNo" into the document and
then protect it so the user can only fill in the textform field?
 
G

Gordon Bentley-Mix

BTDT tj. We were all newbies once (although it was much longer ago for some
than for others). This forum is a great resource - especially when the VBA
help doesn't.

Good luck and happy coding!
--
Cheers!
Gordon

Uninvited email contact will be marked as SPAM and ignored. Please post all
follow-ups to the newsgroup.


tjbennett said:
Thanks Gordon. Add also thank you for the comments on my code, I am still a
beginner and will take all the advise I can get.

Gordon Bentley-Mix said:
tj,

I do this all the time, and it's pretty simple.

I'm assuming that TextBox1 is a TextBox control on a UserForm and that the
UserForm has some sort of CommandButton control on it that the user clicks to
insert the values from the UserForm into the document - an "OK" button or
some such. In the Click event for this button just use a variation of the
following:

Private Sub btnOK_Click()
ActiveDocument.Unprotect "password"
[do stuff to create the document like...]
ActiveDocument.Bookmarks("txtWorkNo").Range.InsertBefore(TextBox1)
[etc. ...]
ActiveDocument.Protect wdAllowOnlyFormFields, True, "password"
End Sub

See the VBA help topics on "protection" for more information.

And just a quick comment on your code if I may:

The line

ActiveDocument.Bookmarks("txtWorkNo").Range.InsertBefore(TextBox1)

relies on using the default property of the 'TextBox1' control, which just
happens to be the .Value property. IMHO it's best to explicitly specify the
property you want to use whenever possible - as Helmut did in his example,
although he used the .Text property. You never know when MS might decide to
change the default property of a particular type of control, and then your
code breaks without any real indication of why.

In addition, it's just a good habit to get into because you probably won't
always want to work with just the default property of a control. For example,
with a ListBox control you may want to work with the entire list (the .List
property) or the index of the selected item in the list (the .ListIndex
property) rather than the actual value the selected item. This is especially
true if you're adding items to the list through the UserForm itself - a list
of names or something - instead of using the ListBox to just present a list
for the users to select from. In this instance, the value that's selected is
of less interest than some of the other available properties. And even if you
are using the ListBox to provide a list for the users to pick from, the
.ListIndex property of the selected item is safer and easier to work with
than the .Value property. Using the .ListIndex lets you change the displayed
values (to make them more descriptive perhaps) without having to search your
code to find all the places where you might have used .Value to control the
flow of the code (i.e. in a Select Case statement). The value of the items
can be changed without touching the index.

Explicitly specifying the property to work with also makes your code easier
understand, which can be especially important if you come back to a project
after a few weeks or months and need to come up to speed quickly with what's
going on. (It's also a bit of kindness for the developer who gets tasked with
picking up your project after you've moved on - says the Voice of Experience.
;-D)

--
Cheers!
Gordon
The Kiwi Koder

Uninvited email contact will be marked as SPAM and ignored. Please post all
follow-ups to the newsgroup.


tjbennett said:
tThanks Helmut,
I actually figured it out, I now have it

With ActiveDocument.bookmarks("txtWorkNo").Range
.InsertBefore (TextBox1)

and it works. However, I do have another problem, in that I would like to
protect the document (forms) so that users will only be able to enter data
onto the document in a textform field that I have setup. The reason is that
after they enter data in that field and "TAB" off of it, the data is then
written to an access database table. So how do I leave the document
"unprotected" so the code can write the "txtWorkNo" into the document and
then protect it so the user can only fill in the textform field?


:

Hi,

try:

ActiveDocument.bookmarks("txtWorkNo").Range.InsertBefore(TextBox1.text)

--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
F

fumei via OfficeKB.com

There is also a reason why:

With ActiveDocument.bookmarks("txtWorkNo").Range.InsertBefore(TextBox1)

End With

did NOT work, but:

With ActiveDocument.bookmarks("txtWorkNo").Range
.InsertBefore(TextBox1)

End With

does work. The first With statement did not have anything to work ...ummm...
with. The With statement was trying to process action on the Textbox1, NOT
the Range. So it was expecting something, a function...something.

By changing it to:

With ActiveDocument.bookmarks("txtWorkNo").Range

now it is trying to process action WITH the RANGE, and InsertBefore is a
valid action.

Although if it is a single action, there is not much point in using With.
tjbennett said:
tThanks Helmut,
I actually figured it out, I now have it

With ActiveDocument.bookmarks("txtWorkNo").Range
.InsertBefore (TextBox1)

and it works. However, I do have another problem, in that I would like to
protect the document (forms) so that users will only be able to enter data
onto the document in a textform field that I have setup. The reason is that
after they enter data in that field and "TAB" off of it, the data is then
written to an access database table. So how do I leave the document
"unprotected" so the code can write the "txtWorkNo" into the document and
then protect it so the user can only fill in the textform field?
[quoted text clipped - 9 lines]
Vista Small Business, Office XP
 

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