Arnaud
Arnaud

Reputation: 109

Fill a Word document form with OLE in C++ Builder

I want to have my C++ Builder application fill the following Word document form : http://oesv.at/media/media_vereinsservice/media_formulare/Zuschlagsberechnung-Alpin_06-07.doc

I manage to do this with Ole procedures calling OpenOffice Writer but for users of my application who don't have OpenOffice installed but Microsoft Office instead I want to call Ole procedures with Word.

I tried without success to find and see the structure of this document file: like a tree map with : the document object, the Fields or FormFields objects, their properties "Field", which have properties Result, which is a range object having property Text and the document property Selection which has the procedure "TypeText". But no structure view available, so just guessing in the dark how the document file is structured.

Here below two solutions I tried without success:

first: the commented part consist on selecting the text input field and have this current selection in the document call its procedure "TypeText". This throws an error saying the object I try to modify is in a protected area of the document : "EOleException : Cette méthode ou propriété n'est pas disponible car l'objet fait référence à une zone protégée d'un document.".

second: the code below throws an error "EOleException : Le type ne correspond pas" that can be translated "Type mismatch" on the line "Result.OlePropertySet("Text", vInputText);". I have tried to pass a WideString or directly the default string format for my project without success.

        try
            {
            //---ouverture de Word
            vMSWord.OlePropertySet("Visible", false);

            //---ouverture du fichier
            vFileName = Variant(wFile.c_bstr());
            vWDocuments = vMSWord.OlePropertyGet("Documents");
            vWDocument = vWDocuments.OleFunction("Open", vFileName);

            Variant fields, field;
            fields = vWDocument.OlePropertyGet("FormFields");
            for (int i=1; i<=fields.OlePropertyGet("Count"); i++)
                {
                Variant field = fields.OleFunction("Item",(Variant)i);
                //field.OleFunction("Select");
                //Variant selection = vMSWord.OlePropertyGet("Selection");
                //selection.OleProcedure("TypeText",WideString("My input text"));

                Variant Result = field.OlePropertyGet("Result"); // result = objet range
                Variant vInputText = Variant(WideString("My input text").c_bstr());
                Result.OlePropertySet("Text", vInputText);
                }

            //---sauvegarde en fichier texte
            vFileName = Variant(wNewFile.c_bstr());
            ShowMessage("Saveas :");
            vWDocument.OleProcedure("Saveas", vFileName);
            }
        catch(Exception &e)
            {
            ShowMessage(AnsiString(e.ClassName())+ e.Message);
            }

Upvotes: 2

Views: 2207

Answers (1)

x74
x74

Reputation: 1

I do it so:

...
String FieldName = "SomeFormFieldName";  // Try WideString 
String Text = "SomeText"; // Try WideString 

Variant Field = Document.OlePropertyGet("FormFields").OleFunction("Item", (OleVariant)FieldName);
Field.OlePropertyGet("Range").OlePropertySet("Text", Text);
...

But you must be shure that the Document contains fields is exactly of FormFields type not MergeField or others. Also you must set the Bookmark property for Fields inside the template document and use it as FieldName.

Upvotes: 0

Related Questions