How robots work


Borland C++ MS Word Automation

IntroductionOriginally, I wrote a C++ parserwordTables =
which was used to parse given MS WordwordActiveDocument.OlePropertyGet( "Tables"
documents and put them into some form of a);long table_count =
structure that was more useful for datawordTables.OlePropertyGet(  "count"  );..
processing. After I wrote the parser, I
started working with .NET and C# to re-createAs I mentioned before, all your data types
the parser. In the process, I also wrote myare going to be of Variant. So we declare a
first article for Code Project, Automating MSVariant data type called wordTables to
Word Using Visual Studio .NET. Several peoplerepresent Tables object in our Document
have requested to see the C++ version of theobject.Variant wordTables =
application, hence, I finally got some timewordActiveDocument.OlePropertyGet( "Tables"
to put something together. I have written);
this article with the intention of making it
easier for someone who is looking for quickThe line above will return all Table objects
answers. I hope that people can benefit fromthat are within our active Document object.
the information provided and help them getSince Tables is a property of a Document
started faster.BackgroundNo specialobject, we have to use the OlePropertyGet(
background is necessary. Just have some hands"Tables" ); to get the value.long table_count
on experience with C++.Using the codeI think=  wordTables.OlePropertyGet(  "count"  );
the best way to present the code would be to
first give you the critical sections whichThe line above will return the number of
you need to get an instance of MS Word, andtables in out Tables object. This is done by
then give you snapshots of code that performcalling the OlePropertyGet( "count" ); to
specific functions. I believe this way willreturn us the value.You might be wondering
help you get started faster in developingwhere do I get this information from? The
your own programs.The following block is theanswer to that question is in the first
header portion of the CPP file.Note: The mostarticle: Automating MS Word Using Visual
important include files are and . These areStudio .NET.The next block of code will
used for COM and OLE.// Vahe Karamian -demonstrate how to extract content from the
04-20-2004  -  For  Code  ProjectTables  object..
/.
---------------------------------------------
------------------------------.
#includeint  t,  r,  c;try
#pragma hdrstop// We need this for the OLE{for( t=1; tPanels->Items[0]->Text = "Find &
objectReplace...";
#includevk_timerTimer( Sender
);wordSelectionFind.OleFunction( "Execute",
#include"^l",false, false, false, false, false, true,
1, false," ", 2, false, false, false, false
#include  "Unit1.h");
#includewordSelectionFind.OleFunction( "Execute",
"^p", false,false, false, false, false, true,
/1, false," ", 2, false, false, false, false
---------------------------------------------);//  Save  the  new  document
------------------------------#pragma
package(smart_init)vk_converted_document.OleFunction( "SaveAs",
vk_converted_filename );// Close the new
#pragma  resource  "*.dfm"document
TForm1  *Form1;vk_converted_document.OleProcedure( "Close"
);
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 StudioSo what we are doing in the code above, we
.NET.As before, you can either make a Windowsare opening an existing document with
Forms Application or a Command Linevk_this_doc = vk_word_doc.OleFunction(
application, the process is the same. The"Open", vk_filename );. Next we add a new
code below is based on a Windows Formsdocument with Variant vk_converted_document =
application, that has a button to start thevk_word_doc.OleFunction( "Add" );. Then we
process. When the user clicks the button, thewant to select the content from the existing
Button1Click(TObject *Sender) event will bedocument and paste them in our new document.
called and the code executed.Note: To betterThis portion is done by Variant
understand the code, ignore everything in thevk_this_doc_select = vk_this_doc.OleFunction(
code except the portions that are in"Select" ); to get a select object and
bold.TForm1  *Form1;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
__fastcall TForm1::TForm1(TComponent*document with Variant
Owner):  TForm(Owner)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
------------------------------void __fastcallour special paste using
TForm1::Button1Click(TObject  *Sender)vk_converted_document_selection.OleFunction(
"PasteSpecial", 0, false, 0, false, 2 );.
{...// used for the file nameOleVariantNow, we have our original content pasted in a
fileName;fileName =special format in the newly created document.
openDialog->FileName;Variant my_word;VariantWe have to do a new select call in the new
my_docs;// create word objectmy_word =document before we do our find and replace.
Variant::CreateObject( "word.application" );/To do so, we simply use the same calls
make word visible, to make invisible putvk_converted_document_select =
falsemy_word.OlePropertySet( "Visible",vk_converted_document.OleFunction( "Select"
(Variant) true );// get document); and vk_converted_document_selection =
objectmy_docs = my_word.OlePropertyGet(vk_word_app.OlePropertyGet( "Selection" );.
"documents" );Variant wordActiveDocument =Next, we create a Find object with Variant
my_docs.OleFunction(  "open", fileName );...wordSelectionFind =
vk_converted_document_selection.OlePropertyGe
So a brief explanation, we define at( "Find" ); and finally, we can use our find
OleVariant data type called fileName, weobject to perform our find and replace with
assign a file path to our fileName variable.wordSelectionFind.OleFunction( "Execute",
In the code above, this is done using a"^l", false, false, false, false, false,
OpenDialog object. Of course, you can justtrue, 1, false, " ", 2, false, false, false,
assign a whole path for testing if you like,false );.That's all there is to it!Points of
i.e., c:\test.doc.Next, we define two VariantInterestPutting structure to a Word document
data types called my_word, and my_docs.is a challenging task, given that many people
my_word will be used to create ahave different ways of authoring documents.
word.application object and my_docs will beNevertheless, it would help for organizations
used to create a documents object.Next, weto start modeling their documents. This will
define another Variant data type calledallow them to apply XML schema to their
myActiveDocument. Using this referenceddocuments and make extracting content from
object, we can now do what we want! In thisthem much easier. This is a challenging task
case, we are going to open the given MS Wordfor most companies; usually, either they are
document.Notice that most of the variableslacking the expertise or the resources. And
are of type Variant.At this point, we have asuch projects are huge in scale due to the
Word document that we can start performingfact that they will affect more than one
functions on. At first, it might take a whilefunctional business area. But on the long
for you to see how it works, but once you getrun, it will be beneficial to the
a hang of it, anything in MS Word domain isorganization as a whole. The fact that your
possible.Let's take a look at the followingdocuments are driven by structured data and
code, it is going to be dealing with tablesnot by formatting and lose documents has a
within a MS Word document...Variantlot of value added to your business.



1 A B C D 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105