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