Protected Forms with Macros

D

Darlene

An end-user will be filling out the Word document form fields in a protected
docuemnt. I have created a table. Once they fill in data in the columns I
would like the totals columns to auto sum. I figure the best way to do that
was to have a macro run on entry in the field after the totals.

However, once I protect the document, the macro won’t run. I get a
Microsoft Bisual Basic Run-Time Error 4605 "This method or property is not
available because the document is a protected document".
 
D

Darlene

Thanks Graham,

But the end user will not know the password. Can I add into the macro
instructions to unlock and then lock/with password? I don't know how to do
that. Can you help?
 
G

Graham Mayor

Insert your password in the code between the quotes in the lines that end
Password:=""

Dim bProtected As Boolean
'Unprotect the file
If ActiveDocument.ProtectionType <> wdNoProtection Then
bProtected = True
ActiveDocument.Unprotect Password:=""
End If

'Do your thing

'Reprotect the document.
If bProtected = True Then
ActiveDocument.Protect _
Type:=wdAllowOnlyFormFields, NoReset:=True, Password:=""
End If


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
D

Darlene

Thank you so much!!! It worked.
--
Darlene


Graham Mayor said:
Insert your password in the code between the quotes in the lines that end
Password:=""

Dim bProtected As Boolean
'Unprotect the file
If ActiveDocument.ProtectionType <> wdNoProtection Then
bProtected = True
ActiveDocument.Unprotect Password:=""
End If

'Do your thing

'Reprotect the document.
If bProtected = True Then
ActiveDocument.Protect _
Type:=wdAllowOnlyFormFields, NoReset:=True, Password:=""
End If


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
N

NZ VBA Developer

Darlene/Graham,

You can also adapt this to unprotect and re-protect a document regardless of
the protection type, like this:

Sub MySub( )
'*** Create a Interger variable for storing the protection type
Dim intProtectType as Integer
intProtectType = ActiveDocument.ProtectionType

'*** Unprotect the document if it's not already
If ProtectType <> 0 Then
ActiveDocument.Unprotect "MyPassword"
End If

'*** Do the stuff you need to do to the unprotected document ***

'*** If the document was protected before, protect it again
'*** using the previous protection type
If ProtectType <> 0 Then
ActiveDocument.Protect ProtectType, True, "MyPassword"
End If
End Sub
 
W

Wendy

I am having the same problem as Darlene but I'm trying to run a
auto-numbering macro in a Word document with protected form fields. I want
the form to have a new Form# at the top every time I open it.
 
W

Wendy

i understand I have to modify my auto-numbering macro and insert the wording
below but could you be more specific to my situation. Here is a copy of my
current macro:

Sub AutoNew()
Dim MyString, docNumber
FileToOpen = "c:\Documents and Settings\wvannoy\My documents\Product
Complaints\docNumfile.txt"
Open FileToOpen For Input As #1
Input #1, docNumber
Close #1 'Close file
ActiveDocument.Bookmarks("docNum").Select
Selection.InsertAfter Text:=docNumber
docNumber = docNumber + 1
Open FileToOpen For Output As #1
Write #1, docNumber
Close #1 'Close file.
End Sub
 
G

Graham Mayor

The following saved in the form template will run (provided the document
c:\Documents and Settings\wvannoy\My documents\Product
Complaints\docNumfile.txt exists) each time you create a new form from the
template.
If you want it to run when you *open* the document then it needs to be
called AutoOpen and you will have to add code to remove the existing number
in order to update it. That being the case I would add another form field
and name it as docNum and use the second version to write the number to that
field. Personally I would use the second macro as an autonew macro in the
template and create new documents as required.

Sub AutoNew()
Dim MyString, docNumber
Dim i As Integer
Dim bProtected As Boolean

'Unprotect the file
If ActiveDocument.ProtectionType <> wdNoProtection Then
bProtected = True
ActiveDocument.Unprotect Password:=""
End If

FileToOpen = "c:\Documents and Settings\wvannoy\My documents\Product
Complaints\docNumfile.txt"
Open FileToOpen For Input As #1
Input #1, docNumber
Close #1 'Close file
ActiveDocument.Bookmarks("docNum").Select
Selection.InsertAfter Text:=docNumber
docNumber = docNumber + 1
Open FileToOpen For Output As #1
Write #1, docNumber
Close #1 'Close file.

'Reprotect the document.
If bProtected = True Then
ActiveDocument.Protect _
Type:=wdAllowOnlyFormFields, NoReset:=True, Password:=""
End If
End Sub


Sub AutoOpen()
Dim MyString As String
Dim docNumber As Integer
Dim oFld As FormFields
Set oFld = ActiveDocument.FormFields
Dim i As Integer
Dim bProtected As Boolean
Set oFld = ActiveDocument.FormFields

'Unprotect the file
If ActiveDocument.ProtectionType <> wdNoProtection Then
bProtected = True
ActiveDocument.Unprotect Password:=""
End If

FileToOpen = "d:\My Documents\Test\Versions\number.txt"
Open FileToOpen For Input As #1
Input #1, docNumber
Close #1 'Close file

With oFld("docNum")
.Result = docNumber
.Enabled = False
End With

docNumber = docNumber + 1
Open FileToOpen For Output As #1
Write #1, docNumber
Close #1 'Close file.

'Reprotect the document.
If bProtected = True Then
ActiveDocument.Protect _
Type:=wdAllowOnlyFormFields, NoReset:=True, Password:=""
End If
End Sub
 
G

Graham Mayor

In the second macro I have repeated the line
Set oFld = ActiveDocument.FormFields
remove the extra one.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
W

Wendy

Graham,
I have a bookmark, not a form field, where I wanted the original macro to
run. I made the changes to my original macro but it won't accept the line:
Type:-wdAllowOnlyFormFields, NoReset:=True, Password:=""
Also, do I need to change the name of this macro to AutoOpen?

I'm not understanding your suggestion to add another form field and name it
docNum. Do I add a form field where my original bookmark is, and add a new
macro titled docNum and remove the original bookmark?
 
G

Graham Mayor

The first macro would insert the information at your bookmark, however it
was not clear from your question how you were using the form. The best way
would be to save the form as a template and then create new documents from
that template. The first (autonew) macro would then write the number at the
bookmark you already have in your document.

This method causes problems if you are *opening* the document and want the
number to update as the last number would already be present. For this
reason, I suggested the second method which uses a form field instead of
your bookmark to display the number. Personally I would use the form field
method in a form whether or not it is used as a template or a document as it
is simple to replace the content with vba.

Whether you call the macro autoopen or autonew depends on how the document
is used. If it is a template from which you create new documents then the
macro should be autonew. If it is a document that is opened and re-used, it
should be autoopen. As already indicated the former is preferable.

The erronoeous line is an indication that you have only copied part of the
line. The full line should be:

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

or broken as shown

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

The full macro is as follows. How it is affected by any other code you have
in autonew I cannot say without seeing that code.

Sub AutoNew()
Dim MyString As String
Dim docNumber As Integer
Dim oFld As FormFields
Dim i As Integer
Dim bProtected As Boolean
Set oFld = ActiveDocument.FormFields

'Unprotect the file
If ActiveDocument.ProtectionType <> wdNoProtection Then
bProtected = True
ActiveDocument.Unprotect Password:=""
End If

FileToOpen = _
"C:\Documents and Settings\wvannoy\My documents\Product
Complaints\docNumfile.txt"
Open FileToOpen For Input As #1
Input #1, docNumber
Close #1 'Close file

With oFld("docNum")
.Result = docNumber
.Enabled = False
End With

docNumber = docNumber + 1
Open FileToOpen For Output As #1
Write #1, docNumber
Close #1 'Close file.

'Reprotect the document.
If bProtected = True Then
ActiveDocument.Protect _
Type:=wdAllowOnlyFormFields, NoReset:=True, Password:=""
End If
End Sub


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
W

Wendy

Now I'm thoroughly confused. Let me back up. Every time I open the document,
I need a new number to appear so that my forms are numbered consecutively. I
removed the bookmark at this location and added a form field.
1) How do I name this form field and tell the macro to insert my autonumber
here?
2) Am I adding two separate macros to this document? The first named
"AutoNew" and the second named "AutoOpen"?
 
G

Graham Mayor

With the form unlocked, double click the form field and change its bookmark
name from Textn (where n is the next number) to docNum.

If you are saving the document as a template *.dot (to create new documents
from) then the macro saved in the document needs to be called autonew.
If you are saving it as a document to be re-opened *.doc then the macro
should be called autoopen.

Change the macro name from my last message as appropriate and watch out for
the line wrapping prematurely in the line containing your text file path
name thanks to the message format.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
W

Wendy

Ok. When I added only the macro titled AutoNew, it ran one time without a
problem but when I saved the template and closed it and re=opened it, I got
an error message highlighting this line in yellow:

With oFld("docNum")
 
W

Wendy

I think the text wrapping is my problem. Not being familiar with macros, I'm
not sure what goes on what line.
 
G

Graham Mayor

I don't think this has anything to do with the macro code (which works as I
have indicated) but the way you are using the document/template.

A TEMPLATE is a layout from which new documents are created.

A DOCUMENT is the product of such a template when a new document is created
from it.

Can we establish how you are using the document/template?

The idea is that you insert the required field docNum in the form TEMPLATE.
This is a text form field renamed to docNum.

You then LOCK, SAVE and CLOSE the TEMPLATE containing the macro AUTONEW
macro (i.e. the macro runs when the new document is created), but without a
macro based on this code called AUTOOPEN.

You don't run the macro in the form template!

You then create a NEW document from the form using File > New. This will
create a new numbered form with all the fields from the template.

If you want another numbered form create ANOTHER new document.

The error associated with the line
With oFld("docNum")
indicates that the form field docNum no longer exists in the form.

The only line that has wrapped prematurely in the message is

"C:\Documents and Settings\wvannoy\My documents\Product
Complaints\docNumfile.txt"

which should all be on one line.

That aside you can copy and paste the rest into your vba editor, into the
module associated with the open form template. The code should not be in
normal.dot or it will run for EVERY new document, most of which will not
have such a field.

If it will help - see http://www.gmayor.com/installing_macro.htm

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 

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