need help with command button on forms

T

Tina

I created a protected form with 11 form fields for users to fill out. I
saved it as a template so they can begin with a blank form each time they
need it. There are times when a user will need to create several forms with
the same information in the first 5 fields. Instead of the user opening a
new document for each additional form they need to submit, (and re-entering
the first 5 fields), what I'd like to do is put in a command button that when
clicked, copies the entire form (with the first 5 fields already filled in
and the remaining 6 fields reset to blank) and then paste this information on
a new page at the end of the form. This way, the original form may have 2
or more additional forms all in one document instead of several separate
documents.

I tried creating a macro but can't get past the fact that the form is
protected. Is this even possible or am I overcomplicating it?

Thanks!!
 
D

David Sisson

I have something similer, however, I use a document with a table that
logs all the formfield data. From this master table, I run a macro
that displays a Userform. The user selects option buttons to select
which forms to fill out, and then the macro builds the empty complete
form from templates. I would suggest you build one document for a
jumping off point, even if you don't log the data, this simplifies
storing the macro. You could have a macro in each form, but when a
change is made, you have to change them all. This usually comes back
to haunt you in one way or another.

Create a header template, plus one template for each form minus the
header.

You also could have the user fill out the header info in the userform,
then pass that info to the header template.

Please reply if this sounds like a solution for you.
 
T

Tina

David - Thanks for the info. I'm confused :-(

How does the table log the formfield data?
I only have the one form so I don't know that I really need an option button?
Are the form fields stored in the header?

I guess I just am not following. Can you simplify please?


Thanks,
 
D

David Sisson

David - Thanks for the info. I'm confused :-(
Sorry, I guess I rambled a bit.

Let me see if I understand what you've described.

In the end, your user will have five different docs (or pages), but
each one will have the same header information?
 
T

Tina

The template has a table where the 11 form fields are located. (The form
fields aren't in the header.)
The user can fill out and save the form and this would be one document.
However, at times, a user would need to fill out several forms and in this
case 5 of the 11 fields within the table would all hold the same info. In
this instance, instead of entering the same info for 5 fields, several times,
I'd like to use a command button that, when clicked, copies the entire form
to page 2 (and again to page 3, etc. depending on how many forms need to be
filled out) within the same document but the first 5 fields will be copied
and the remaining fields will be reset to blanks.

I hope that makes more sense. My problem is getting the command button to
work on a protected form.

Thank you!!
 
D

David Sisson

Here are both versions.

The first one does as you ask, it copies the document and adds it to
the bottom. The second one adds a new document based on the template.

The first one suffers from 'Runs right the first time' symdrone. The
first time it runs, it correctly copies the entire document (which
comprises on only one page), along with the data, and pastes it to the
bottom. But if you need to add 'just one more page', then the entire
document is much longer than the original and so multiplies
exponentinally.

I haven't quite worked out how to only select page one of a multi-page
document, but this will get you started.

I'll work on it some more this weekend.

If you password protect your template, you'll have to add the password
to the unprotect/protect lines accordingly.

In your template, add this code under Module 1. You may rename the
Subs if you like. Then, go back to the template, right click on a
empty spot on the toolbar, select Customize at the bottom. In the
left pane, scroll down to Macros, click once. In the right pane, you
should now see the two macro names Drag one or both of them to the
Menu Bar. Rename them again if you like. Save the template.

Now, File -> New and select the template. The buttons will be
available to the document, through the template.

Click on the button to run the macro.

David

Sub CopyTopFive()
Dim aDoc As Document
Dim Rng As Word.Range
Dim NumCopies As String

Set aDoc = ActiveDocument

If aDoc.ProtectionType <> wdNoProtection Then aDoc.Unprotect

NumCopies = InputBox("How Many Copies?", "Copying Document")
If NumCopies = vbNullString Then End

aDoc.Range.Copy

For A = 1 To CInt(NumCopies)
Set Rng = ActiveDocument.Bookmarks("\EndOfDoc").Range.Duplicate
Rng.InsertBreak wdSectionBreakNextPage
Set Rng = ActiveDocument.Bookmarks("\EndOfDoc").Range.Duplicate
Rng.Paste
Next

If aDoc.ProtectionType = wdNoProtection Then aDoc.Protect
wdAllowOnlyFormFields, True

End Sub


Sub Add_a_Blank()
Dim aDoc As Document
Dim bDoc As Document
Dim TemplatePath As String
Dim NumCopies As String
Dim A As Integer

Set aDoc = ActiveDocument

NumCopies = InputBox("How many new documents? ", "Adding Documents")
If NumCopies = vbNullString Then End

'Change path accordingly.
TemplatePath = "C:\Documents and Settings\dsisson\Application Data
\Microsoft\Templates\"

For A = 1 To CInt(NumCopies)
Set bDoc = Documents.Add(Template:=TemplatePath & "my
Template.dot")
bDoc.FormFields(1).Result = aDoc.FormFields(1).Result
bDoc.FormFields(2).Result = aDoc.FormFields(2).Result
bDoc.FormFields(3).Result = aDoc.FormFields(3).Result
bDoc.FormFields(4).Result = aDoc.FormFields(4).Result
bDoc.FormFields(5).Result = aDoc.FormFields(5).Result
Set bDoc = Nothing
Next

End Sub
 
D

David Sisson

FInal version.. This corrects the exponential problem.

Sub CopyTopFive2()

Dim aDoc As Document
Dim Rng As Range
Dim Rng2 As Range
Dim NumCopies As String

Set aDoc = ActiveDocument
Set Rng = aDoc.Content

If aDoc.ProtectionType <> wdNoProtection Then aDoc.Unprotect

NumCopies = InputBox("How Many Copies?", "Copying Document")
If NumCopies = vbNullString Then End

With Rng.Find
.ClearFormatting
.Text = "^b"
.Forward = True
.Wrap = wdFindStop
End With
Rng.Find.Execute
If Rng.Find.Found Then
Set Rng = aDoc.Range(0, Rng.End - 1)
Rng.Copy
Else
aDoc.Range.Copy
End If

For A = 1 To CInt(NumCopies)
Set Rng = ActiveDocument.Bookmarks("\EndOfDoc").Range.Duplicate
Rng.InsertBreak wdSectionBreakNextPage
Set Rng = ActiveDocument.Bookmarks("\EndOfDoc").Range.Duplicate
Rng.Paste
Next

If aDoc.ProtectionType = wdNoProtection Then aDoc.Protect
wdAllowOnlyFormFields, True


End Sub
 

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