| IntroductionOriginally, I wrote a C++ parser | | | | wordTables = |
| which was used to parse given MS Word | | | | wordActiveDocument.OlePropertyGet( "Tables" |
| documents and put them into some form of a | | | | );long table_count = |
| structure that was more useful for data | | | | wordTables.OlePropertyGet( "count" );.. |
| processing. After I wrote the parser, I | | | | |
| started working with .NET and C# to re-create | | | | As I mentioned before, all your data types |
| the parser. In the process, I also wrote my | | | | are going to be of Variant. So we declare a |
| first article for Code Project, Automating MS | | | | Variant data type called wordTables to |
| Word Using Visual Studio .NET. Several people | | | | represent Tables object in our Document |
| have requested to see the C++ version of the | | | | object.Variant wordTables = |
| application, hence, I finally got some time | | | | wordActiveDocument.OlePropertyGet( "Tables" |
| to put something together. I have written | | | | ); |
| this article with the intention of making it | | | | |
| easier for someone who is looking for quick | | | | The line above will return all Table objects |
| answers. I hope that people can benefit from | | | | that are within our active Document object. |
| the information provided and help them get | | | | Since Tables is a property of a Document |
| started faster.BackgroundNo special | | | | object, 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 which | | | | The line above will return the number of |
| you need to get an instance of MS Word, and | | | | tables in out Tables object. This is done by |
| then give you snapshots of code that perform | | | | calling the OlePropertyGet( "count" ); to |
| specific functions. I believe this way will | | | | return us the value.You might be wondering |
| help you get started faster in developing | | | | where do I get this information from? The |
| your own programs.The following block is the | | | | answer to that question is in the first |
| header portion of the CPP file.Note: The most | | | | article: Automating MS Word Using Visual |
| important include files are and . These are | | | | Studio .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 Project | | | | Tables object.. |
| | | | |
| / | | | | . |
| --------------------------------------------- | | | | |
| ------------------------------ | | | | . |
| | | | |
| #include | | | | int t, r, c;try |
| | | | |
| #pragma hdrstop// We need this for the OLE | | | | {for( t=1; tPanels->Items[0]->Text = "Find & |
| object | | | | Replace..."; |
| | | | |
| #include | | | | vk_timerTimer( Sender |
| | | | );wordSelectionFind.OleFunction( "Execute", |
| #include | | | | "^l",false, false, false, false, false, true, |
| | | | 1, false," ", 2, false, false, false, false |
| #include "Unit1.h" | | | | ); |
| | | | |
| #include | | | | wordSelectionFind.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 Studio | | | | So what we are doing in the code above, we |
| .NET.As before, you can either make a Windows | | | | are opening an existing document with |
| Forms Application or a Command Line | | | | vk_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 Forms | | | | document with Variant vk_converted_document = |
| application, that has a button to start the | | | | vk_word_doc.OleFunction( "Add" );. Then we |
| process. When the user clicks the button, the | | | | want to select the content from the existing |
| Button1Click(TObject *Sender) event will be | | | | document and paste them in our new document. |
| called and the code executed.Note: To better | | | | This portion is done by Variant |
| understand the code, ignore everything in the | | | | vk_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 __fastcall | | | | our special paste using |
| TForm1::Button1Click(TObject *Sender) | | | | vk_converted_document_selection.OleFunction( |
| | | | "PasteSpecial", 0, false, 0, false, 2 );. |
| {...// used for the file nameOleVariant | | | | Now, we have our original content pasted in a |
| fileName;fileName = | | | | special format in the newly created document. |
| openDialog->FileName;Variant my_word;Variant | | | | We 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 put | | | | vk_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 a | | | | t( "Find" ); and finally, we can use our find |
| OleVariant data type called fileName, we | | | | object 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 just | | | | true, 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 Variant | | | | InterestPutting 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 a | | | | have different ways of authoring documents. |
| word.application object and my_docs will be | | | | Nevertheless, it would help for organizations |
| used to create a documents object.Next, we | | | | to start modeling their documents. This will |
| define another Variant data type called | | | | allow them to apply XML schema to their |
| myActiveDocument. Using this referenced | | | | documents and make extracting content from |
| object, we can now do what we want! In this | | | | them much easier. This is a challenging task |
| case, we are going to open the given MS Word | | | | for most companies; usually, either they are |
| document.Notice that most of the variables | | | | lacking the expertise or the resources. And |
| are of type Variant.At this point, we have a | | | | such projects are huge in scale due to the |
| Word document that we can start performing | | | | fact that they will affect more than one |
| functions on. At first, it might take a while | | | | functional business area. But on the long |
| for you to see how it works, but once you get | | | | run, it will be beneficial to the |
| a hang of it, anything in MS Word domain is | | | | organization as a whole. The fact that your |
| possible.Let's take a look at the following | | | | documents are driven by structured data and |
| code, it is going to be dealing with tables | | | | not by formatting and lose documents has a |
| within a MS Word document...Variant | | | | lot of value added to your business. |