Remove Password

M

matt

Hello,
I have a document that is protected by a password. And I have a macro that
sends the document in an email. But I have some other stuff in that macro
that removes some things before it sends it.

Before it removes the those things, I have it execute a:
ActiveDocument.SaveAs "Temp_Doc.doc"

And then after it executes the "SaveAs" it prompts me for the password again
within the new file "Temp_Doc.doc". How do I remove the password protection
while its saving or after it's saved programmatically, while still keeping it
in the origional document? Been trying to do this for HOURS!!!

Please Help!
Thanks
 
G

Gordon Bentley-Mix

matt,

Seems pretty straight forward to me - ActiveDocument.Unprotect just before
the SaveAs.

Think about this: You say that you have a macro that removes some "stuff"
from the document before sending it. This macro doesn't affect the original
document, does it? The "stuff" that's removed is only removed from the
ActiveDocument and then the changes are saved in the copy of the document
that's created by the SaveAs, so unprotecting the document before saving a
copy of it shouldn't affect the original either. And even if you do save the
"removed stuff" changes to the original, you should still be able to
unprotect the document and do the SaveAs without removing the protection from
the original...
--
Cheers!
Gordon

Uninvited email contact will be marked as SPAM and ignored. Please post all
follow-ups to the newsgroup.
 
G

Gordon Bentley-Mix

Some rough-n-ready sample code to show you what I mean. Note the order of
execution.

Private Sub EmailMe()
With ActiveDocument
If .ProtectionType <> wdNoProtection Then .Unprotect "myPassword"
.SaveAs "Temp_Doc.doc"
'Do the other stuff to the document to clean it up & email it
End With
End Sub

Protection is removed from the ActiveDocument and then the ActiveDocument is
saved with a new file name, leaving the original document intact - including
the protection.
--
Cheers!
Gordon

Uninvited email contact will be marked as SPAM and ignored. Please post all
follow-ups to the newsgroup.
 
M

matt

Hey Gordon thanks for the reply,
I won't be able to test this till I get to work tomorrow, but you know what
your absolutley right. I guess I never thought of it like that, about trying
to remove the protection and then the saveAs. I guess I just didn't have the
right order of commands

Using Word 2003 - I can't look at it now cause I have Office 2007 at home.

Is unprotect the same thing as assigning a password, because I'm pretty sure
I go to Tools/Options --> Security tab, and then I asigned a password in the
second textbox. But I think there is a button that says Protect or Protection
or something like that, and when you click it, it opens another box that
gives you some options to protect your document. And I'm positive I didn't
select anything in there, all I did was asign a password.
So will the "ActiveDocument.Unprotect" command be what I want to use?

I'll let you know tomorrow when I get to work what happens.

Thanks again,
Matt
 
G

Gordon Bentley-Mix

My apologies Matt. I got a bit confused between protection (forms, read-only,
etc.) and "password to open / modify" security. However, I've done some
rather extensive investigation, and I think I know what the problem is now.

You're getting prompted for the password to _open_ the document
"Temp_Doc.doc" when you try to save it, correct? What I think is happening is
that this document already exists - and in a version that has "password to
open" protection on it - and Word is trying to open this existing document
before saving the new document over it. I bet that if you delete the existing
"Temp_Doc.doc" between runs, you don't get prompted for the password. (I know
you can't test this now, but give it a go tomorrow.)

There are a couple of possible solutions to this problem, and being a bit of
a "belt-n-braces" man myself, I'd probably implement both of them just to be
sure.

First, just BEFORE you do your SaveAs, include the line:

ActiveDocument.Password = ""

This will remove the "password to open" protection from the document before
you save it as "Temp_Doc.doc", but it should leave the original document
intact. And with no "password to open" protection on the newly-created, you
shouldn't get prompted for the password in any subsequent runs.

Second, I'd look into writing some code to delete the "Temp_Doc.doc" after
you've sent it off. Investigate the "Kill" statement in VBA. Here is a bit of
code from one of my templates that contains similar functionality to help get
you started:

Private Sub cmdEMail_Click()
Dim MyOlapp As Object, MyItem As Object, MyAttachment As Object, MyFile
As Variant, olMailItem
On Error Resume Next

UserForm1.Hide
MyFile = Environ("Temp") & "\Kewl.doc"
ActiveDocument.SaveAs MyFile
Set MyOlapp = CreateObject("Outlook.Application")
Set MyItem = MyOlapp.CreateItem(olMailItem)
Set MyAttachment = MyItem.Attachments
MyItem.Subject = "Kewl Document Attached"
MyAttachment.Add MyFile
Documents(MyFile).Close (wdDoNotSaveChanges)
Kill MyFile
MyItem.Display
Unload UserForm1
End Sub

Note that this is very old code that I inherited from someone else so it's
probably not the most elegant example, but you should get the idea.
--
Cheers!
Gordon

Uninvited email contact will be marked as SPAM and ignored. Please post all
follow-ups to the newsgroup.
 
T

Tony Jollans

I may be missing something here but - in both 2003 and 2007 -

ActiveDocument.SaveAs "Temp_Doc.doc". Password:=""

- should save your document under a new name without a password.

Nothing to do with e-mail. You can do what you like with the document after
you've saved it, including e-mailing it to someone.
 
T

Tony Jollans

Sorry, I typed a full stop instead of a comma; that should be:

ActiveDocument.SaveAs "Temp_Doc.doc", Password:=""
 
M

matt

Yea you were right about having to delete the document first. After playing
around with this for hours I found that the only way was as you said to
delete the document before the code is run.
So about deleting a document programatically, there is no straight forward
way to do that?
 
M

matt

Many Thanks Gordon,
After hours and hours and hours, I finally got what I needed. And it was SO
simple. I think I maybe had a typo or something or the wrong order of
something but this was all I needed:

ActiveDocument.SaveAs FileName:="temp_document2.doc", Password:="", _
WritePassword:="", ReadonlyRecommended:=False

I want to thank you for taking the time to look into this.
Thanks again,
Matt
 
G

Gordon Bentley-Mix

Good catch Tony. I forgot that Password is an optional argument of the SaveAs
method.
--
Cheers!
Gordon

Uninvited email contact will be marked as SPAM and ignored. Please post all
follow-ups to the newsgroup.
 
G

Gordon Bentley-Mix

Matt,

Go back and look at the sample code in my previous post. I think the Kill
statement will do what you what you're looking for.
--
Cheers!
Gordon

Uninvited email contact will be marked as SPAM and ignored. Please post all
follow-ups to the newsgroup.
 

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