Identify TextField through iteration

Creating a macro - Writing a Script - Using the API

Identify TextField through iteration

Postby Diez » Wed Mar 06, 2019 5:56 pm

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
Diez
 
Posts: 7
Joined: Tue Mar 05, 2019 10:37 pm

Re: Identify TextField through iteration

Postby Villeroy » Wed Mar 06, 2019 7:52 pm

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, no OpenOffice, LibreOffice 6.x
User avatar
Villeroy
Volunteer
 
Posts: 26980
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Identify TextField through iteration

Postby hubert lambert » Wed Mar 06, 2019 8:27 pm

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   Expand viewCollapse view
    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
hubert lambert
 
Posts: 129
Joined: Mon Jun 13, 2016 10:50 am

Re: Identify TextField through iteration

Postby UnklDonald418 » Wed Mar 06, 2019 10:01 pm

Using the MRI tool on a Writer document with some inserted datetime values I can test a textfield with
Code: Select all   Expand viewCollapse view
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.6 & LibreOffice 6.1.5.2 - Windows 10 Professional
UnklDonald418
Volunteer
 
Posts: 1200
Joined: Wed Jun 24, 2015 12:56 am
Location: Colorado, USA

Re: Identify TextField through iteration

Postby Villeroy » Thu Mar 07, 2019 10:17 am

Basic code produced by Mri with LO 6.0
Code: Select all   Expand viewCollapse view
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, no OpenOffice, LibreOffice 6.x
User avatar
Villeroy
Volunteer
 
Posts: 26980
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Identify TextField through iteration

Postby Lupp » Thu Mar 07, 2019 3:24 pm

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 6.2 and older versions, PortableOpenOffice 4.1.5 and older, StarOffice 5.2
---
Lupp from München
User avatar
Lupp
Volunteer
 
Posts: 2523
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

Re: Identify TextField through iteration

Postby Diez » Fri Mar 08, 2019 10:43 am

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
Diez
 
Posts: 7
Joined: Tue Mar 05, 2019 10:37 pm

Re: Identify TextField through iteration

Postby JeJe » Fri Mar 08, 2019 11:00 am

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
Openoffice 4.1.2
Windows 8
JeJe
Volunteer
 
Posts: 552
Joined: Wed Mar 09, 2016 2:40 pm

Re: Identify TextField through iteration

Postby Lupp » Fri Mar 08, 2019 3:53 pm

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 38 times
On Windows 10: LibreOffice 6.2 and older versions, PortableOpenOffice 4.1.5 and older, StarOffice 5.2
---
Lupp from München
User avatar
Lupp
Volunteer
 
Posts: 2523
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany


Return to Macros and UNO API

Who is online

Users browsing this forum: No registered users and 2 guests