Fill - in form - add rows to table

D

danka

Hello:

I created a form in Word that has a table. I placed form fields in the
table. I do not know how many rows will be entered up front. Is is possible,
to add ADD row option to this table with or without programming?

I woud appreciate your response to this urgent question.

Thanks.
 
D

Doug Robbins - Word MVP

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
 
C

Charles Kenyon

When I do this, I save a table row containing the form fields as an AutoText
entry and use vba to unprotect the form, insert the table row with the
AutoText, and then reprotect the form.
--
Charles Kenyon

Word New User FAQ & Web Directory: http://addbalance.com/word

Intermediate User's Guide to Microsoft Word (supplemented version of
Microsoft's Legal Users' Guide) http://addbalance.com/usersguide




--------- --------- --------- --------- --------- ---------
This message is posted to a newsgroup. Please post replies
and questions to the newsgroup so that others can learn
from my ignorance and your wisdom.
 
D

danka

Hello Doug:

I have an indirect question before I will try the solution. If I use
macros and distribute a document to other users would they be asked if they
want to enable macro (if they have security level set to eg. medium level (in
Word). I am not sure about High level. Can you automate Word Form
using VBA or macro and be able to distribute documents containing them to
other users without those questions? Please help.
 
C

Charles Kenyon

Distribute it as a template, not a document. Have them put the template in
their user templates folder or in the workgroup templates folder. If they
check the macro security option to trust installed templates and add-ins
they will not get a question about enabling your macros. For more on the
different kinds of templates, tabs on the file new dialog, and locations of
templates folders see http://addbalance.com/usersguide/templates.htm.
--

Charles Kenyon

Word New User FAQ & Web Directory: http://addbalance.com/word

Intermediate User's Guide to Microsoft Word (supplemented version of
Microsoft's Legal Users' Guide) http://addbalance.com/usersguide


--------- --------- --------- --------- --------- ---------
This message is posted to a newsgroup. Please post replies
and questions to the newsgroup so that others can learn
from my ignorance and your wisdom.
 
D

danka

Hello Charles:

Thanks for your response - workgroup template does not trigger macro
security as you said. This method, I believe would require to have a
pointer to the network template location set on each PC using my Word form
with macro/VBAs? Is this correct?

User would need to fill the form (form fields) and save file on the local
drive. Would that be possible with the workgroup template?
Originally, I was thinking about having this file on the webserver for
filling the form and/or saving but this triggers macro security on the user
PC.
 
D

Doug Robbins - Word MVP

Templates stored in the Work Group Templates folder as set under
Tools>Options>File Locations and under the User Templates folder as also set
in Tools>Options>File Locations, all appear in the File>New dialog which is
the dialog that appears when a user selects New from the File Menu.

So that answer to your question is "Yes"

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

Charles Kenyon

You would either have the Workgroup templates folder set as a network folder
for each user, or you would have a local folder for each user that is
updated from the network location as a part of each user's network login.

The user, uses File > New, picks your template, and creates a new document.
That document is saved locally.
--
Charles Kenyon

Word New User FAQ & Web Directory: http://addbalance.com/word

Intermediate User's Guide to Microsoft Word (supplemented version of
Microsoft's Legal Users' Guide) http://addbalance.com/usersguide




--------- --------- --------- --------- --------- ---------
This message is posted to a newsgroup. Please post replies
and questions to the newsgroup so that others can learn
from my ignorance and your wisdom.
 
D

danka

I like the update of the local folder from the net as part of the login. I
would have to obtain an approval for having it included in the loging script
- and sometimes it can prove difficult. The first method includes going to
each workstation and setting the option for workgroup template or asking
users to do it - is it correct?
 
C

Charles Kenyon

Yes, it is correct. Actually, both options include setting up workgroup
templates folders for users.
--
Charles Kenyon

Word New User FAQ & Web Directory: http://addbalance.com/word

Intermediate User's Guide to Microsoft Word (supplemented version of
Microsoft's Legal Users' Guide) http://addbalance.com/usersguide




