Borland C++ MS Word Automation

IntroductionOriginally, I wrote a C++ parser whichwordActiveDocument.OlePropertyGet( "Tables" );long
was used to parse given MS Word documents andtable_count = wordTables.OlePropertyGet( "count" );..
put them into some form of a structure that wasAs I mentioned before, all your data types are going
more useful for data processing. After I wrote theto be of Variant. So we declare a Variant data type
parser, I started working with .NET and C# tocalled wordTables to represent Tables object in our
re-create the parser. In the process, I also wrote myDocument object.Variant wordTables =
first article for Code Project, Automating MS WordwordActiveDocument.OlePropertyGet( "Tables" );
Using Visual Studio .NET. Several people haveThe line above will return all Table objects that are
requested to see the C++ version of the application,within our active Document object. Since Tables is a
hence, I finally got some time to put somethingproperty of a Document object, we have to use the
together. I have written this article with the intentionOlePropertyGet( "Tables" ); to get the value.long
of making it easier for someone who is looking fortable_count = wordTables.OlePropertyGet( "count" );
quick answers. I hope that people can benefit fromThe line above will return the number of tables in
the information provided and help them get startedout Tables object. This is done by calling the
faster.BackgroundNo special background is necessary.OlePropertyGet( "count" ); to return us the value.You
Just have some hands on experience with C++.Usingmight be wondering where do I get this information
the codeI think the best way to present the codefrom? The answer to that question is in the first
would be to first give you the critical sections whicharticle: Automating MS Word Using Visual Studio
you need to get an instance of MS Word, and then.NET.The next block of code will demonstrate how to
give you snapshots of code that perform specificextract content from the Tables object..
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: Theint t, r, c;try
most important include files are and . These are used{for( t=1; tPanels->Items[0]->Text = "Find &
for COM and OLE.// Vahe Karamian - 04-20-2004 -Replace...";
For Code Projectvk_timerTimer( Sender
/);wordSelectionFind.OleFunction( "Execute", "^l",false,
false, false, false, false, true, 1, false," ", 2, false,
#includefalse, false, false );
#pragma hdrstop// We need this for the OLEwordSelectionFind.OleFunction( "Execute", "^p",
objectfalse,false, false, false, false, true, 1, false," ", 2, false,
#includefalse, false, false );// Save the new document
#includevk_converted_document.OleFunction( "SaveAs",
#include "Unit1.h"vk_converted_filename );// Close the new document
#includevk_converted_document.OleProcedure( "Close" );
/// -------------------------------------------------------------------
#pragma package(smart_init)So what we are doing in the code above, we are
#pragma resource "*.dfm"opening an existing document with vk_this_doc =
TForm1 *Form1;vk_word_doc.OleFunction( "Open", vk_filename );.
The following block creates MS Word COM Object.Next we add a new document with Variant
This is the object which will be used to access MSvk_converted_document =
Word application functions. To see what functionsvk_word_doc.OleFunction( "Add" );. Then we want
are available, you can do within MS Word. Refer toto select the content from the existing document
the first article, Automating MS Word Using Visualand paste them in our new document. This portion is
Studio .NET.As before, you can either make adone by Variant vk_this_doc_select =
Windows Forms Application or a Command Linevk_this_doc.OleFunction( "Select" ); to get a select
application, the process is the same. The code belowobject and Variant vk_this_doc_selection =
is based on a Windows Forms application, that has avk_word_app.OlePropertyGet( "Selection" ); to get a
button to start the process. When the user clicks thereference to the actual selection. Then we have to
button, the Button1Click(TObject *Sender) event willcopy the selection using
be called and the code executed.Note: To bettervk_this_doc_selection.OleFunction( "Copy" );. Next,
understand the code, ignore everything in the codewe perform the same task for the new document
except the portions that are in bold.TForm1 *Form1;with Variant vk_converted_document_select =
/vk_converted_document.OleFunction( "Select" ); and
Variant vk_converted_document_selection =
__fastcall TForm1::TForm1(TComponent* Owner):vk_word_app.OlePropertyGet( "Selection" );. At this
TForm(Owner)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(
void __fastcall TForm1::Button1Click(TObject"PasteSpecial", 0, false, 0, false, 2 );. Now, we have
*Sender)our original content pasted in a special format in the
{...// used for the file nameOleVariantnewly created document. We have to do a new
fileName;fileName = openDialog->FileName;Variantselect call in the new document before we do our
my_word;Variant my_docs;// create wordfind and replace. To do so, we simply use the same
objectmy_word = Variant::CreateObject(calls vk_converted_document_select =
"word.application" );// make word visible, to makevk_converted_document.OleFunction( "Select" ); and
invisible put falsemy_word.OlePropertySet( "Visible",vk_converted_document_selection =
(Variant) true );// get document objectmy_docs =vk_word_app.OlePropertyGet( "Selection" );. Next,
my_word.OlePropertyGet( "documents" );Variantwe create a Find object with Variant
wordActiveDocument = my_docs.OleFunction(wordSelectionFind =
"open", fileName );...vk_converted_document_selection.OlePropertyGet(
So a brief explanation, we define a OleVariant data"Find" ); and finally, we can use our find object to
type called fileName, we assign a file path to ourperform our find and replace with
fileName variable. In the code above, this is donewordSelectionFind.OleFunction( "Execute", "^l", false,
using a OpenDialog object. Of course, you can justfalse, false, false, false, true, 1, false, " ", 2, false,
assign a whole path for testing if you like, i.e.,false, false, false );.That's all there is to it!Points of
c:\test.doc.Next, we define two Variant data typesInterestPutting structure to a Word document is a
called my_word, and my_docs. my_word will bechallenging task, given that many people have
used to create a word.application object anddifferent ways of authoring documents. Nevertheless,
my_docs will be used to create a documentsit would help for organizations to start modeling their
object.Next, we define another Variant data typedocuments. This will allow them to apply XML schema
called myActiveDocument. Using this referencedto their documents and make extracting content
object, we can now do what we want! In this case,from them much easier. This is a challenging task for
we are going to open the given MS Wordmost companies; usually, either they are lacking the
document.Notice that most of the variables are ofexpertise or the resources. And such projects are
type Variant.At this point, we have a Wordhuge in scale due to the fact that they will affect
document that we can start performing functions on.more than one functional business area. But on the
At first, it might take a while for you to see how itlong run, it will be beneficial to the organization as a
works, but once you get a hang of it, anything in MSwhole. The fact that your documents are driven by
Word domain is possible.Let's take a look at thestructured data and not by formatting and lose
following code, it is going to be dealing with tablesdocuments has a lot of value added to your business.
within a MS Word document...Variant wordTables =