Borland C ++ MS Word Automation

Introduction

Initially, I wrote a C ++ parser that was used to analyze given MS Word documents and place them in some form of structure that was more useful for data processing. After I wrote the parser, I started working with .NET and C # to restore 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, which is why I finally had time to put together something. I have written this article for the purpose of making it easier for anyone looking for quick answers. I hope people can take advantage of the information provided and help them get started faster.

background

No special background is needed. Just get some hands-on experience with C ++.

Using the code

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

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 the MS Word COM object. This is the object that will be used to access MS Word application features. To see what features are available, you can do within MS Word. See the first article, Automating MS Word using Visual Studio .NET.

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

Note: To understand the code better, ignore everything in the code except the parts 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 the word visible to make the invisible set false

my_word.OlePropertySet (“Visible”, (Variant) true);

// retrieve document object

my_docs = my_word.OlePropertyGet (“documents”);

Variant wordActiveDocument = my_docs.OleFunction (“open”, filename);

.

.

.

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

Next, we define two variant data types called my_word and my_docs. my_word is used to create a word.application object and my_docs will be used to create a document 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 open the given MS Word document.

Note that most of the variables are of the Variant type.

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

Let’s take a look at the following code, it’s about tables within an MS Word document.

.

.

Variant wordTables = wordActiveDocument.OlePropertyGet (“Tables”);

long table_count = wordTables.OlePropertyGet (“count”);

.

.

As I mentioned before, all your data types will 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 returns all table objects that are within our active document object. Since tables are a property of a document object, we need to use OlePropertyGet (“tables”); to get the value.

long table_count = wordTables.OlePropertyGet (“count”);

The line above returns the number of tables in the object out of tables. This is done by calling OlePropertyGet (“count”); to give us the value.

You may be wondering where do I get this information from? The answer to this 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;

sample

{

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 = “READ”;

// ———————————————— ——————-

// Vahe Karamian – 10-10-2003

// This part of the code converts the word document to

// unformatted text, and does extensive cleanup

statusBar-> Panels-> Items[0]-> Text = “Conversion 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”);

// Insert 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 (“Insert Special”,

0, false, 0, false, 2);

// Select the text of the new document again

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 clean up here …

Variant wordSelectionFind =

vk_converted_document_selection.OlePropertyGet (“Find”);

statusBar-> Panels-> Items[0]-> Text = “Find & Replace …”;

vk_timerTimer (Sender);

wordSelectionFind.OleFunction (“Finish”, “^ l”,

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

“”, 2, false, false, false, false);

wordSelectionFind.OleFunction (“Execute”, “^ p”, false,

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 do in the code above, we open 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 will select the content from the existing document and paste it into our new document. This part is performed by Variant vk_this_doc_select = vk_this_doc.OleFunction (“Select”); getting a selected object and Variant vk_this_doc_selection = vk_word_app.OlePropertyGet (“Selection”); to get a reference to the current selection. Then we need to copy the vk_this_doc_selection.OleFunction (“Copy”) checkbox. Then 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 point, we have a selection object for the existing document and the new document. Now we will use both of them to execute our special paste using vk_converted_document_selection.OleFunction (“PasteSpecial”, 0, false, 0, false, 2); Now we have inserted our original content into a special format in the newly created document. We need to make another selected call in the new document before we find our replacement and replace it. 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 execute our findings 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

Structuring a Word document is a challenging task, given that many people have different ways of creating documents. Nevertheless, it would help organizations to begin modeling their documents. This allows them to apply XML forms to their documents and make extracting content from them much easier. This is a challenging task for most companies; usually they lack either the expertise or the resources. And such projects are large in scope due to the fact that they will affect more than one functional business area. But in 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 loss of documents has a lot of added value to your business.