Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / June 2007

Tip: Looking for answers? Try searching our database.

Problems unprotecting template, running a macro and reprotecting

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
SMOO - 21 Jun 2007 16:55 GMT
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
Graham Mayor - 23 Jun 2007 10:38 GMT
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

Signature

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

My web site www.gmayor.com
Word MVP web site http://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>

> Good morning,
>
[quoted text clipped - 77 lines]
>
> SMOO
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.