MS Office Forum / Word / Programming / February 2008
Macros and Protect / Unprotect
|
|
Thread rating:  |
Diana Morrison - 20 Sep 2006 21:03 GMT I have created a form that a manager completes. I created a macro that changes the font to one colour, creates a header and saves to a certain file. It's run from a macro button on the end of the form. However, if the form is locked, i can't run it. Is there a way in the macro to first unlock the form, then run the macro, then password protect the form again?
Jay Freedman - 21 Sep 2006 01:54 GMT There shouldn't be any problem getting a macrobutton to run a macro, even in a protected form. The problem is that the macro can't make any changes to the document outside the form fields while the protection is turned on.
To work around this, set up your macro like this:
Sub DoSomething() With ActiveDocument If .ProtectionType <> wdNoProtection Then .Unprotect Password:="myPassWord" End If End With
' make your formatting changes here ActiveDocument.Protect Type:=wdAllowOnlyFormFields, _ NoReset:=True, Password:="myPassWord" End Sub
-- Regards, Jay Freedman Microsoft Word MVP FAQ: http://word.mvps.org Email cannot be acknowledged; please post all follow-ups to the newsgroup so all may benefit.
>I have created a form that a manager completes. I created a macro that >changes the font to one colour, creates a header and saves to a certain file. > It's run from a macro button on the end of the form. However, if the form >is locked, i can't run it. Is there a way in the macro to first unlock the >form, then run the macro, then password protect the form again? Diana Morrison - 26 Sep 2006 14:03 GMT This worked great - thanks!
One other tiny problem...after the new document is updated, protected and saved, when you re-open it the userform appears. How do i get rid of that?
Thanks, Diana
> There shouldn't be any problem getting a macrobutton to run a macro, > even in a protected form. The problem is that the macro can't make any [quoted text clipped - 28 lines] > >is locked, i can't run it. Is there a way in the macro to first unlock the > >form, then run the macro, then password protect the form again? Jay Freedman - 26 Sep 2006 20:30 GMT Do you have two macros in the template named AutoNew and AutoOpen? If so, remove the one named AutoOpen.
Read http://www.word.mvps.org/FAQs/MacrosVBA/DocumentEvents.htm for background on what happens when.
 Signature Regards, Jay Freedman Microsoft Word MVP FAQ: http://word.mvps.org Email cannot be acknowledged; please post all follow-ups to the newsgroup so all may benefit.