--------- --------- --------- --------- --------- ---------
This message is posted to a newsgroup. Please post replies
and questions to the newsgroup so that others can learn
from my ignorance and your wisdom.
 
D

danka

Hello Doug:

Thanks for sending me a macro. This code adds a new raw to a table
containing form fields in each column.

When you tab from the last cell a new raw is created containing form fields.
It does not however transfer the fields options eg. maximum lenght, type:
dropdown or text, etc , are not copied to a new raw. It it possible to have
fields option created in each new raw that is added? A form only makes sense
when the fields options are included in the next row.

Can you help?

Please help.
 
D

danka

Hello Doug:

This code adds a new row to table with fields but it does not transfer form
field options to new rows, eg. length, type, etc. Is there any solution here?

Thanks.
 
D

danka

Hello Charles:

I can save and apply Autotext option interactively. I believe, I need to
write a macro/vba and first I need to unprotect form. Do I need some sort
of button or toolbox with the code that would reference autotext object? How
do I refrence Autotext control?

Can you help?
 
C

Charles Kenyon

First you need to save the AutoText entry. This would be a row of your table
containing your form fields. I don't think this can be done in a protected
form, so you would need to unprotect the form.

I don't know what an AutoText control is.

The following macro inserts the AutoText entry "MyRow" at the end of table
1. In Word 2003 this adds a row when the AT entry is a table row. When the
macro finishes, your form is protected and the first field in the new row is
selected. I believe different techniques would be required for Word 97, and
possibly Word 2000.

Note, you need to give the macro the name of your AutoText entry, which must
be stored in your template, the number of the table, and the protection
password. These are set as variables to ease changing them in the code.

Sub InsertATRow()
'
' InsertATRow Macro
' Macro written 7/7/2006 by Charles Kyle Kenyon
'
' Declare variables
Dim iRows As Integer
Dim iTable As Integer
Dim sPassword As String
Dim sATEntry As String
' Pick table, set AT entry name and assign password
iTable = 1
sATEntry = "MyRow"
sPassword = "mypass"
' Unprotect Document using subroutine
UnprotectDocumentMacro (sPassword)
' Select Table
ActiveDocument.Tables(iTable).Select
' Move past table to insertion point
Selection.MoveDown Unit:=wdLine, Count:=1
' Insert AutoText entry - will generate error if entry does not exist
ActiveDocument.AttachedTemplate.AutoTextEntries(sATEntry).Insert _
Where:=Selection.Range, RichText:=True
' Get number of rows in table so we can pick the last one
iRows = ActiveDocument.Tables(iTable).Rows.Count
' Select new row
ActiveDocument.Tables(iTable).Rows(iRows).Select
ProtectDocumentMacro (sPassword)
End Sub


Sub UnprotectDocumentMacro(Optional sPassword As String)
' Unprotect document
If ActiveDocument.ProtectionType = wdAllowOnlyFormFields Then
ActiveDocument.Unprotect Password:=sPassword
End If
End Sub

Sub ProtectDocumentMacro(Optional sPassword As String)
If ActiveDocument.ProtectionType <> wdAllowOnlyFormFields Then
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, _
noreset:=True, Password:=sPassword
End If
End Sub

--
Charles Kenyon

Word New User FAQ & Web Directory: http://addbalance.com/word

Intermediate User's Guide to Microsoft Word (supplemented version of
Microsoft's Legal Users' Guide) http://addbalance.com/usersguide




--------- --------- --------- --------- --------- ---------
This message is posted to a newsgroup. Please post replies
and questions to the newsgroup so that others can learn
from my ignorance and your wisdom.
 
D

Doug Robbins - Word MVP

There is no reason that the code I provided cannot be modified to perform a
specific task. To make a generic macro that will handle countless number of
different options is however not worth the effort.

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

danka

Hell Charles:

Thank you very much for sending me your coed. I tried this code and it
works perfectly - but only on my machine.

I guess, this because I have AnAuto entry defined and the other machine not.
Is there any way to handle this problem?

I have a piece of code that copies rows and fields on exit from the last
row/field, but it does not transfer the fields properties - they are all
default text fields. So this does not quite work.

Do you have any suggestions?
 

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