Help Creating a simple VBA form

M

Mark.P.Bruno

Hello,
I'm trying to design a VBA form that has a bunch of text boxes and a
generate button. When the generate button is pressed, the text of the
text boxes are placed in corresponding labels on the word document.
(Sounds pretty pointless but the tricky positioning of the labels makes
it worthwhile).

My code works perfectly in the original .doc I created, when I run the
form from the VBA editor. Since I want this to be a template where the
form displays automatically upon opening a new document, I added
"UserForm1.Show" to a Document_New() subroutine in ThisDocument. Then
I saved it as a .dot file.

Upon opening the .dot file, the form appears, but when the genereate
button is clicked, none of the text is actually put onto the document.

I'm sorry my description is so poor; this is literally my first
experience with VBA (I know some VB 6.0 from way back). Does anyone
know what I'm doing wrong?

Thanks a lot.
-Mark
 
E

Ed

Hi, Mark. Why don't you post the code you are using with your button?

Also, you say
When the generate button is pressed, the text of the
text boxes are placed in corresponding labels on the word document.
What are you using to receive the text for the "labels" in your document?
Bookmarks? Fields? Table cells?

If the code "works perfectly" in the VBE, then I might suspect the way you
instance the document object the form is looking for could be amiss. How
are you telling the UserForm which doc to put the text into?

Ed
 
M

Mark.P.Bruno

Thanks for the quick reply, Ed. Sorry I'm so terribly inarticulate;
I'm really only a C++/Java programmer who had some very basic VB6
experience in the late 90s. I suspected that it was the way I
instanced the object. I just have "UserForm1.show" in Document_New()
and the doc is referenced as "ThisDocument" within UserForm1.

The labels in the word doc are just that: VB labels. They were the
first things I found when poking around in the menu. In the subroutine
of my button I use:
ThisDocument.Label1.Caption = TextBox1.Text
and so on.

While we're at it, I'd actually rather be using fields; would you
happen to have a link to a tutorial on that?

Obviously I'm totally hacking my way through this, but we just need a
very simple form that's easy to use. Thanks for the help!
-Mark

"If the code "works perfectly" in the VBE, then I might suspect the way
you
instance the document object the form is looking for could be amiss.
How
are you telling the UserForm which doc to put the text into? "
 
D

Doug Robbins - Word MVP

Use DOCVARIABLE fields in the document where you want the data from the form
to be displayed and in a command button click event on the user form use the
following code

With ActiveDocument
.Variables("varname").Value = txtvarname.Text
'repeat for each textbox
.Fields.Update
End With

The DOCVARIABLE fields that you would have in the template would contain

{ DOCVARIABLE varname }



--
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
 
M

Mark.P.Bruno

Thanks, but I'm still having trouble making an instance of UserForm1 in
the general declarations of the template -- I think. Or am I totally
barking up the wrong tree?
 
D

Doug Robbins - Word MVP

Put the UserForm1.Show in an AutoNew() macro in the template.

--
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