slicker methodology

W

Wicked Wizard

I've got some code attached to a button that takes data from a user form and
puts it in a letter at some bookmarks, very simple, thus:

Private Sub cmdOK_Click()
'utilise the data on the form when the user hits OK

'hide the form, leaving it in memory to access the data
frmGetData.Hide

'take the data from the boxes on the form and place it at the bookmarks in
the document
If ActiveDocument.Bookmarks.Exists("Addressee") Then
ActiveDocument.Bookmarks("Addressee").Range.Text = txtAddressee.Text
End If
If ActiveDocument.Bookmarks.Exists("Company") Then
ActiveDocument.Bookmarks("Company").Range.Text = txtCompany.Text
End If
If ActiveDocument.Bookmarks.Exists("LineOne") Then
ActiveDocument.Bookmarks("LineOne").Range.Text = txtLineOne.Text
End If
If ActiveDocument.Bookmarks.Exists("LineTwo") Then
ActiveDocument.Bookmarks("LineTwo").Range.Text = txtLineTwo.Text
End If
If ActiveDocument.Bookmarks.Exists("LineThree") Then
ActiveDocument.Bookmarks("LineThree").Range.Text = txtLineThree.Text
End If
If ActiveDocument.Bookmarks.Exists("LineFour") Then
ActiveDocument.Bookmarks("LineFour").Range.Text = txtLineFour.Text
End If
If ActiveDocument.Bookmarks.Exists("Salutation") Then
ActiveDocument.Bookmarks("Salutation").Range.Text = txtSalutation.Text
End If
If ActiveDocument.Bookmarks.Exists("Subject") Then
ActiveDocument.Bookmarks("Subject").Range.Text = txtSubject.Text
End If

'move the cursor to the start of the letter to begin typing
Selection.GoTo what:=wdGoToBookmark, Name:="StartText"

'get the form out of memory
Unload frmGetData

End Sub

Works fine, and it's only a small form. But I can see that I might want to
collect lots of data, whereupon this becomes rather tedious. So I thought,
if I were any good with VBA (and I am only starting - an aged canine slowly
learning a new trick) I would loop through the text boxes on the form, get
the data and stick it in each bookmark by looping through them too. It's
getting to the stage where I need to go for my afternoon lie down, so I
thought I'd ask for some help and see if this can be done as I envisage.
Can any of you young things put me right please, while I reach for my zimmer
frame?
 
J

Jezebel

There's no 'general' solution, but here's one method:

When you create the form, for each textbox, set the .Tag property to contain
the name of the corresponding bookmark.

Then you can use code like

Dim pControl as Control
For each pControl in Me.Control
If Len(pControl.Tag) > 0 then 'Only want the tagged
controls -- there might be others
ActiveDocument.Bookmarks(pControl.Tag).Range = pControl.Text
End if
Next

Suggest you consider using DocProperty fields rather than bookmarks for
putting the values into the document. The method you are currently using has
the side effect of deleting the bookmarks, so your code will fail if you run
it a second time on the same document. Instead of setting the bookmark, you
set the document property --

ActiveDocument.CustomDocumentProperties(pControl.Tag) = pControl.Text

And you would need to add, at the end, an instruction to update fields. This
method has the added benefit that you can use the same piece of information
in more than one place in the document; and your document retains the
information (so you could read it back into the form) even if you don't
display it anywhere.
 
W

Wicked Wizard

Thanks, that's really useful!

OK

Jezebel said:
There's no 'general' solution, but here's one method:

When you create the form, for each textbox, set the .Tag property to
contain the name of the corresponding bookmark.

Then you can use code like

Dim pControl as Control
For each pControl in Me.Control
If Len(pControl.Tag) > 0 then 'Only want the tagged
controls -- there might be others
ActiveDocument.Bookmarks(pControl.Tag).Range = pControl.Text
End if
Next

Suggest you consider using DocProperty fields rather than bookmarks for
putting the values into the document. The method you are currently using
has the side effect of deleting the bookmarks, so your code will fail if
you run it a second time on the same document. Instead of setting the
bookmark, you set the document property --

ActiveDocument.CustomDocumentProperties(pControl.Tag) = pControl.Text

And you would need to add, at the end, an instruction to update fields.
This method has the added benefit that you can use the same piece of
information in more than one place in the document; and your document
retains the information (so you could read it back into the form) even if
you don't display it anywhere.
 
A

Anne P.

Hi,

Here's an even easier one:
Name all books marks in your document with the same name as the control. For example, if you have a text box named txtName, name the bookmark in the document exactly the same.
In the code for the OK button (or whatever you name the button that finishes the data fill), enter the following code:

Dim bmk As Bookmark
Dim ctrl As Control

'Loop thru controls and find ones that match bookmark name and then fill bookmark

'with contents of control

For Each ctrl In Me.Controls

If ActiveDocument.Bookmarks.Exists(ctrl.Name) Then

ActiveDocument.Bookmarks (ctrl.Name).Range.Text = ctrl.Text

End If

Next

'If you want to delete the bookmark after filling:

For Each bmk In ActiveDocument.Bookmarks

bmk.Delete

Next

Anne
 

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