what code will unprotect a document.

H

H Dodson

Using Word 2000 and later, I need a code to put in my macro that will
unprotect the document. I am using a simple macro as below and the first
thing I want it to do is to unprotect the doc. Any ideas?
This is what I have thus far:

Selection.WholeStory
Selection.Copy
ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
End Sub
 
H

H Dodson

Sweet! Works like a champ. I will use it for protected documents and this is
exactly what I need, but for other users, I wanted to make less prone to
errors. If the document is not protected already, it gives me an error. Can
I put some code in there that will disregard the action for unprotected
documents? --
Thanks for your help!
H Dodson
 
G

Greg Maxey

H,

Try:

Sub Test()
If ActiveDocument.ProtectionType <> wdNoProtection Then
ActiveDocument.Unprotect
End If
'Rest of your code
End Sub
 
H

H Dodson

Greg,
The last code didn't work for me. Let me say that it very well could be
user error, for I a like a monkey plugging this code in. It means nothing to
me. This is what I have as my program. Is this what you intended for me to
do?

Sub Macro1()
'
' Macro1 Macro
' Macro recorded 5/6/2005 by Howard Dodson
'
CommandBars("Stop Recording").Visible = False
End Sub
Sub eForm_Copy()


Sub Test()
If ActiveDocument.ProtectionType <> wdNoProtection Then
ActiveDocument.Unprotect
Selection.WholeStory
Selection.Copy
ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
' eForm_Copy Macro
' Macro recorded 5/6/2005 by Howard Dodson
'
End Sub
 
G

Greg Maxey

H,

Don't feel bad about the monkey reference. It wasn't that long that I felt
the same way and I still have a long way to go. I think you where trying
for something like this:

Sub eForm_Copy()

If ActiveDocument.ProtectionType <> wdNoProtection Then
ActiveDocument.Unprotect
End If
Selection.WholeStory
Selection.Copy
ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges

End Sub

What does all that mean? A great way to learn is to select and item say
Unprotect and press the F1 key. I have never taken any formal courses or
read a book on VBA, but I have read thousands of posting and their answers.
Most experts tend to prefer using Range over Selection. So with your task
above, it could be written like this:

Sub eForm_Copy()
Dim oDoc As Document

Set oDoc = ActiveDocument
If oDoc.ProtectionType <> wdNoProtection Then
oDoc.Unprotect
End If
oDoc.Content.Copy
oDoc.Close SaveChanges:=wdDoNotSaveChanges

End Sub

If you pasted the above code in your VB Editor, select Content, then F1 you
will see that Content is a range object.

Note: Be sure to paste either of the above at the end of any other macro
you have in the open project. Don't paste it between another Sub - End Sub
routine.

HTH
 
H

H Dodson

Greg,
That did the trick. I tried it on both a protected doc and an unprotected
and it copied each one and closed w/o saving changes or pestering me about
it! So the code the you wrote is "cleaner" than what I had? More
stable/efficient? (It works regardless!) I appreciate the assistance. Now I
can go share w/ coworkers and shave some time off a repetative task!
 
G

Greg Maxey

Now I can go share w/ coworkers and shave some time off a repetitive task!

Good. I hope it makes you the hero for the day :)
So the code the you wrote is "cleaner" than what I had? More
stable/efficient?

What you had didn't work remember ;-)

Like I said, I have never had any formal schooling. From what I see, most
of the pros will use a range rather than a selection whenever possible. I
suppose it is more efficient. However in the case of the two versions we
are comparing here the difference wouldn't amout to the blink of an eye.
 

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