Word automation with C++ Builder 5

1.6k Views Asked by At

I'm trying to control Word through a c++ builder 5 application. I would like to open a ".dot" model file created with Word and modify it. In the ".dot" model file there are some fields. For example, Title, LastName, FirstName, Address and so on, and I would like to modify these fields putting text into them and then saving file with a new name, for example "Warning.doc" leaving the ".dot" file unaltered.

I can open the file, count the number of fields it contains, but then when it comes to replacing each field with a string I don't know how to do because I don't have a complete documentation on OleFunction and OlePropertyGet methods. I attach my source code to this message, can anybody help me to solve this problem please?

try
{
       my_word = Variant::CreateObject("word.application");
}
catch (...)
{
       Application->MessageBox("Unable to obtain Word automation object",
                               "Error:",MB_OK | MB_ICONERROR);
}
my_word.OlePropertySet("Visible", (Variant)true); 

void __fastcall TForm1::Button2Click(TObject *Sender)
{
   Variant  this_doc;
   Variant  my_fields;
   Variant  test;
   int k,field_count;
   AnsiString test1;

   AnsiString filename = "d:\\ProgrammaWord\\1-Avviso.dot";

   my_docs = my_word.OlePropertyGet("Documents");

   this_doc = my_docs.OleFunction("Open", filename);

   my_fields = this_doc.OlePropertyGet("Fields");

   field_count = my_fields.OlePropertyGet("Count");

   for(k = 1; k <= field_count; k++)
   {
     test = my_fields.OleFunction("Item",(Variant)k);
     test1 = test.OleFunction("Value");  //This instruction throws an exception
                                         // "Value" is not a recognized parameter 
                                         // in this case
     Memo1->Lines->Add(test1);
   }
 }
1

There are 1 best solutions below

0
AudioBubble On

I never used word Ole but I used it for Outlook and Excel, I can't try it with word since I'm currently on OSX but you should try something similar as what I did.

The generic way of using Ole was to OleGetproperty() while you get the targeted field then OleSetProperty("action", ...).

for example when I wanted to change the color of the text in a particular cell of my excel document I used:

Variant _excel = Variant::CreateObject("Excel.Application");
Variant _workbook = _excel.OlePropertyGet("WorkBooks").OleFunction("Open", filename);
Variant _worksheet = _workbook.OlePropertyGet("WorkSheets", sheet);

_worksheet.OlePropertyGet("Cells", row, col).OlePropertyGet("Font").OlePropertySet("Color", color);

Here I instanciate an excel object, then I load a file into it (_workbook), then I select the _worksheet from the _workbook and I start my business.

Here comes the interesting part:

It concist of getting to a particular cell, getting the font object from it, and then setting the color of this font object.

Disclaimer: This is an example from my sources for excel, it's not directly related to your example, but maybe you can understand the principe with it. I can't try to figure out what you need because I have no windows right now.

Hope this can help you. Finding ressources for OLE can be fierce if you don't have the good patterns to look for.