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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word97: How to fill a document template using a userform?

Status
Not open for further replies.

Katerine

Programmer
Mar 9, 2001
234
US
Hi all,
I apologize for what is probably a ridiculously simple question. I'm a relative newcomer to Word VBA (I've been programming in Access for a couple years now), and I need to update some templates.

Imagine something like your manual tax return. That's kind of what these templates look like: they contain a lot of text, and a lot of table fields and "fill-in-the-blank" areas. All of the text is written into the template itself, but the data needs to be filled in by code, after the user inputs it into a userform.

Currently, all of the areas that require data (about 40 of them) have unique mailmerge fields in them. At the end of the data-entry, the user clicks a "continue" or "finish" button, which adds MailMerge sets to the document based on the data in the form.

This works, but it seems like there should be a much easier way to do it to me. One that doesn't require hard-coding over 40 mail-merge names into the code. So I'm looking for a better way. It just seems like there should be a much more direct way of adding data to a document than this. Or isn't there? :-I

Thanks in advance for any help you can provide! :)
 
I have never used Mail merge so I don't really know what it does, but if I was going to populate field from a userform I would just put Labels on the Document then fill them from VB.

ThisDocument.Label1.Caption = TextBox1.Text

Let me know if this isn't what you are looking for.
 
Another way you can do it is to have Tables on your document and populate the cells of the tables.

This method would allow the user to change the fields after the Userform has closed.

This bit of code creates a table and populates two cells with data.

Private Sub AddTable()
Set myTable = ActiveDocument.Tables.Add(Selection.Range, 3, 3)
With myTable
.Cell(1, 1).Range.InsertAfter "First Cell"
.Cell(myTable.Rows.Count, myTable.Columns.Count).Range.InsertAfter "Last Cell"
End With
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top