PDA

View Full Version : Table copy and reset form fields



samallred
04-14-2008, 11:02 AM
I don't think this is possible from my perspective and what I have tried to do. I have a word table with form fields in it. The user can click a button and get a copy of the table just fine. The problem I am having is that the copied table needs to have its form fields cleared out.

When I try to use the protect/unprotect of the document it will clear the data if I set NoReset to False or it will leave the data if I set NoReset to True.

All I want to do is reset the form fields on the table I am pasting below the original table.

Any ideas?

Here's what I have presently which I use to copy the table:


If ActiveDocument.ProtectionType <> wdNoProtection Then
ActiveDocument.Unprotect
End If

Dim r As Range
Set r = ActiveDocument.Bookmarks("itemTable").Range
r.Copy

Selection.EndKey Unit:=wdStory, Extend:=wdMove
Selection.InsertBreak Type:=wdSectionBreakNextPage

Selection.Paste
Set r = Nothing


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


After pasting or before pasting, how can I reset the form fields?

Thanks.

VBA Tags added. Select your code and hit the VBA button when posting to format your code in the forum.

fumei
04-14-2008, 11:25 AM
1. Please use the VBA code tags when posting code.

2. NoReset is global. It either resets ALL the formfields, or does not reset ALL the formfields. You can not reset some formfields.

3. Copying formfields themselves is generally not a good idea. Why? Because copied formfields have NO name, nor are they marked as bookmarks.


Why are you doing this? Why are you copying the table below itself?

In other words, what is the purpose behind this?

BTW: you could of course gather the data from the original formfields, store it, use ReSet = True, and put the original values back into the original formfields.

OR....

loop through the formfields in the copied table, and make them blank. Like this:

Sub whatever()
Dim oFF As FormField
Dim r As Range
If ActiveDocument.ProtectionType <> wdNoProtection Then
ActiveDocument.Unprotect
End If


Set r = ActiveDocument.Bookmarks("itemTable").Range
r.Copy

Selection.EndKey Unit:=wdStory, Extend:=wdMove
Selection.InsertBreak Type:=wdSectionBreakNextPage

Selection.Paste
Set r = Nothing
' loops through formfield in LAST table in document
' ie. the new one
For Each oFF In ActiveDocument.Tables(ActiveDocument.Tables.Count) _
.Range.FormFields
oFF.Result = ""
Next

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

End Sub

samallred
04-16-2008, 09:54 AM
Thanks for your reply.

I have a document that has a table with form fields including some check boxes. The table represents an item the user adds. They may want to have more than one item so I created a button that would copy the table and add an new table so they could add another item. Each table represents an item. Some may need many items. Right now if the user fills the table out and wants to add another I would like to copy the table and add a new one below it but have the field forms reset only in the new table. The original should remain the same.

My approach of how to do this task is probably wrong. I haven't copied the form fields and then added them back before. Any code samples on how to do this? Also, to reset the form works for the text form fields but not for the check box form fields.

I am not new to programming but am new to Words style of doing things. Thanks for any suggestions and help you might offer.

samallred
04-16-2008, 02:56 PM
The code you included in your first response worked for me. I now have what I need to make the table copy work.

Thanks!