[Solved] Selecting and Updating a Text Field

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
macroman25
Posts: 33
Joined: Fri Dec 04, 2015 12:32 am

[Solved] Selecting and Updating a Text Field

Post by macroman25 »

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

	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
FJCC
Moderator
Posts: 9277
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Selecting and Updating a Text Field

Post by FJCC »

There may be a better way but this seems to do the job.

Code: Select all

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
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
macroman25
Posts: 33
Joined: Fri Dec 04, 2015 12:32 am

Re: Selecting and Updating a Text Field

Post by macroman25 »

Thanks FJCC
For some reason I get "Property or method not found: Content."
any ideas?
LibreOffice 5.0.3.2 on Windows 10
FJCC
Moderator
Posts: 9277
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Selecting and Updating a Text Field

Post by FJCC »

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.
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
macroman25
Posts: 33
Joined: Fri Dec 04, 2015 12:32 am

Re: Selecting and Updating a Text Field

Post by macroman25 »

Attached.
Attachments
textfield.odt
(8.65 KiB) Downloaded 255 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

Post by macroman25 »

I can probably use a different field type if that's easier...
LibreOffice 5.0.3.2 on Windows 10
FJCC
Moderator
Posts: 9277
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Selecting and Updating a Text Field

Post by FJCC »

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.
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
macroman25
Posts: 33
Joined: Fri Dec 04, 2015 12:32 am

Re: Selecting and Updating a Text Field

Post by macroman25 »

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

Post by macroman25 »

Thanks FJCC - works a treat now!
LibreOffice 5.0.3.2 on Windows 10
bkr
Posts: 1
Joined: Thu Aug 01, 2019 2:04 pm

Re: Selecting and Updating a Text Field

Post by bkr »

FJCC wrote:There may be a better way but this seems to do the job.

Code: Select all

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/com ... sters.html

the TextFieldMasters property is a collection that implements the XNameAccess interface (which means you can use the

Code: Select all

getByName()
method to retrieve the instance then set the value with a method like the following:

Code: Select all

    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

NoSuchElementException
s, you can use the following code to print out the contents of the collection:

Code: Select all

for field_name in document.TextFieldMasters.getElementNames():
    print(field_name)
OpenOffice 3.4.0 on Ubuntu and Mac OSX
Post Reply