How to validate a Word document...?

R

RobKinney1

Hello, I am developing an application in C++ that opens a Word document. It
needs to validate that it is a valid Word document and then close it.

Well, I can open and close it, but it seems that Word will open practically
anything! I can open exe’s, dll’s, etc. The all open without error. Is
there any type of checking I can do in the document to make sure that this is
a valid Word document (aside from checking the file extension, you can never
trust what a customer really uploaded).

Any help is appreciated. Thank you all for reading this.

Rob K
 
R

RobKinney1

Ok, I have found out a solution and decided to post it here for any other
poor soul that is struggling with it in the future.

The 2 articles I have used from Microsoft are Q186898 and Q238393.

Q186898 explains how to open a file and grab some preliminary information
such as verifying if it is a valid valid OLE compound document (valid Word or
Word Perfect document). It also allows you to check the security field
incase there is password protection for opening the file. That way, when you
open the file from your C++ code later, knowing this can prevent that
security dialog to come up and cause your program to hang. You can just
reject the document at this point until the user has fixed it.

Q238393 shows how you can open your Word document from the command line and
after eliminating all the MessageBoxes, can be a pure command line driven
app. The artical does however fail to mention that if you want to open a
document that is not hard-coded as C:\Doc1.doc, you must assign the value
like so using CStrings (it is the only way I could get it to work, but I had
to convert the app so that it was using MFC in a Static Library first):

Instead of:
x.bstrVal = ::SysAllocString(L"C:\\Doc1.doc");

use:
inputFileCString = "obtainedEarlierDynamically.doc";
x.bstrVal = inputFileCString.AllocSysString();

Then everything works like a charm. It takes a bit of work to merge these 2
programs together and figure out how to pull the right data from the
document, but after you do it makes a great stable server app.
 

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