> This worked great - thanks! > [quoted text clipped - 38 lines] >>> way in the macro to first unlock the form, then run the macro, then >>> password protect the form again? Diana Morrison - 26 Sep 2006 22:19 GMT No - only AutoNew
> Do you have two macros in the template named AutoNew and AutoOpen? If so, > remove the one named AutoOpen. [quoted text clipped - 44 lines] > >>> way in the macro to first unlock the form, then run the macro, then > >>> password protect the form again? Jay Freedman - 27 Sep 2006 03:19 GMT Hmmm. A userform can't show itself; it *must* be called by a separate macro. And because it's showing up when the document opens, that macro has to be one of two kinds of "auto" macros.
Look in the ThisDocument module of the template -- is there a Document_Open routine there? (That's also mentioned in the article I pointed to.)
Are there any macros in the document itself? That's unlikely, because Word throws a security warning message or blocks macros altogether unless the security level is set to Low. But as Sherlock Holmes supposedly said, "When you have eliminated the impossible, what is left, no matter how unlikely, is the truth."
-- Regards, Jay Freedman Microsoft Word MVP FAQ: http://word.mvps.org Email cannot be acknowledged; please post all follow-ups to the newsgroup so all may benefit.
>No - only AutoNew > [quoted text clipped - 46 lines] >> >>> way in the macro to first unlock the form, then run the macro, then >> >>> password protect the form again? Diana Morrison - 27 Sep 2006 14:47 GMT Jay, that was the problem - a document_open routine in the This Document module. (I'm in WAY over my head). Where would i be without all the help I've received from you people over the years? Thank you so much. Diana Morrison
> Hmmm. A userform can't show itself; it *must* be called by a separate > macro. And because it's showing up when the document opens, that macro [quoted text clipped - 67 lines] > >> >>> way in the macro to first unlock the form, then run the macro, then > >> >>> password protect the form again? Jay Freedman - 27 Sep 2006 16:26 GMT You're welcome.
When you have some spare time(!), read that article. It'll help to get your head above water. <g>
I will say that it's unusual to find both an AutoNew macro and a Document_Open macro in the same template; developers usually adopt one pair of auto macros or the other.
 Signature Regards, Jay Freedman Microsoft Word MVP FAQ: http://word.mvps.org Email cannot be acknowledged; please post all follow-ups to the newsgroup so all may benefit.
> Jay, that was the problem - a document_open routine in the This > Document module. (I'm in WAY over my head). Where would i be [quoted text clipped - 80 lines] >>>>>>> it. Is there a way in the macro to first unlock the form, then >>>>>>> run the macro, then password protect the form again? Sin - 07 Nov 2006 23:42 GMT Hi Jay,
I've tried your code but realise one problem with it - i.e. I could only run the macro once (by clicking the macrobutton) on the protected form, the 2nd attempt onward won't work unless I close the document and re-open it. Are there anyway to work around it so that user can click on the macro as many time as they want without close and re-open the form?
> There shouldn't be any problem getting a macrobutton to run a macro, > even in a protected form. The problem is that the macro can't make any [quoted text clipped - 28 lines] > >is locked, i can't run it. Is there a way in the macro to first unlock the > >form, then run the macro, then password protect the form again? Jay Freedman - 08 Nov 2006 02:03 GMT I have no problem at all getting this macro to run multiple times without closing the document. Exactly what happens when you try it? "It doesn't work" is not a useful description.
It would help if you post your actual macro.
-- Regards, Jay Freedman Microsoft Word MVP FAQ: http://word.mvps.org Email cannot be acknowledged; please post all follow-ups to the newsgroup so all may benefit.
>Hi Jay, > [quoted text clipped - 36 lines] >> >is locked, i can't run it. Is there a way in the macro to first unlock the >> >form, then run the macro, then password protect the form again? Sin - 08 Nov 2006 03:25 GMT My macro actually unlock the form, change colors, print the form and re-lock the form. Once the form is printed, it will disable all other macrobutton on the page (I had another macrobutton on the page to display a msgbox which works multiple times as well). The problem seems to lie with this particular macrbutton only.
Sub test()
With ActiveDocument If .ProtectionType <> wdNoProtection Then .Unprotect Password:="tony" End If End With Dim tblCurrent As Table Dim lngUndoCount As Long Dim rgeStart As Range
Set rgeStart = Selection.Range
'Set to false to make code run faster 'In case of large document with many tables Application.ScreenUpdating = False
lngUndoCount = 0 For Each tblCurrent In ActiveDocument.Tables tblCurrent.Shading.BackgroundPatternColor = wdColorWhite lngUndoCount = lngUndoCount + 1 Next
With Application 'Reset to true or if user moves dialog around, 'there wil be traces on the screen .ScreenUpdating = True .Dialogs(wdDialogFilePrint).Show .ScreenUpdating = False
ActiveDocument.Undo lngUndoCount
rgeStart.Select
.ScreenRefresh .ScreenUpdating = True End With
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, _ NoReset:=True, Password:="tony"
End Sub
Thx sin
> I have no problem at all getting this macro to run multiple times > without closing the document. Exactly what happens when you try it? [quoted text clipped - 49 lines] > >> >is locked, i can't run it. Is there a way in the macro to first unlock the > >> >form, then run the macro, then password protect the form again? Jay Freedman - 08 Nov 2006 04:36 GMT This is only a guess, but here's what I think may be happening:
When you click the macrobutton to launch the macro, the Selection is on the macrobutton. As the macro starts, it assigns that location to rgeStart. Toward the end of the macro, you call rgeStart.Select, and then you protect the form.
If the Selection is in a non-form-field piece of text when you start form protection, the cursor is forced to the next form field. I think it's at that time the macrobutton loses it. The trouble is, I can't reproduce the behavior you're seeing in a simplified version here. My macrobutton just continues to work.
You could try putting the Selection in the next form field after the macrobutton instead. If that doesn't help, you can email the form to me if you want me to examine it.
-- Regards, Jay Freedman Microsoft Word MVP FAQ: http://word.mvps.org Email cannot be acknowledged; please post all follow-ups to the newsgroup so all may benefit.
>My macro actually unlock the form, change colors, print the form and re-lock >the form. Once the form is printed, it will disable all other macrobutton on [quoted text clipped - 102 lines] >> >> >is locked, i can't run it. Is there a way in the macro to first unlock the >> >> >form, then run the macro, then password protect the form again? Sin - 08 Nov 2006 06:43 GMT Sorry that I cannot send you the form since it's confidential.
Basically, the first 2 pages of the form is all writing with no form field, follwoing your comment below, what I found is that the macrobutton will work so long as it's placed on page with form field (regardless of the last cursor position) but it won't work if i place it on pages without form fields. Are you way to work around this?
> This is only a guess, but here's what I think may be happening: > [quoted text clipped - 126 lines] > >> >> >is locked, i can't run it. Is there a way in the macro to first unlock the > >> >> >form, then run the macro, then password protect the form again? Jean-Guy Marcil - 08 Nov 2006 15:48 GMT Sin was telling us: Sin nous racontait que :
> Sorry that I cannot send you the form since it's confidential. > [quoted text clipped - 3 lines] > (regardless of the last cursor position) but it won't work if i place > it on pages without form fields. Are you way to work around this? I wrote the restart code.... At the time, it was in a different thread. I had no idea that it was going to be used sandwiched between two Unprotect/Protect statements, and I did not know it was destined to be activated from a MACROBUTTON.
So, I never tested for that.
Now, instead of playing around with a MACROBUTTON, if you are dealing with a template, why don't you use a Toolbar button?
 Signature Salut! _______________________________________ Jean-Guy Marcil - Word MVP jmarcilREMOVE@CAPSsympatico.caTHISTOO Word MVP site: http://www.word.mvps.org
Sin - 08 Nov 2006 21:05 GMT The form is a downloadable template and the toolbar button won't be visible to other users.
I managed to work around the problem by putting in a invisiable field on the first page, as such, the button will work multiple times.
Thx Cindy
> Sin was telling us: > Sin nous racontait que : [quoted text clipped - 16 lines] > Now, instead of playing around with a MACROBUTTON, if you are dealing with a > template, why don't you use a Toolbar button? Jean-Guy Marcil - 09 Nov 2006 03:30 GMT Sin was telling us: Sin nous racontait que :
> The form is a downloadable template and the toolbar button won't be > visible to other users. > > I managed to work around the problem by putting in a invisiable field > on the first page, as such, the button will work multiple times. Glad you worked it out!
 Signature Salut! _______________________________________ Jean-Guy Marcil - Word MVP jmarcilREMOVE@CAPSsympatico.caTHISTOO Word MVP site: http://www.word.mvps.org
DJElliott - 21 Feb 2008 22:18 GMT Jay, I am still struggling to create a form in MS Word 2003 (XP). I want to use a macro button that allows a user to save/close a document and one that routes it. I tried using a macrobutton, but run into the protected/unprotected minefield. I double click on the macrobutton and nothing happens. :-)
Public Sub SaveIt() ' With ActiveDocument If .ProtectionType <> wdNoProtection Then .UnProtect Password:="" End If End With ' 'Enable ENTER key on exit Application.Run MacroName:="ReinstateEnterKey" MsgBox Prompt:="SAVES and CLOSES supplier maintenance document, but does not route it." If ActiveDocument.Saved = False Then ActiveDocument.SaveAs ActiveDocument.protect Type:=wdAllowOnlyFormFields, _ NoReset:=True, Password:="" 'Close saved supplier maintenance form ActiveDocument.Close End Sub
 Signature DJELLIOTT
> There shouldn't be any problem getting a macrobutton to run a macro, > even in a protected form. The problem is that the macro can't make any [quoted text clipped - 28 lines] > >is locked, i can't run it. Is there a way in the macro to first unlock the > >form, then run the macro, then password protect the form again? Jay Freedman - 22 Feb 2008 01:33 GMT The problem with using a macrobutton field in a protected form is that you can't really double-click it. Word sees the first click as an attempt to select something in the protected area of the form, and before your second click registers as being part of a double-click, the cursor is forced into the next available form field. The macro never gets invoked.
The solution is another macro that tells Word to execute the macrobutton field on a single click instead of a double-click. See the "Double-click or single-click" section of http://www.word.mvps.org/FAQs/TblsFldsFms/HLinksInForms.htm.
>Jay, I am still struggling to create a form in MS Word 2003 (XP). I want to >use a macro button that allows a user to save/close a document and one that [quoted text clipped - 20 lines] > ActiveDocument.Close >End Sub -- Regards, Jay Freedman Microsoft Word MVP FAQ: http://word.mvps.org Email cannot be acknowledged; please post all follow-ups to the newsgroup so all may benefit.
Graham Mayor - 22 Feb 2008 07:18 GMT Or use a custom toolbar button rather than a macrobutton?
 Signature <>>< ><<> ><<> <>>< ><<> <>>< <>><<> Graham Mayor - Word MVP
My web site www.gmayor.com Word MVP web site http://word.mvps.org <>>< ><<> ><<> <>>< ><<> <>>< <>><<>
> The problem with using a macrobutton field in a protected form is > that you can't really double-click it. Word sees the first click as [quoted text clipped - 32 lines] >> ActiveDocument.Close >> End Sub
|
|
|