MS Word Automation help

S

scottmallory

Hello,
I am trying to find a method via VBA or VB that will determine whether
a MS Word Doc is password protected.
A boolean result would be the best..

OR

can I trap for the Password Dialog that opens up when a passworded doc
is opened and handle it myself via code?


Some Background
We are trying to page out (each page as a seperate doc) a document
that may or may not be passworded, a a background function of an
application.

What we want to do it manage the password dialog event ourselves so we
can keep word hidden away, and pass in the passwords in our open call
as needed.

FYI: Word stays open when a password is required and looks like crap
while we process 20 or more pages.

Thanks
Scott
 
S

Shauna Kelly

Hi Scott

I'm not sure that I really understand what you're trying to do, but
would the following help:

Dim oDoc As Word.Document

Set oDoc = Word.Documents.Open _
(FileName:="C:\wherever\whatever.doc", _
PasswordDocument:="PasswordToOpenThisDoc")

There are, of course, risks associated with hard-coding a password. If
your document holds the destruct sequence for a nuclear power station
this would not be a good way to do it. If your document is protected to
prevent people from using the wrong version of the company logo, it
might not matter so much. Use your discretion.

Hope this helps.

Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word
 
S

scottmallory

I over explained it...

I am looking for a method to determine
IF the word doc HAS a password BEFORE I try to open it.

I'm thinking something like
pDoc = Word.Documents.File("C:\somefile.doc")
res = pDoc.IsProtected()

Is this even possible?


We are trying to keep word in the background as much as possible
We're processing several hundred files, containing 100's of pages

Scott
 
S

Shauna Kelly

Hi Scott

Just to be sure we're on the same track, I am assuming you are asking
about the password required to *open* a document (set at Tools > Options
Security), not the password required to edit a document that has been
protected (set at Tools > Protect Document).

As far as I know, there is no way to ascertain whether a document
requires a password to open it.

However, Microsoft provides a useful file dsofile.dll at
http://support.microsoft.com/kb/224351. It lets you read document
properties of a Word document from outside Word. I don't know whether it
can tell you whether a document needs a password to open the document,
but a bit of experimentation should determine whether it will meet your
needs.

Hope this helps.

Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word
 
M

msnews.microsoft.com

I'm know I'm late to this thread, but I was searching for something similar
and after my search resulted in zero answers, I created a solution that may
also help you.

I need to recurse a directory tree, open up each document in Word, and query
and report some statistics. Of course if a document is password protected,
the code will stop as Word displays the "Password" dialog box, waiting for
the user to enter a password and click the OK button or click the Cancel
button. Here's a simple function to tell you whether a document has a
"Password to Open" password set.

Since I'm not modifying the document, I attempt to open each document as
read only, but just in case a "Password to Open" password is set, I provide
a password which is probably invalid (a blend of Einstein & Yogi Berra), I
trap the error and return a Boolean value indicating whether the document is
password protected. I hope this helps.

Public Function blnPasswordProtected(strFilePath As String) As Boolean
Dim docToOpen As Word.Document

On Error Resume Next
Set docToOpen = Word.Application.Documents.Open(strFilePath, False, True,
False, "E=M*YogiBerra2")
If Err.Number > 0 Then
blnPasswordProtected = True
Else
blnPasswordProtected = False
End If ' Err.Number > 0
docToOpen.Close
Set docToOpen = Nothing
End Function ' blnPasswordProtected(strFilePath As String) As Boolean
 

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