VBA to Password Protect Files

  • Thread starter Herbert dela Cruz
  • Start date
H

Herbert dela Cruz

Hello everyone!

I would like to know how to programatically password protect a Word
document. I've recorded a macro to perform the function of saving the
file with a password, but when I modifed the recorded macro for use, it
didn't work - the file was saved, but without the password.

I'm currently using Word2000.

Thanks for your input! :D

- Herbert
 
C

Cindy M.

Hi Herbert,
I would like to know how to programatically password protect a Word
document. I've recorded a macro to perform the function of saving the
file with a password, but when I modifed the recorded macro for use, it
didn't work - the file was saved, but without the password.

I'm currently using Word2000.
It's always a good idea to show us the code that's not working...

FWIW, there are two ways in Word to assign a password: a parameter of the
SAVE method, or directly to the document, before saving. In my 15 years of
experience with Word, the former has never worked properly (not even in
the UI). I always assign the password explicitly: ActiveDocument.Password

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 
J

Jezebel

You can also execute the ToolsProtect dialog, although I'm not sure that's
the kind of protection the OP had in mind.
 
H

Herbert dela Cruz

Hi Cindy!
It's always a good idea to show us the code that's not working...

OK. Here's the code, but a little background first. :D I'm trying to
create an app that uses an OLE container to access Word documents. The
documents will be password protected, and the problem is with saving
them with the passwords intact. It's my first time to use OLE automation
and I wasn't able to find any documentation on how to directly save a
password protected document from the OLE container, so I thought of
saving the document using the OLE container's built-in "save" method.
After that, I open it using WordVBA, plug in the password, and then save
it, again using WordVBA.

== begin code ===
OleContainer.SaveAsDocument(FileName);
OleContainer.Close;
WordApp.Documents.Open (FileName, EmptyParam, EmptyParam,
EmptyParam, Passedword, EmptyParam, EmptyParam,
EmptyParam, EmptyParam, EmptyParam);
WordApp.ActiveDocument.Password := Passedword;
WordApp.ActiveDocument.Save;
WordApp.ActiveDocument.Close(EmptyParam, EmptyParam, EmptyParam);
=== end code ===

Passedword is a variable containing the desired password. At the moment,
the file gets saved, but it's not password protected.


Cindy and everyone, thanks for the help! :D
 
C

Cindy M.

Hi Herbert,

I won't be able to test, not having an OleContainer, so I'm limited to
makeing trouble-shooting suggestions...

If you open the document in the Word UI (no VBA code), assign a password,
save, close... Does the password "take", or do you still see the same
problem?

Also, check in Word's File/Open dialog box which *file type* Word is
opening. Is it a "Word document" or something else? Word can only save
passwords in Word documents. If the file type is RTF or something else,
that would explain why the password is being lost.
OK. Here's the code, but a little background first. :D I'm trying to
create an app that uses an OLE container to access Word documents. The
documents will be password protected, and the problem is with saving
them with the passwords intact. It's my first time to use OLE automation
and I wasn't able to find any documentation on how to directly save a
password protected document from the OLE container, so I thought of
saving the document using the OLE container's built-in "save" method.
After that, I open it using WordVBA, plug in the password, and then save
it, again using WordVBA.

== begin code ===
OleContainer.SaveAsDocument(FileName);
OleContainer.Close;
WordApp.Documents.Open (FileName, EmptyParam, EmptyParam,
EmptyParam, Passedword, EmptyParam, EmptyParam,
EmptyParam, EmptyParam, EmptyParam);
WordApp.ActiveDocument.Password := Passedword;
WordApp.ActiveDocument.Save;
WordApp.ActiveDocument.Close(EmptyParam, EmptyParam, EmptyParam);
=== end code ===

Passedword is a variable containing the desired password. At the moment,
the file gets saved, but it's not password protected.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 
H

Herbert dela Cruz

Hi Cindy!
If you open the document in the Word UI (no VBA code), assign a password,
save, close... Does the password "take", or do you still see the same
problem?

With Word UI, the document is saved with the password intact.
Also, check in Word's File/Open dialog box which *file type* Word is
opening. Is it a "Word document" or something else? Word can only save
passwords in Word documents. If the file type is RTF or something else,
that would explain why the password is being lost.

(about an hour later...)

You're right about the document formats; when I issue the
OleContainer.SaveAsDocument command, the contents of the OleContainer
(which is just an embedded Word application) do get saved as the
filename specified, including the ".doc" extension, but it isn't exactly
a Word document per se. It's a file with a ".doc" extension, neither
Word nor RTF, but Word somehow recognizes it as a "correct" Word
document. Correct in the sense that it doesn't generate any errors when
opening and saving.

Thanks for pointing it out - it helped save me a few more head bangs. :D

Cheers!
 
C

Cindy M.

Hi Herbert,
You're right about the document formats; when I issue the
OleContainer.SaveAsDocument command, the contents of the OleContainer
(which is just an embedded Word application) do get saved as the
filename specified, including the ".doc" extension, but it isn't exactly
a Word document per se. It's a file with a ".doc" extension, neither
Word nor RTF, but Word somehow recognizes it as a "correct" Word
document. Correct in the sense that it doesn't generate any errors when
opening and saving.

Thanks for pointing it out
thanks for reporting back the results of your research :) Good luck with
your project!

Cindy Meister
 

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