Creating AutoTextEntry from form data

G

Graham Scott

Hi,

This is my first stab at a VBA form, and I am having some trouble trying to
create AutoTextEntries from data entered in the form.

I have a template with a form. When the user attempts to create a document
from the template, I want to display a form for the user to enter some data
(name, phone, e-mail, course number, etc). When the form is dismissed, I
validate the data entered, and put it into Document Variables, for display
in the header, AND I also want to put some items into AutoText entries in
the template, so that (a) they can be easily used elsewhere in the text, and
(b) next time the user creates a document from the template, I can use those
AutoText entries to pre-load the form.

So the process is basically:
- Display the form, preloading with previously used values where possible.
- After the user hits OK, in Sub Document_New(), retrieve the values from
the form fields, load them into Document Variables, and create / update
AutoText Entries.

I retrieve the values from the form TextBoxes into String variables, but
'AutoTextEntries. Add' will not accept a String value for the Range
parameter, and I can't quite get my head around exactly what I am supposed
to give it for Range. I don't find the help very helpful on this - the Range
is supposed to be a Range of text from the document, yes? I don't see how I
am to manage that, unless I can somehow tell it to use the contents of the
Document Variable which I have set up, but that doesn't seem to work either.

Very confused! Any help will be much appreciated!

GRS
 
P

Peter Hewett

Hi Graham

Just some background. You don't need to use document a variable to insert
data into a Header you can just use a bookmark as you would for inserting
data into the document body. By adding fields to the header you've just got
something else to worry about, because now you have to update the fields!

AutoText can only be added from a document not from a variable. Autotext is
inherently document oriented and retains font/paragraph formatting,
bookmarks, styles and just about anything else you can think of! So you
have to add the text to the document create the AutoText entry and the
delete the text.

The easy way to do this is use bookmarks for everything and then reload the
controls using the bookmarked text. If the user has deleted the bookmarked
text then disable that particular control. Here's a simple version of what
I use. Add this code to you Form:

Public Sub ReloadFromBMs(Optional ByVal boolDiableIfBlank As _
Boolean = False)
txtA.Value = GetBMText("BM_A")
txtB.Value = GetBMText("BM_B")
txtC.Value = GetBMText("BM_C")

If boolDiableIfBlank Then
If LenB(txtA.Value) = 0 Then txtA.Enabled = False
If LenB(txtB.Value) = 0 Then txtB.Enabled = False
If LenB(txtC.Value) = 0 Then txtC.Enabled = False
End If
End Sub

Private Function GetBMText(ByVal strBMName As String) As String
With ActiveDocument.Bookmarks
If .Exists(strBMName) Then
GetBMText = .Item(strBMName).Range.Text
End If
End With
End Function

The above example uses 3 TextBox controls "txtA", "txtB" and "txtC" and the
document uses bookmarks "BM_A", "BM_B" and "BM_C". You can optionall
disable the TextBox control if the bookmark was missing or a null string.

You use the code like this:

Public Sub FormTest()
Load frmTestMe
frmTestMe.ReloadFromBMs True
frmTestMe.Show
End Sub


HTH + Cheers - Peter
 

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