Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word User Form - Referencing Bookmarks 1

Status
Not open for further replies.

supernewb

Technical User
Feb 7, 2005
18
US
I have created a simple user form in Word 2000 with two checkboxes (CheckBox1 and CheckBox2) with corresponding textboxes (TextBox1 and TextBox2). I would like the user to be able to select one or both of the checkboxes(got that part). On the word document, I have created a bookmark "b3" to mark the location where I would like the text to be copied to.

I am having difficulty transferring the text from the user's selection to the active document behind the form. I know it is the reference to the bookmark. Here's where I'm stuck:

If CheckBox1.Value = True Then ActiveDocument.Bookmarks("b3")= TextBox1.Value

Thanks in advance for any assistance.
supernewb
 
I tried this and it worked!?

If CheckBox1.Value = True Then ActiveDocument.Bookmarks("b3").Range = TextBox1.Text

I am used to VBA in Excel and Access. I rarely use Word. This may not be the best way to do this, but at least it put the text in there. :)

Feedback always welcome.
cheers.
D
 
If CheckBox1.Value = True Then ActiveDocument.Bookmarks("b3").Range.Text = TextBox1.Text

Try adding the .Text property.

Try also .InsertAfter.

A lot depends on whether this is a one-time thing, or something that will be reused. Often inserting text inti a bookmark will delte the bookmark itself.

In which case, you need to re-establish the bookmark.

There are a few ways to do this.

1. you can make the spot where you want the result of Textbox1 into a formfield. In this case, you simply dump the text from textbox1 into the form field. NOTE: in this casew, as you are inserting the resukts directly, you do NOT have to protected the document for forms. You can state the .Resuklt of a form field, via code, WITHOUT protection.

Code:
aDoc.FormFields("Text1").Result = Textbox1.Text

Note also you can remove the grey indication of a form field to make it look like ordinary text. Bring up the form field toolbar, and click the "form Field Shaing" icon. Shading is removed and the text inside the form field looks like everything else.

2. Go to the bookmarks and insert the text. Often this deletes the bookmark, in which case you have to re-establish the bookmark. NOTE: the following has made the ActiveDocument into an Document object (aDoc). If you do not wish to do so, then replace aDoc with ActiveDocument

Code:
Selection.GoTo what:=wdGoToBookmark, Name:="b3"
Selection.TypeText Text:=Textbox1.Text
Selection.MoveStart unit:=wdCharacter, Count:=-Len(Textbox1.Text)
aDoc.Bookmarks.Add Name:="b3", Range:=Selection.Range


Gerry
 
Thank you Gerry!! The .Text property and "2" option for re-establishing the bookmarks worked great. I didn't realize that I was pasting text over the bookmarks. I will also use the fields (your "1" option) for another part of my form.

This form will be re-used again and again and since practically all of the info stays the same (just different parts printed each time), I want to print only the text that applies (with my new userform :) ) to save myself alot of cutting and pasting in the future.

Thank you again for your help.
Dina
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top