Identify TextField through iteration

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
Diez
Posts: 7
Joined: Tue Mar 05, 2019 10:37 pm

Identify TextField through iteration

Post by Diez »

Hello all.

I'm newbie at this forum but I'm searching in since days ago looking for an information about TextFields:

I have a document with many Date fields inserted on it, with different shifts in days. I'd like to change only one of it using a macro, I know how to change the Date modifying "Adjust" field, I know how to iterate through all TextField in the document, but I could'nt find any method to identify the DateTime I want to change, is there any way to identify inserted TextFields?

Regards and thanks in advanced
LibreOffice 6 on Debian Testing
User avatar
Villeroy
Volunteer
Posts: 31269
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Identify TextField through iteration

Post by Villeroy »

Install the MRI extension. Latest version on github: https://github.com/hanya/MRI/blob/maste ... -1.3.1.oxt
Restart the office.
Open a text document with fields.
menu:Tools>AddOns>Mri
Double-click the line starting with "TextFields" on the properties tab.
Switch to the methods tab and double-click "createEnumeration" then "nextElement".
Go back and double-click "nextElement" until you have the wanted date field. It has a property "DateTimeValue" which is a com.sun.star.util.DateTime struct.
Hit Ctrl+H for a code snippet.

In your own code create a new DateTime struct, fill in values and assign it to the DateTimeValue property of your object.

[Tutorial] Introduction into object inspection with MRI
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
hubert lambert
Posts: 145
Joined: Mon Jun 13, 2016 10:50 am

Re: Identify TextField through iteration

Post by hubert lambert »

Hi,

I can't see a way to identify a given date field among other date fields, but inserting a bookmark at that field will help. Let's say this bookmark is named "theDate":

Code: Select all

    doc = thiscomponent
    datebookmark = doc.Bookmarks.getByName("theDate")
    datefield = datebookmark.Anchor.TextField
    datefield.Adjust = 60*24
Regards.
Last edited by hubert lambert on Thu Mar 07, 2019 3:47 pm, edited 1 time in total.
AOOo 4.1.2 on Win7 | LibreOffice on various Linux systems
UnklDonald418
Volunteer
Posts: 1544
Joined: Wed Jun 24, 2015 12:56 am
Location: Colorado, USA

Re: Identify TextField through iteration

Post by UnklDonald418 »

Using the MRI tool on a Writer document with some inserted datetime values I can test a textfield with

Code: Select all

oTextField.getPropertySetInfo().hasPropertyByName("DateTimeValue")
if it returns TRUE I know that textfield contains a datetime value.
If your problem has been solved, please edit this topic's initial post and add "[Solved]" to the beginning of the subject line
Apache OpenOffice 4.1.14 & LibreOffice 7.6.2.1 (x86_64) - Windows 10 Professional- Windows 11
User avatar
Villeroy
Volunteer
Posts: 31269
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Identify TextField through iteration

Post by Villeroy »

Basic code produced by Mri with LO 6.0

Code: Select all

Sub Snippet
  Dim oTextFields As Variant
  Dim oObj1 As Variant
  Dim oObj2 As Variant
  Dim bIsDate As Boolean
  Dim nIsFieldDisplayed As Integer
  Dim nIsFieldUsed As Single
  Dim bIsFixed As Boolean
  Dim bIsFixedLanguage As Boolean
  Dim nNumberFormat As Long
  Dim aDateTimeValue As New com.sun.star.util.DateTime

  oTextFields = ThisComponent.getTextFields()
  oObj1 = oTextFields.createEnumeration()
  oObj2 = oObj1.nextElement()
  
  bIsDate = oObj2.IsDate
  nIsFieldDisplayed = oObj2.IsFieldDisplayed
  nIsFieldUsed = oObj2.IsFieldUsed
  
  bIsFixed = oObj2.IsFixed
  bIsFixedLanguage = oObj2.IsFixedLanguage
  nNumberFormat = oObj2.NumberFormat
  
  aDateTimeValue = oObj2.DateTimeValue
End Sub
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
User avatar
Lupp
Volunteer
Posts: 3542
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

Re: Identify TextField through iteration

Post by Lupp »

Diez wrote:...but I could'nt find any method to identify the DateTime I want to change,...


By what
do you want to identify the TextField you are talking of?
-- By its value (date only or date-time)? What if there is more than one occurrence?
-- By position in the document? How to describe that position? If you cannot address the field, you cannot identify it.
-- By what else?

(If there are dates defined with an offset by days, I don't know anyway how to handle them.)
On Windows 10: LibreOffice 24.2 (new numbering) and older versions, PortableOpenOffice 4.1.7 and older, StarOffice 5.2
---
Lupp from München
Diez
Posts: 7
Joined: Tue Mar 05, 2019 10:37 pm

Re: Identify TextField through iteration

Post by Diez »

Hello all.

I've tested the Bookmarks proposal and I think is enough for me. I've discarded the MRI because implies (I think) to install on all computers where it will used the code. The code will be into a template to be distributed. The idea is to install as little as possible.

In any case, is strange for me that inserted fields have not any kind of identification.

Thank you
LibreOffice 6 on Debian Testing
JeJe
Volunteer
Posts: 2764
Joined: Wed Mar 09, 2016 2:40 pm

Re: Identify TextField through iteration

Post by JeJe »

MRI is a tool for examining objects and seeing what properties they have - you only need to install it on the computer you write the code on as a help with writing the code. I consider it indespensible - you want it!

Basic IDE Tools is another every helpful extension:

https://extensions.openoffice.org/fr/pr ... -ide-tools
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
User avatar
Lupp
Volunteer
Posts: 3542
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

Re: Identify TextField through iteration

Post by Lupp »

I would like to add a few remarks.

To get the DateTime fields we needn't loop through all the TextFields. The first TextField of DateTime type (supporting the service "com.sun.star.text.TextField.DateTime") we find points to the common TextFieldMaster of all DateTime fields contained in the document. That object in turn gives access to all its dependent fields via an array object.

The suggestion to use (a) bookmark(s) to point to DateTime fields needing an update now and then is well suitable, but the bookmarks will be shown in the navigator panel, and may cause some irritation probably. There is an alternative to "name" any TextField.
It has an .Anchor property, and the Anchor is prepared to accept a HyperLink. There are the three String properties HyperLinkURL, HyperLinkTarget, and HyperLinkName. The HyperLinkName can be (slightly mis-)used as a name for the TextField itself without setting an URL or a Target.

See attached example.

===Edit1 2019-03-08 16:15 CET===
Forget it!
Lupp wrote:The HyperLinkName can be (slightly mis-)used as a name for the TextField itself without setting an URL or a Target.
This seems to work first - and it works as long as the file is open. However, the HyperLinkName isn't saved if not also a link is set.
What I stated otherwise seems to be valid.
Attachments
aooMarkDateTimeFields_1.odt
(23.62 KiB) Downloaded 212 times
On Windows 10: LibreOffice 24.2 (new numbering) and older versions, PortableOpenOffice 4.1.7 and older, StarOffice 5.2
---
Lupp from München
Post Reply