UserForm interaction

W

Wim R.M. Dekeyser

Hi,

I am creating UserForms to ease the creation, editing, and checking of
standard documents.

It is likely that the user does not finish creating the document in one
session and therefore, I am passing information from bookmarks in the
document back to the UserForm.

My problem is that there are many bookmarks and I would like to be able to
do something like:

for each bookmark
get the value (text string)
put this in the correct place in the UserForm
next

It doesn't seem possible to do this because e.g. UserForm1.TextBox1.Value =
aBookmark.Range.Text requires the correct Name of the UserForm item. I tried
making the bookmark name identical to the UserForm item name and feeding a
strName instead of TextBox1 but that does not work.

Any tips?

cheers,
wim dekeyser
 
G

Greg Maxey

You will need to do some error handling (e.g., for any bookmarks that don't
have a corresponding control and vise versa), but something like this should
get you started:
Private Sub UserForm_Initialize()
Dim oCtrs As Controls
Dim oBm As Bookmark
Set oCtrs = Me.Controls
For Each oBm In ActiveDocument.Bookmarks
oCtrs(oBm.Name).Text = oBm.Range.Text
Next oBm
Set oCtrs = Nothing
End Sub




Private Sub CommandButton1_Click()
Dim oBMs As Bookmarks
Dim oCtr As Control
Dim oRng As Word.Range
Set oBMs = ActiveDocument.Bookmarks
For Each oCtr In Me.Controls
On Error GoTo Err_Handler
Set oRng = oBMs(oCtr.Name).Range
oBMs.Add oCtr.Name, oRng
Err_ReEntry:
Next oCtr
Set oBMs = Nothing
Unload Me
Exit Sub
Err_Handler:
If Err.Number = 5941 Then Resume Err_ReEntry
End Sub
 
D

Doug Robbins - Word MVP

As bookmarks are rather fragile in the hands of users, you might be better
off to be writing the data from the userforms to document variables and have
it displayed in the document by the use of DOCVARIABLE fields. On
initialization of the userform, that information can be read from the
variables and displayed in the controls of the form in a similar way.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
W

Wim R.M. Dekeyser

Hi Doug,

I apologize for the late answer - I didn't expect more answers to pop up:)

Thanks for the tip. I'll search the net for information on this. Do you
recommend using document variables also for larger pieces of text (entire
paragraphs - several pages) or just for a few words here and there?

(I found out today that bookmarks can be fragile. So far my code for
extracting the information out of them upon initialization and then writing
new information into them started adding more and more spaces to the
bookmark. Adding a find and replace function ended up deleting the
bookmarks...)

While I am here: can a user dynamically update the UserForm that is being
filling out? I'll have to somehow cater for creating tables in the document
without knowing in advance how many rows there will be.

Thanks!
wim
 
D

Doug Robbins - Word MVP

Assigning what will become a whole paragraph to a document variable is not
an issue. A whole page probably implies multiple paragraphs and while you
can assign multiple paragraphs to a document variable by inserting a vbCr
into the string at the appropriate place, doing that for information that is
being entered by the user into a textbox control on a userform could be an
issue.

I am not sure what you mean by having the user dynamically update the
userform that is being filled out.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
W

Wim R.M. Dekeyser

Hi Doug,

Thanks for your help!

I understand (i.e. I've read somewhere) that there can be issues with CR in
a textbox control. I'll look into that a bit more when I get to that. I will
go the docvariable way and abandon the bookmarks approach.


About dynamic userforms: very specifically, I am working on templates that
in chapter 1.2.2 will refer to customer documents in a table with 3 columns:
"number", "ID", and "Name".

Not only will this customer document data vary, also the _number_ of
documents to refer to will vary. Can I make a button "Insert new row" on the
UserForm that updates the UserForm with an extra 3 textboxes?

cheers,
wim
 
D

Doug Robbins - Word MVP

I think what you want to be doing is have a list box on the userform and
three text boxes into which the user would enter the details for the new
item. Then by clicking on a command button, the new item would be added to
the list box. You would probably also want command buttons to delete a
selected item form the list or to edit it by loading it back into the text
boxes.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 

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