Fill text fields in OO Writer with C#

Java, C++, C#, Delphi... - Using the UNO bridges
Post Reply
terina
Posts: 2
Joined: Mon Jun 02, 2014 2:52 pm

Fill text fields in OO Writer with C#

Post by terina »

How can I get a c# application to fill text fields in an OO writer document on the base of the field names?

The code looks like this in MS Word

Code: Select all

[...]

foreach (Microsoft.Office.Interop.Word.ContentControl field in doc.ContentControls)
             {
                 switch (field.Title)
                 {
                     case "CustomerName":
                         field.Range.Text = lblCustomerName.Text;
                 }
             }
The code below is adjusted for oo writer documents and works fine, but it will only replace an existing text in a textfield with new information. I would like the application to search for the name of the text field and then fill the field with text, just as in the ms word example above.

Code: Select all

[...]

unoidl.com.sun.star.container.XEnumerationAccess xtextfields =
               ((unoidl.com.sun.star.text.XTextFieldsSupplier)oDoc).getTextFields();
                unoidl.com.sun.star.container.XEnumeration enumaration = xtextfields.createEnumeration();

 while (enumaration.hasMoreElements())
                {
                    uno.Any field = enumaration.nextElement();
                   
                    if (field.Value is unoidl.com.sun.star.text.XTextField)
                    {
                        string name = ((unoidl.com.sun.star.text.XTextField)field.Value).getAnchor().getString();
                        

                        if (name == "CustomerName")
                        {
                            ((unoidl.com.sun.star.text.XTextField)field.Value).getAnchor().setString(lblCustomerName.Text);

                        }

                   }
              }
OpenOffice 4.0.1 on Windows 7
Post Reply