Borland C++ MS Phrase Automation


Initially, I wrote a C++ parser which used to be used to parse given MS Phrase paperwork and put them into some type of a construction that used to be extra helpful for knowledge processing. After I wrote the parser, I began working with .NET and C# to re-create the parser. Within the course of, I additionally wrote my first article for Code Venture, Automating MS Phrase The usage of Visible Studio .NET. A few individuals have requested to peer the C++ model of the appliance, therefore, I at last obtained a while to place one thing collectively. I’ve written this text with the intention of constructing it more straightforward for anyone who’s in search of fast solutions. I’m hoping that individuals can profit from the ideas equipped and lend a hand them get began quicker.

Historical past

No different history is vital. Simply have some palms on expertise with C++.

The use of the code

I believe one of the best ways to current the code can be to first provide the important sections which you wish to get an occasion of MS Phrase, after which provide you with snapshots of code that function particular capabilities. I consider this manner will will let you get began sooner in creating your personal packages.

The next block is the header part of the CPP file.

Be aware: An important embody recordsdata are and . These are used for COM and OLE.

// Vahe Karamian – 04-20-2004 – For Code Undertaking



#pragma hdrstop

// We’d like this for the OLE object



#embrace “Unit1.h”



#pragma package deal(smart_init)

#pragma useful resource “*.dfm”

TForm1 *Form1;

The next block creates MS Phrase COM Object. That is the thing that allows you to be used to get admission to MS Phrase software capabilities. To peer what capabilities are on hand, you are able to do inside MS Phrase. Consult with the primary article, Automating MS Phrase The use of Visible Studio .NET.

As earlier than, you could both make a Home windows Types Utility or a Command Line utility, the method is similar. The code under is in response to a Home windows Varieties software, that has a button to start out the method. When the person clicks the button, the Button1Click(TObject *Sender) adventure will probably be referred to as and the code performed.

Be aware: To raised be mindful the code, ignore the whole thing within the code excluding the parts which can be in daring.

TForm1 *Form1;


__fastcall TForm1::TForm1(TComponent* Proprietor)

: TForm(Proprietor)


void __fastcall TForm1::Button1Click(TObject *Sender)




// used for the file identify

OleVariant fileName;

fileName = openDialog->FileName;

Variant my_word;

Variant my_docs;

// create phrase object

my_word = Variant::CreateObject( “phrase.utility” );

// make phrase seen, to make invisible put false

my_word.OlePropertySet( “Seen”, (Variant) actual );

// get record object

my_docs = my_word.OlePropertyGet( “paperwork” );

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




So a short clarification, we outline a OleVariant knowledge kind known as fileName, we assign a file route to our fileName variable. Within the code above, that is accomplished the use of a OpenDialog object. In fact, which you could simply assign a complete course for trying out in case you like, i.e., c:check.doc.

Subsequent, we outline two Variant knowledge varieties referred to as my_word, and my_docs. my_word will likely be used to create a object and my_docs will probably be used to create a paperwork object.

Subsequent, we outline every other Variant knowledge sort referred to as myActiveDocument. The use of this referenced object, we will now do what we would like! On this case, we’re going to open the given MS Phrase report.

Discover that lots of the variables are of sort Variant.

At this level, we have now a Phrase file that we will begin performing features on. To start with, it would take some time so that you can see the way it works, however while you get a grasp of it, the rest in MS Phrase area is that you can imagine.

Let’s check out the next code, it will be coping with tables inside a MS Phrase report.



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

lengthy table_count = wordTables.OlePropertyGet( “rely” );



As I discussed earlier than, your entire information varieties are going to be of Variant. So we declare a Variant information sort referred to as wordTables to characterize Tables object in our Report object.

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

The road above will return all Desk objects which might be inside our lively Record object. Due to the fact that Tables is a property of a File object, we’ve to make use of the OlePropertyGet( “Tables” ); to get the worth.

lengthy table_count = wordTables.OlePropertyGet( “depend” );

The road above will return the choice of tables in out Tables object. That is completed by way of calling the OlePropertyGet( “depend” ); to come back us the worth.

You could be questioning the place do I get this knowledge from? The reply to that question is within the first article: Automating MS Phrase The use of Visible Studio .NET.

The subsequent block of code will exhibit how you can extract content material from the Tables object.




int t, r, c;

are trying

