Link to home
Start Free TrialLog in
Avatar of ILGDRM
ILGDRM

asked on

How to get word document content

Hi ,
      After opening the word document through the following code we can get the Text content of Word Document.

IDispatch* pDispRange = oDocument.GetContent();
Range objRange(pDispRange);
AfxMessageBox(objRange.GetText());

How to get the entire word document content ( text, images and tables) in Byte Array.
Avatar of alb66
alb66
Flag of Italy image

Why use Automation to get the binary content?
You can use the standard file function.
Avatar of ILGDRM
ILGDRM

ASKER

Thanks for reply
Actually I want to retrive document content on DocumentOpen Event. On the fly i want to decrypt the content and assign this content to _Document Ptr so that user can access the Decrypted Content.
On DocumentBeforeSave I want to encrypt the content and store into the file.
ASKER CERTIFIED SOLUTION
Avatar of ambience
ambience
Flag of Pakistan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ILGDRM

ASKER

Thanks for Approaches provided by you.
Can you provide sample source code for Suggested approaches.
Alternatively, you can yse VBA. VBA is better suited for Office Automation. In VBA, you can access the document content easily:
        ActiveDocument.Content



ActiveDocument.Content will give a Range object. You can use that object to iterate over images, text, tables etc. but there is no easy way to convert it into a byte array.

Before we embark upon a particular option can you please provide a little insight into your requirements?

What exactly do you want to achieve by enc/dec a word document?
What were your initial thoughts that made you try enc/dec the contents in open/close handlers?
When you enc a document, is it necessary that the resultant be also a word doc?
Avatar of ILGDRM

ASKER

Hi Ambience,

What exactly do you want to achieve by enc/dec a word document?
What were your initial thoughts that made you try enc/dec the contents in open/close handlers?
->
My requirement is I want to protect ( Encrypt ) the output generated by word document (i.e. *.doc).
I want to encrypt  it immedietly after storing document on disk(On DocumentSave event). Same encrypted document I want to decrypt on the fly when user tries to open it(On DocumentBeforeOpen event). I want to encrypt entire document or content irrespective of it's data type. How to achive
Option 2 suggested by you i.e. "Consider encrypting the whole document, rather than contents of the doc. You can encrypt the file after it is written by Word and conversely, decrypt before feeding it to word."

When you enc a document, is it necessary that the resultant be also a word doc?
-> Not Necssary
Well then the simplest that I can recommend is using the EncryptFile API provided by windows.

BOOL bRes = EncryptFile(szPathToWordDoc);

where szPathToWordDoc is the path to the file created by word. Similarly you can use the DecryptFile function to do the reverse (before opening the document).


Foe more info see:
http://msdn.microsoft.com/en-us/library/aa364021(VS.85).aspx
http://msdn.microsoft.com/en-us/library/aa363903(VS.85).aspx


Hope this helps ...
Avatar of ILGDRM

ASKER

Hi ,
Thanks for Reply!!
Suggested solution is possible only if intented document is closed ( in Off Line Mode ).
Actually my requirement is
 I want to retrive document content or whole document on DocumentOpen Event or while opening Encrypted Document. And while saving or on DocumentBeforeSave I want to Encrypt the Document Content or Whole document. While Accessing the File ( Document ) on Hard Disk it should be in encrypted format. When Word application try to access encrypted file content should be in decrypted format on the fly.
Strange, the code snippet that you originally posted

IDispatch* pDispRange = oDocument.GetContent();
Range objRange(pDispRange);
AfxMessageBox(objRange.GetText());

suggested something different. It appeared as though you have access to the file and Automate word to open the file. In that case I thought that you may insert an extra decryption step.

Is it ok for you to insert VB script macros to the document that you are trying to encrypt? Or do you want to use the C++ application to encrypt the doc?
ILGDRM,
As I said earlier, using VBA is my choice for this problem. Are you open to using VBA. If yes, you can get the content of the document easily (see may earlier post) and there are ways how you can encrpyt the data from within. See the below link:
http://www.webace.com.au/~balson/InsaneExcel/Encryption.htm
Avatar of ILGDRM

ASKER

Hi
I want to use the C++ Application to Encrypt / Decrypt the document ( COM Addin ).
If possible please provide the sample source code.
HI

Did you ever get this to work? I am currently trying to achieve the same thing