Problems unprotecting template, running a macro and reprotecting

S

SMOO

Good morning,

I have a template that contains 3 macro buttons. I have the template
password protected but I would still like users to be able to use the macro
buttons. I keep running into two problems:

1. The macro buttons do not work at all when the template is password
protected
2. If I protect the form and then save it and reopen it the macro buttons
again do not work. But if I reopen the document and then reprotect it (with
out a password) the macros work fine.

Here are the macros I am trying to run:

A)

Option Explicit

Sub add_page()
If ActiveDocument.ProtectionType <> wdNoProtection Then
ActiveDocument.Unprotect Password:="summer"
End If

Selection.EndKey Unit:=wdStory
ActiveDocument.AttachedTemplate.AutoTextEntries("AddTable").Insert _
Where:=Selection.Range, RichText:=True
ActiveDocument.Fields.Update

ActiveDocument.Protect Type:=wdAllowOnlyFormFields, _
NoReset:=True
ActiveDocument.Bookmarks("Notes2").Range.Fields(1).Result.Select
End Sub


B)
Sub Information()
If ActiveDocument.ProtectionType <> wdNoProtection Then
ActiveDocument.Unprotect Password:="summer"
End If

frmInformation.Show

If ActiveDocument.ProtectionType = wdNoProtection Then
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, _
NoReset:=True
End If

End Sub


C)
Sub FormsSpellCheck()

' If document is protected, Unprotect it.
If ActiveDocument.ProtectionType <> wdNoProtection Then
ActiveDocument.Unprotect Password:="summer"
End If

' Set the language for the document.
Selection.WholeStory
Selection.LanguageID = wdEnglishUS
Selection.NoProofing = False

' Perform Spelling/Grammar check.
If Options.CheckGrammarWithSpelling = True Then
ActiveDocument.CheckGrammar
Else
ActiveDocument.CheckSpelling
End If

' ReProtect the document.
If ActiveDocument.ProtectionType = wdNoProtection Then
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, _
NoReset:=True
End If

End Sub

Any guidance would be greatly appreciated!


Warm Regards,

SMOO
 
G

Graham Mayor

The most obvious omission is that your macros do not re-password protect the
form after running so would get themselves hopelessly lost. Take the example
of the spell check. The code you need would be:

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

'Do your thing

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


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