for( t=1; tFiles->Strings[i];

vk_converted_filename = openDialog->Information->Strings[i] + “_c.doc”;

// Open the given Phrase file

vk_this_doc = vk_word_doc.OleFunction( “Open”, vk_filename );

statusBar->Panels->Objects[2]->Textual content = “READING”;

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

// Vahe Karamian – 10-10-2003

// This part of the code will convert the phrase record into

// unformatted textual content, and do in depth smooth up

statusBar->Panels->Gadgets[0]->Textual content = “Changing to textual content…”;

vk_timerTimer( Sender );

// Create a brand new report

Variant vk_converted_document = vk_word_doc.OleFunction( “Add” );

// Make a selection textual content from the unique report

Variant vk_this_doc_select = vk_this_doc.OleFunction( “Make a selection” );

Variant vk_this_doc_selection = vk_word_app.OlePropertyGet( “Choice” );

// Replica the chosen textual content

vk_this_doc_selection.OleFunction( “Reproduction” );

// Paste chosen textual content into the brand new record

Variant vk_converted_document_select =

vk_converted_document.OleFunction( “Make a selection” );

Variant vk_converted_document_selection =

vk_word_app.OlePropertyGet( “Choice” );

vk_converted_document_selection.OleFunction( “PasteSpecial”,

zero, false, zero, false, 2 );

// Re-Choose the textual content within the new report

vk_converted_document_select =

vk_converted_document.OleFunction( “Make a selection” );

vk_converted_document_selection =

vk_word_app.OlePropertyGet( “Choice” );

// Shut the unique report

vk_this_doc.OleProcedure( “Shut” );

// Let’s do out smooth-up right here …

Variant wordSelectionFind =

vk_converted_document_selection.OlePropertyGet( “To find” );

statusBar->Panels->Gadgets[0]->Textual content = “In finding & Substitute…”;

vk_timerTimer( Sender );

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

false, false, false, false, false, genuine, 1, false,

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

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

false, false, false, false, actual, 1, false,

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

// Keep the brand new report

vk_converted_document.OleFunction( “SaveAs”, vk_converted_filename );

// Shut the brand new record

vk_converted_document.OleProcedure( “Shut” );

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

So what we’re doing within the code above, we’re opening an current record with vk_this_doc = vk_word_doc.OleFunction( “Open”, vk_filename );. Subsequent we add a brand new report with Variant vk_converted_document = vk_word_doc.OleFunction( “Add” );. Then we wish to choose the content material from the prevailing report and paste them in our new record. This component is finished with the aid of Variant vk_this_doc_select = vk_this_doc.OleFunction( “Choose” ); to get a make a selection object and Variant vk_this_doc_selection = vk_word_app.OlePropertyGet( “Choice” ); to get a reference to the real choice. Then we’ve got to repeat the choice the usage of vk_this_doc_selection.OleFunction( “Replica” );. Subsequent, we function the identical process for the brand new record with Variant vk_converted_document_select = vk_converted_document.OleFunction( “Make a selection” ); and Variant vk_converted_document_selection = vk_word_app.OlePropertyGet( “Choice” );. Right now, we now have a ramification object for the present report and the brand new record. Now, we’re going to be the usage of them each to do our different paste the usage of vk_converted_document_selection.OleFunction( “PasteSpecial”, zero, false, zero, false, 2 );. Now, we now have our authentic content material pasted in a unique structure within the newly created report. We have now to do a brand new make a choice name within the new report ahead of we do our in finding and change. To take action, we merely use the identical calls vk_converted_document_select = vk_converted_document.OleFunction( “Make a selection” ); and vk_converted_document_selection = vk_word_app.OlePropertyGet( “Choice” );. Subsequent, we create a To find object with Variant wordSelectionFind = vk_converted_document_selection.OlePropertyGet( “In finding” ); and eventually, we will use our in finding object to operate our in finding and change with wordSelectionFind.OleFunction( “Execute”, “^l”, false, false, false, false, false, authentic, 1, false, ” “, 2, false, false, false, false );.

Which is all there’s to it!

Factors of Pastime

Hanging construction to a Phrase record is a difficult process, provided that many individuals have alternative ways of authoring paperwork. Nonetheless, it could assist for firms to start out modeling their paperwork. This may enable them to use XML schema to their paperwork and make extracting content material from them so much more uncomplicated. It is a difficult job for many corporations; frequently, both they’re missing the experience or the instruments. And such initiatives are big in scale because of the truth that they are going to impact multiple useful trade house. However on the long term, it is going to be a good option to the group as an entire. The truth that your paperwork are pushed by means of structured information and now not via formatting and lose paperwork has quite a lot of worth brought to your small business.

with the aid of-ascitconsultancy services and products

Posted in techinnovation and tagged , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , .