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

Dynamic Word Document Creation

Status
Not open for further replies.

TSMABob

Programmer
Jun 27, 2003
15
US
So, here's a problem for anyone who wants to tackle it. I'm wondering about the feasability of it, so let me know if you think its possible and any suggestions on how to approach it.

I have a Template in Word 2002 that has a number of form fields for a user to enter data. There are set headers and footers with quite a bit of information in them so that all thats really in the body is a table and a few other pieces of text.

What I want to do is have a prompt for the user to enter how many rows the table should have, and have that many rows appear on the page. The problem arises that each row has 5 text box form fields, and bookmarks in each of those form fields to identify them.

Can I create tables, bookmarks, and form fields on the fly? or is this going to be too much of a hassle to bother with?
 
Actually, it's doable. You'll only need to write code to build one row with something to give each bookmark (and field) a unique name. Let Word write it for you using the record Macro feature then tweak the code so you can run it from outside the app.

I'd go for it if that's what you really want. I'd also buy the WROX Word VBA book for some moral support.

 
Hey Bob,

This code adds a form field to the first cell in each row in the referenced table...
Code:
Sub AddFormFields()
    With ActiveDocument.Tables(1)
        For Each r In .Rows
        r.Cells(1).Select
        Selection.FormFields.Add Range:=Selection.Range, Type:= _
            wdFieldFormTextInput
        Next
    End With
End Sub
Hope this helps :)

Skip,
Skip@TheOfficeExperts.com
 
Aviatrix and Skip,

wow, thanks so much for your help. I've been modifying your code a bit Skip to get it where I want to be, and using the Record Macro option (what a fun tool) to really get things moving.

One problem that seems to be coming up is that I can't merge cells very well, and I can't seem to resize the cells either. I'm thinking I might have to bail on that idea, the record macro tool won't let me alter any table properties while its recording. Maybe just having the table autoresize each row as text is entered could be a way to go?
 
What I've done before is to keep a .dot template file with the table header and one blank but formatted row. I either build the whole file I want to use from this template or copy the table from the template and paste it into my working document. This can save lots of time and aggravation. Realize that if you send Word a tab character while in the last row of a table you will add another row to the table with the same formatting as the previous row.

If you are not familiar with creating templates check out the Word Help files.

Good luck! Once you figure out the basics there's no end to what you can do with this stuff.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top