PDA

View Full Version : Looking for a Tutorial



cvernon
04-02-2009, 09:09 AM
Does anybody have a link to a good tutorial for using Word VBA to create tables and formfields?

I'm basically looking to be able to launch a macro that would insert a table (where I specifiy, of course) and populate said table with a variety of formfields, which I assume I'd need to create as an array.

Thanks!

fumei
04-02-2009, 09:55 AM
I can not point you to a tutorial, but if you specify precisely what it is you want to do, we can probably help.

At the top of a variety of questions, could you please clarify your assumption that you would need to create an array of formfields. What exactly do you mean by that? An array of Type (of formfield)? An array of names to be used - using the default names is not a good idea.

How are you thinking of determining the "variety of formfields"?

Have you tried recording a macro that makes a table and puts in formfields, and looking at the code?

Do you have a determination of which cells in your table have what type of formfield?

This can be done, but you need to figure out EXACTLY what it is you want to do. Here is a demo document that demonstrates the possibilities. Click "Make Table With Formfield" on the top toolbar. It creates a table at a bookmark ("TableHere") - but it could of course make the table anywhere you like. If you wanted it at the Selection point, then you use that range instead of the range object I used.

The code then makes text formfields in each cell of Row 1, giving each its own name. It is a VERY good idea to give uniques names rather than use the default names.

It then makes checkbox formfields in each cell of Row 2, giving each its own names.

The names come from arrays built into the code. The code used follows, hopefully commented enough to show what is going on.

NOTE: if you are not going to use parameters (e.g. .Exitmacro = ""), then you can take out those lines.


Option Explicit

Sub MakeTableWithFormfields()
Dim oCell As Cell
Dim oTable As Table
Dim oFF As FormField
Dim r As Range
Dim TextFF()
Dim CheckboxNames()
Dim j As Long

' give arrays some values
TextFF = Array("ClientFirstName", _
"ClientLastName", "ClientAddress")
CheckboxNames = Array("Yadda", "Whatever", "HoHum")

' set the range object to the bookmark
' "TableHere" - for demo purposes

Set r = ActiveDocument.Bookmarks("TableHere").Range

' create table as table object
' at the bookmark location
' set as r (above)
Set oTable = ActiveDocument.Tables.Add(Range:=r, _
NumRows:=3, NumColumns:=3, _
DefaultTableBehavior:=wdWord9TableBehavior, _
AutoFitBehavior:=wdAutoFitFixed)

' put a text formfield in each cell
' of Row 1, giving each a name
' from the TextFF array
' NOTE: Type 70 is wdFieldFormTextInput

For Each oCell In oTable.Rows(1).Cells
' set range object for each cell
Set r = oCell.Range
' set (create) a formfield using that cell range
Set oFF = r.FormFields.Add(Range:=r, Type:=70)
' set parameter values of formfield
With oFF
.Name = TextFF(j)
.EntryMacro = ""
.ExitMacro = ""
.Enabled = True
.OwnHelp = False
.HelpText = ""
.OwnStatus = False
.StatusText = ""
With .TextInput
.EditType Type:=wdRegularText, _
Default:="", Format:=""
.Width = 0
End With
End With
j = j + 1
Next
' reset counter
j = 0
' make checkbox in each cell
' in Row 2, giving each a name
' from CheckboxNames array
' Type 71 is wdFieldFormCheckbox

For Each oCell In oTable.Rows(2).Cells
Set r = oCell.Range
Set oFF = r.FormFields.Add(Range:=r, Type:=71)
With oFF
.Name = CheckboxNames(j)
.EntryMacro = ""
.ExitMacro = ""
.Enabled = True
.OwnHelp = False
.HelpText = ""
.OwnStatus = False
.StatusText = ""
With .CheckBox
.AutoSize = True
.Size = 10
.Default = False
End With
End With
j = j + 1
Next
End Sub

fumei
04-02-2009, 09:58 AM
Final note. The document uses a BasicText style, and the table cells use it. This could be any style you like.

cvernon
04-02-2009, 01:25 PM
Good questions there, fumei. Hopefully I can clear it up some.

An array of names for the formfields, yes.

The ‘variety of formfields’ (as I wrote) would be duplicates of formfields I used in the first chunk of the table.


Let’s say I have a form that I want to capture member information, which I have formatted into a table over a few rows & columns:

name (text form field)
position (drop-down form field)
birth date (text form field)
telephone number (text form field)
left-handed (check box form field)

I want a macro that will copy that “template” and add another instance of it to the bottom of the table when I wish to add another player to the roster. I’d also like a way to delete a player as well (maybe include a button within each player’s section to grab the relevant section of the table and delete them).

Hopefully that is more clear and thank you for the feedback!

fumei
04-03-2009, 01:23 PM
Oh-oh. That is quite another thing. Copying formfields is NOT a good idea.

Also you wrote: "add another instance of it to the bottom of the table "

That is NOT the same as: "would insert a table (where I specifiy, of course) and populate said table with a variety of formfields"

In the first case you are adding a row (I think) to an existing table, and putting formfields in that new row.

In the second case (actually your first post) you are asking about inserting a new table.

Or are you?

When doing this stuff (VBA, or any other programming) it is essential that you figure out EXACTLY, precisely, what it is you want to do.

That is far, far, far more important than any actual coding.