Log in

View Full Version : [SOLVED:] Code to Temporarily Remove Restricted Editing to run a macro?



Hollins2007
03-07-2020, 10:39 PM
Hello gurus

I'm not familiar with VBA or coding at all, but am learning as I go here.

I have a form Office 365 macro-enabled Word document that will be distributed and used by hundreds of people, so it must have editing restricted on it (the "Filling in Forms" restriction).

However, I have created buttons in the Quick Access Toolbar with macros that will be making changes to the document, and so will require the restricted editing to be removed. I would like to know how I could temporarily remove the protection and then enable it again once the macro has finished running.

For reference, I have one button that will allow users to remove certain pages, and one button that will allow users to duplicate certain pages. The buttons and macros work fine themselves when I have the Restricted Editing protection OFF, but I didn't factor that in when I put it together initially and have only just discovered that these buttons won't work with the restriction on.

Here's one of the codes I'm working with (the one that duplicates specific pages):

Sub AddGrantorPage()
'Duplicates any page in the document


Page = InputBox("Enter the Page to Duplicate")
Count = InputBox("Enter Number of times to duplicate")
With Selection
.GoTo wdGoToPage, wdGoToAbsolute, Page
.Bookmarks("\Page").Range.Copy
For i = 1 To Count: .Paste: Next


End With


End Sub


I did some internet searching and found these two particular lines of code that I thought might work to remove the protection temporarily, but when I insert them at the beginning and end of the code that I currently have, it returns "runtime error 424: Object required"... so I am not sure if the code isn't what I need, or if I am just using it incorrectly:

Sub AddGrantorPage()
'Duplicates any page in the document


oDoc.Unprotect Password:="my password"
Page = InputBox("Enter the Page to Duplicate")
Count = InputBox("Enter Number of times to duplicate")
With Selection
.GoTo wdGoToPage, wdGoToAbsolute, Page
.Bookmarks("\Page").Range.Copy
For i = 1 To Count: .Paste: Next
oDoc.Protect Type:=wdAllowOnlyFormFields, NoReset:=True, _
Password:="my password"

End with

End Sub

I'm sure there is probably a simple fix, but my knowledge of code is so little that I don't even know if I am on the right track.

Thanks so much in advance for any insight or suggestions you might have!

macropod
03-08-2020, 12:34 AM
Change:
oDoc
to:
ActiveDocument

Hollins2007
03-08-2020, 03:49 AM
Ah, yes. that seems such a simple fix now that I see it there. haha

Just checked it out and it does work for all of my buttons/macros. My document is now ready to go! Perfecto!

Thanks so much for your quick assistance. Might seem simple, but you've saved me literal hours here as a vba newbie trying to figure that out. Can't show my appreciation enough. :bow: