Adding FormFields

K

Ken McLennan

G'day there One & All,

Please accept my apology if I've come to the wrong NG, although
the name seems inclusive of pretty much everything related to Word VP
Programming =).

I'm currently trying to write a bit of a Word application. I'm not
a trained, nor even a practised, macro writer but I have had a little
success with Excel applications for my employer. I'm still trying to
come to grips with Word's way of doing things and I've struck
difficulties.

I have a document with a table at the top of the page. It has only
a single row with 4 cells. The first contains day & date fields which I
have working the way I want. The next 3 cells are causing my problems.
After the headings I'm trying to get FormFields for data entry as
follows:

Name: Shift: Number:
[ ] [ ]-[ ] [ ]

As you can probably guess, the "Shift" fields are for start &
finish times. I'll write a macro to add 8 hours to the start time to
automatically enter the end time. Name & Number need nothing fancy.

I have 2 command buttons included which are labelled "+" & "-".
What I'm trying to do is to have the first one add a row of fields under
what's already there:

Name: Shift: Number:
[ ] [ ]-[ ] [ ]
[ ] [ ]-[ ] [ ]

...and the latter one bring up a form to select a name and remove the
fields associated with it.

I can make & manipulate the userform without any problem, but I
can't figure out how to make the fields appear where I want them
(immediately below the others, in the same cells).

I could probably get it to work by making a nested table of only
one column width for each field - code for which I found on a forum
somewhere - but although that was suitable for the forum member's
situation, that seems to be just a workaround here. Surely there must be
some way to position text entry formfields when you make them.

Can anybody please shed some light on this for me?

Thanks in advance,
Ken McLennan
Qld, Australia
 
D

Doug Robbins

This may give you a few pointers:

Sub addrow()

'

' Macro created 02/02/03 by Doug Robbins

' To add a new row to a table containing formfields in every column

' automatically on exit from the last cell in the present last row of the
table

Dim rownum As Integer, i As Integer

ActiveDocument.Unprotect

ActiveDocument.Tables(1).Rows.Add

rownum = ActiveDocument.Tables(1).Rows.Count

For i = 1 To ActiveDocument.Tables(1).Columns.Count

ActiveDocument.FormFields.Add
Range:=ActiveDocument.Tables(1).Cell(rownum, i).Range,
Type:=wdFieldFormTextInput

Next i

ActiveDocument.Tables(1).Cell(ActiveDocument.Tables(1).Rows.Count,
ActiveDocument.Tables(1).Columns.Count).Range.FormFields(1).ExitMacro =
"addrow"

ActiveDocument.Tables(1).Cell(ActiveDocument.Tables(1).Rows.Count,
1).Range.FormFields(1).Select

ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True



End Sub

--
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
Ken McLennan said:
G'day there One & All,

Please accept my apology if I've come to the wrong NG, although
the name seems inclusive of pretty much everything related to Word VP
Programming =).

I'm currently trying to write a bit of a Word application. I'm not
a trained, nor even a practised, macro writer but I have had a little
success with Excel applications for my employer. I'm still trying to
come to grips with Word's way of doing things and I've struck
difficulties.

I have a document with a table at the top of the page. It has only
a single row with 4 cells. The first contains day & date fields which I
have working the way I want. The next 3 cells are causing my problems.
After the headings I'm trying to get FormFields for data entry as
follows:

Name: Shift: Number:
[ ] [ ]-[ ] [ ]

As you can probably guess, the "Shift" fields are for start &
finish times. I'll write a macro to add 8 hours to the start time to
automatically enter the end time. Name & Number need nothing fancy.

I have 2 command buttons included which are labelled "+" & "-".
What I'm trying to do is to have the first one add a row of fields under
what's already there:

Name: Shift: Number:
[ ] [ ]-[ ] [ ]
[ ] [ ]-[ ] [ ]

...and the latter one bring up a form to select a name and remove the
fields associated with it.

I can make & manipulate the userform without any problem, but I
can't figure out how to make the fields appear where I want them
(immediately below the others, in the same cells).

I could probably get it to work by making a nested table of only
one column width for each field - code for which I found on a forum
somewhere - but although that was suitable for the forum member's
situation, that seems to be just a workaround here. Surely there must be
some way to position text entry formfields when you make them.

Can anybody please shed some light on this for me?

Thanks in advance,
Ken McLennan
Qld, Australia
 
C

Chuck

Try this - it adds form fields in the same cells in columns 2 and 3 of
whatever row is selected. The basic idea is to use ranges to position the
insertion point.

sub AddFields()

Dim lngRow As Long
Dim lngCol As Long
Dim rngRange As Range

lngRow = Selection.Information(wdStartOfRangeRowNumber)
lngCol = Selection.Information(wdStartOfRangeColumnNumber)

Set rngRange = Selection.Tables(1).Cell(lngRow, 2).Range
With rngRange
.InsertAfter vbNewLine
.MoveEnd wdCharacter, -1
.Collapse wdCollapseEnd
End With
Selection.FormFields.Add Range:=rngRange, Type:= _
wdFieldFormTextInput

Set rngRange = Selection.Tables(1).Cell(lngRow, 3).Range
With rngRange
.InsertAfter vbNewLine
.MoveEnd wdCharacter, -1
.Collapse wdCollapseEnd
End With
Selection.FormFields.Add Range:=rngRange, Type:= _
wdFieldFormTextInput
With rngRange
.Expand wdParagraph
.MoveEnd wdCharacter, -1
.InsertAfter "-"
.Collapse wdCollapseEnd
End With
Selection.FormFields.Add Range:=rngRange, Type:= _
wdFieldFormTextInput
With rngRange
.Expand wdParagraph
.MoveEnd wdCharacter, -1
.InsertAfter " "
.MoveEnd wdCharacter, -1
.Collapse wdCollapseEnd
End With
Selection.FormFields.Add Range:=rngRange, Type:= _
wdFieldFormTextInput

End Sub
 
K

Ken McLennan

G'day there Doug,
This may give you a few pointers:

Too bloody right it did!!!

I still haven't quite got it how I wanted, but it's well and truly
off to a running start!!

Thanks very much for your assistance, I really appreciate it.

See ya
Thanks again
Ken McLennan
Qld, Australia
 

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