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