Password Protecting Office Files...

S

Stretchcoder

I need to write code (Visual C++ 2005) to add a password (or encryption) to
various types of office files (Without opening the apps for the user to see).
Basically I want the user to enter a password, and if they try to open say a
..doc file later it will look to them like the file was protected in MS Word
watever version is on their PC.

This code must work for versions of Office 2000 and later, and must work
even if Office is not installed on a particular PC.

After stumbling around MS documentation for quite a while, I have had a
terrible time figuring out what dlls and other files I need to include to get
the automation to work for all the various Office apps, and what files will
actually need to reside on the PC with my exe in order for this all to work.

Any suggestions on where I should look to figure this out or a better way to
do it would be greatly appreciated!

I think I have figured out how to do this with Word 2003, it looks something
like:

Word::_ApplicationPtr wordapp;
// create new MS Word instance
wordapp.CreateInstance("Word.Application");
wordapp->Visible = false;
// open document file
_variant_t vtDocFile = filename;
Word::_DocumentPtr pDoc;
pDoc = wordapp->Documents->Open(&vtDocFile);
pDoc->ReadOnlyRecommended = false;
pDoc->Password = password;
pDoc->WritePassword = _T("");
pDoc->Save();
wordapp->Quit();

Thanks for the help!
 
S

Steve Rindsberg

I hate to say it, but I think this may be a hopeless quest.

In order to automate Office apps, they have to be installed on the PC. You're
not licensed to distribute any needed DLLs any more than you'd be licensed to
distribute Office itself. Automating Office w/o having Office installed is
simply a non-starter.

Note also that at least some Office apps (PowerPoint for one) don't support
password protection prior to Office 2002.
 
S

Stretchcoder

Steve (Or anyone else willing to help :)
What if I write the code to work as follows:

1 - Check to see that the appropriate app is installed on the user's PC
2 - If so, find out what version is installed
3 - Based on the version info, password protect accordingly (or tell the
user they are out of luck

If I do it this way, how do I include the appropriate DLLs during run time
rather than at compile? (This is more a C++ question, but I still hope you
can help!)

Thanks!
 
C

Cindy M.

Hi =?Utf-8?B?U3RyZXRjaGNvZGVy?=,
What if I write the code to work as follows:

1 - Check to see that the appropriate app is installed on the user's PC
2 - If so, find out what version is installed
3 - Based on the version info, password protect accordingly (or tell the
user they are out of luck

If I do it this way, how do I include the appropriate DLLs during run time
rather than at compile? (This is more a C++ question, but I still hope you
can help!)
Which dlls, more specifically, do you have in mind, here? Are you thinking
about references to the installed COM object libraries? I think you'd need to
look at "late-binding" (PInvoke) instead of directly addressing the object
model libraries, given this scenario. Here are some articles that may help

http://support.microsoft.com/kb/247579/en-us
http://support.microsoft.com/kb/302902/en-us

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 :)
 
S

Stretchcoder

I checked out the articles, but could really use a C++ code sample of late
binding with Office automation... any ideas?

Thanks again!
 
S

Steve Rindsberg

Steve (Or anyone else willing to help :)
What if I write the code to work as follows:

1 - Check to see that the appropriate app is installed on the user's PC
2 - If so, find out what version is installed
3 - Based on the version info, password protect accordingly (or tell the
user they are out of luck

If I do it this way, how do I include the appropriate DLLs during run time
rather than at compile? (This is more a C++ question, but I still hope you
can help!)

Not sure what DLLs you'd need to include. Could you not write your code so that it
branches based on the Office version (or lack of same) that you discover?
 
C

Cindy M.

Hi =?Utf-8?B?U3RyZXRjaGNvZGVy?=,
I checked out the articles, but could really use a C++ code sample of late
binding with Office automation... any ideas?
I searched the KB + MSDN yesterday, and didn't turn up any C++ examples.
Believe me, I tried. You might ask in a C++ group if anyone knows of any
examples for using PInvoke. But you'll be very lucky if you find any that
illustrate exactly the commands you want to use. Chances are, you're going to
have to slog through it the same way I did for the C# code I needed that
required PInvoke. Painful...

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 :)
 
S

Stretchcoder

I did indeed end up having to slog through it mostly on my own... Yuk!
However, I sure did learn a lot!

Thanks for the help!
 

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