[Solved] Selecting and Updating a Text Field

Creating a macro - Writing a Script - Using the API

[Solved] Selecting and Updating a Text Field

Postby macroman25 » Wed Jan 13, 2016 3:38 am

I have a field:
Type: Variable
Name: ClientName
Format: Text

The field is already within the .odt document, with the default value set to "Name"
I would like to update this value to a given client name (string value) "John Smith"

I have already checked out a lot of documentation and having no luck...
I imagined it would be something along these lines:
Code: Select all   Expand viewCollapse view
   NameField = Doc.getTextFieldMasters().getByName("ClientName")
   NameField.Content = "JohnSmith"
Last edited by macroman25 on Wed Jan 13, 2016 6:11 am, edited 1 time in total.
LibreOffice 5.0.3.2 on Windows 10
macroman25
 
Posts: 33
Joined: Fri Dec 04, 2015 12:32 am

Re: Selecting and Updating a Text Field

Postby FJCC » Wed Jan 13, 2016 3:54 am

There may be a better way but this seems to do the job.
Code: Select all   Expand viewCollapse view
oTextFields = ThisComponent.getTextFields()
Enum = oTextFields.createEnumeration()
While Enum.hasMoreElements()
   oField = Enum.nextElement()
   If oField.TextFieldMaster.Name = "ClientName" Then
      oField.TextFieldMaster.Content = "John Smith"
   End If
Wend
Windows 10 and Linux Mint, since 2017
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
FJCC
Moderator
 
Posts: 7302
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Selecting and Updating a Text Field

Postby macroman25 » Wed Jan 13, 2016 4:10 am

Thanks FJCC
For some reason I get "Property or method not found: Content."
any ideas?
LibreOffice 5.0.3.2 on Windows 10
macroman25
 
Posts: 33
Joined: Fri Dec 04, 2015 12:32 am

Re: Selecting and Updating a Text Field

Postby FJCC » Wed Jan 13, 2016 4:13 am

We may be using different field types. Can you upload a document with an example of your field? Just a little dummy text and the field will be enough.
Windows 10 and Linux Mint, since 2017
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
FJCC
Moderator
 
Posts: 7302
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Selecting and Updating a Text Field

Postby macroman25 » Wed Jan 13, 2016 4:17 am

Attached.
Attachments
textfield.odt
(8.65 KiB) Downloaded 65 times
LibreOffice 5.0.3.2 on Windows 10
macroman25
 
Posts: 33
Joined: Fri Dec 04, 2015 12:32 am

Re: Selecting and Updating a Text Field

Postby macroman25 » Wed Jan 13, 2016 4:19 am

I can probably use a different field type if that's easier...
LibreOffice 5.0.3.2 on Windows 10
macroman25
 
Posts: 33
Joined: Fri Dec 04, 2015 12:32 am

Re: Selecting and Updating a Text Field

Postby FJCC » Wed Jan 13, 2016 5:49 am

The code I originally posted worked for a field inserted with Insert -> Fields -> Other -> Variables Tab -> Type=User field. Try using that. I can't find a way to change the value of the field type you used.
Windows 10 and Linux Mint, since 2017
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
FJCC
Moderator
 
Posts: 7302
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Selecting and Updating a Text Field

Postby macroman25 » Wed Jan 13, 2016 5:58 am

OK Thanks, will give that a try... annoying that they can be so different...
LibreOffice 5.0.3.2 on Windows 10
macroman25
 
Posts: 33
Joined: Fri Dec 04, 2015 12:32 am

Re: Selecting and Updating a Text Field

Postby macroman25 » Wed Jan 13, 2016 6:10 am

Thanks FJCC - works a treat now!
LibreOffice 5.0.3.2 on Windows 10
macroman25
 
Posts: 33
Joined: Fri Dec 04, 2015 12:32 am

Re: Selecting and Updating a Text Field

Postby bkr » Fri Aug 02, 2019 1:01 am

FJCC wrote:There may be a better way but this seems to do the job.
Code: Select all   Expand viewCollapse view
oTextFields = ThisComponent.getTextFields()
Enum = oTextFields.createEnumeration()
While Enum.hasMoreElements()
   oField = Enum.nextElement()
   If oField.TextFieldMaster.Name = "ClientName" Then
      oField.TextFieldMaster.Content = "John Smith"
   End If
Wend


There is a better way - you can call each field by name using the TextFieldMasters property of the document as described here: https://www.openoffice.org/api/docs/common/ref/com/sun/star/text/TextFieldMasters.html

the TextFieldMasters property is a collection that implements the XNameAccess interface (which means you can use the
Code: Select all   Expand viewCollapse view
getByName()
method to retrieve the instance then set the value with a method like the following:

Code: Select all   Expand viewCollapse view
    def set_field(self, document, name, value):
        field = document.TextFieldMasters.getByName('com.sun.star.text.fieldmaster.User.{0}'.format(name))
        field.Content = value


note that the name of the text field needs to include the namespace (hence the string formatting). If you keep getting
Code: Select all   Expand viewCollapse view
NoSuchElementException
s, you can use the following code to print out the contents of the collection:

Code: Select all   Expand viewCollapse view
for field_name in document.TextFieldMasters.getElementNames():
    print(field_name)
OpenOffice 3.4.0 on Ubuntu and Mac OSX
bkr
 
Posts: 1
Joined: Thu Aug 01, 2019 2:04 pm


Return to Macros and UNO API

Who is online

Users browsing this forum: No registered users and 2 guests