SHOPPING CART
Borland C++ MS Word Automation

Borland C++ MS Word Automation

Introduction

Originally, I wrote a C++ parser which was used to parse given MS Word documents and put them into some form of a structure that was more useful for data processing. After I wrote the parser, I started working with .NET and C# to re-create the parser. In the process, I also wrote my first article for Code Project, Automating MS Word Using Visual Studio .NET. Several people have requested to see the C++ version of the application, hence, I finally got some time to put something together. I have written this article with the intention of making it easier for someone who is looking for quick answers. I hope that people can benefit from the information provided and help them get started faster.

Background

No special background is necessary. Just have some hands on experience with C++.

Using the code

I think the best way to present the code would be to first give you the critical sections which you need to get an instance of MS Word, and then give you snapshots of code that perform specific functions. I believe this way will help you get started faster in developing your own programs.

The following block is the header portion of the CPP file.

Note: The most important include files are and . These are used for COM and OLE.

// Vahe Karamian - 04-20-2004 - For Code Project
//---------------------------------------------------------------------------
#include
#pragma hdrstop

// We need this for the OLE object
#include
#include
#include "Unit1.h"
#include
//---------------------------------------------------------------------------

#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
The following block creates MS Word COM Object. This is the object which will be used to access MS Word application functions. To see what functions are available, you can do within MS Word. Refer to the first article, Automating MS Word Using Visual Studio .NET.

As before, you can either make a Windows Forms Application or a Command Line application, the process is the same. The code below is based on a Windows Forms application, that has a button to start the process. When the user clicks the button, the Button1Click(TObject *Sender) event will be called and the code executed.

Note: To better understand the code, ignore everything in the code except the portions that are in bold.

TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)

: TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{

.

.

.

// used for the file name

OleVariant fileName;

fileName = openDialog->FileName;

Variant my_word;

Variant my_docs;

// create word object

my_word = Variant::CreateObject( "word.application" );

// make word visible, to make invisible put false

my_word.OlePropertySet( "Visible", (Variant) true );

// get document object

my_docs = my_word.OlePropertyGet( "documents" );

Variant wordActiveDocument = my_docs.OleFunction( "open", fileName );

.

.

.
So a brief explanation, we define a OleVariant data type called fileName, we assign a file path to our fileName variable. In the code above, this is done using a OpenDialog object. Of course, you can just assign a whole path for testing if you like, i.e., c:\test.doc.

Next, we define two Variant data types called my_word, and my_docs. my_word will be used to create a word.application object and my_docs will be used to create a documents object.

Next, we define another Variant data type called myActiveDocument. Using this referenced object, we can now do what we want! In this case, we are going to open the given MS Word document.

Notice that most of the variables are of type Variant.

At this point, we have a Word document that we can start performing functions on. At first, it might take a while for you to see how it works, but once you get a hang of it, anything in MS Word domain is possible.

Let's take a look at the following code, it is going to be dealing with tables within a MS Word document.

.

.

Variant wordTables = wordActiveDocument.OlePropertyGet( "Tables" );

long table_count = wordTables.OlePropertyGet( "count" );

.

.
As I mentioned before, all your data types are going to be of Variant. So we declare a Variant data type called wordTables to represent Tables object in our Document object.

Variant wordTables = wordActiveDocument.OlePropertyGet( "Tables" );
The line above will return all Table objects that are within our active Document object. Since Tables is a property of a Document object, we have to use the OlePropertyGet( "Tables" ); to get the value.

long table_count = wordTables.OlePropertyGet( "count" );
The line above will return the number of tables in out Tables object. This is done by calling the OlePropertyGet( "count" ); to return us the value.

You might be wondering where do I get this information from? The answer to that question is in the first article: Automating MS Word Using Visual Studio .NET.

The next block of code will demonstrate how to extract content from the Tables object.

.
.
.
int t, r, c;

try
{

for( t=1; tFiles->Strings[i];
vk_converted_filename = openDialog->Files->Strings[i] + "_c.doc";

// Open the given Word file
vk_this_doc = vk_word_doc.OleFunction( "Open", vk_filename );

statusBar->Panels->Items[2]->Text = "READING";

// -------------------------------------------------------------------
// Vahe Karamian - 10-10-2003
// This portion of the code will convert the word document into
// unformatted text, and do extensive clean up
statusBar->Panels->Items[0]->Text = "Converting to text...";
vk_timerTimer( Sender );

// Create a new document
Variant vk_converted_document = vk_word_doc.OleFunction( "Add" );

// Select text from the original document
Variant vk_this_doc_select = vk_this_doc.OleFunction( "Select" );
Variant vk_this_doc_selection = vk_word_app.OlePropertyGet( "Selection" );

// Copy the selected text
vk_this_doc_selection.OleFunction( "Copy" );

// Paste selected text into the new document
Variant vk_converted_document_select =

vk_converted_document.OleFunction( "Select" );
Variant vk_converted_document_selection =

vk_word_app.OlePropertyGet( "Selection" );
vk_converted_document_selection.OleFunction( "PasteSpecial",

0, false, 0, false, 2 );

// Re-Select the text in the new document
vk_converted_document_select =

vk_converted_document.OleFunction( "Select" );
vk_converted_document_selection =

vk_word_app.OlePropertyGet( "Selection" );

// Close the original document
vk_this_doc.OleProcedure( "Close" );

// Let's do out clean-up here ...
Variant wordSelectionFind =

vk_converted_document_selection.OlePropertyGet( "Find" );

statusBar->Panels->Items[0]->Text = "Find & Replace...";
vk_timerTimer( Sender );

wordSelectionFind.OleFunction( "Execute", "^l",

false, false, false, false, false, true, 1, false,

" ", 2, false, false, false, false );
wordSelectionFind.OleFunction( "Execute", "^p", false,

false, false, false, false, true, 1, false,

" ", 2, false, false, false, false );

// Save the new document
vk_converted_document.OleFunction( "SaveAs", vk_converted_filename );

// Close the new document
vk_converted_document.OleProcedure( "Close" );
// -------------------------------------------------------------------
So what we are doing in the code above, we are opening an existing document with vk_this_doc = vk_word_doc.OleFunction( "Open", vk_filename );. Next we add a new document with Variant vk_converted_document = vk_word_doc.OleFunction( "Add" );. Then we want to select the content from the existing document and paste them in our new document. This portion is done by Variant vk_this_doc_select = vk_this_doc.OleFunction( "Select" ); to get a select object and Variant vk_this_doc_selection = vk_word_app.OlePropertyGet( "Selection" ); to get a reference to the actual selection. Then we have to copy the selection using vk_this_doc_selection.OleFunction( "Copy" );. Next, we perform the same task for the new document with Variant vk_converted_document_select = vk_converted_document.OleFunction( "Select" ); and Variant vk_converted_document_selection = vk_word_app.OlePropertyGet( "Selection" );. At this time, we have a selection object for the existing document and the new document. Now, we are going to be using them both to do our special paste using vk_converted_document_selection.OleFunction( "PasteSpecial", 0, false, 0, false, 2 );. Now, we have our original content pasted in a special format in the newly created document. We have to do a new select call in the new document before we do our find and replace. To do so, we simply use the same calls vk_converted_document_select = vk_converted_document.OleFunction( "Select" ); and vk_converted_document_selection = vk_word_app.OlePropertyGet( "Selection" );. Next, we create a Find object with Variant wordSelectionFind = vk_converted_document_selection.OlePropertyGet( "Find" ); and finally, we can use our find object to perform our find and replace with wordSelectionFind.OleFunction( "Execute", "^l", false, false, false, false, false, true, 1, false, " ", 2, false, false, false, false );.

That's all there is to it!

Points of Interest

Putting structure to a Word document is a challenging task, given that many people have different ways of authoring documents. Nevertheless, it would help for organizations to start modeling their documents. This will allow them to apply XML schema to their documents and make extracting content from them much easier. This is a challenging task for most companies; usually, either they are lacking the expertise or the resources. And such projects are huge in scale due to the fact that they will affect more than one functional business area. But on the long run, it will be beneficial to the organization as a whole. The fact that your documents are driven by structured data and not by formatting and lose documents has a lot of value added to your business.

Borland C++ MS Word Automation

Borland C++ MS Word Automation

Leave a Reply