Page 1 of 1

[Solved] Selecting and Updating a Text Field

Posted: Wed Jan 13, 2016 3:38 am
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"

Re: Selecting and Updating a Text Field

Posted: Wed Jan 13, 2016 3:54 am
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

Re: Selecting and Updating a Text Field

Posted: Wed Jan 13, 2016 4:10 am
by macroman25
Thanks FJCC
For some reason I get "Property or method not found: Content."
any ideas?

Re: Selecting and Updating a Text Field

Posted: Wed Jan 13, 2016 4:13 am
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.

Re: Selecting and Updating a Text Field

Posted: Wed Jan 13, 2016 4:17 am
by macroman25
Attached.

Re: Selecting and Updating a Text Field

Posted: Wed Jan 13, 2016 4:19 am
by macroman25
I can probably use a different field type if that's easier...

Re: Selecting and Updating a Text Field

Posted: Wed Jan 13, 2016 5:49 am
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.

Re: Selecting and Updating a Text Field

Posted: Wed Jan 13, 2016 5:58 am
by macroman25
OK Thanks, will give that a try... annoying that they can be so different...

Re: Selecting and Updating a Text Field

Posted: Wed Jan 13, 2016 6:10 am
by macroman25
Thanks FJCC - works a treat now!

Re: Selecting and Updating a Text Field

Posted: Fri Aug 02, 2019 1:01 am
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)