Page 1 of 1

Identify TextField through iteration

PostPosted: Wed Mar 06, 2019 5:56 pm
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

Re: Identify TextField through iteration

PostPosted: Wed Mar 06, 2019 7:52 pm
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

Re: Identify TextField through iteration

PostPosted: Wed Mar 06, 2019 8:27 pm
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   Expand viewCollapse view
    doc = thiscomponent
    datebookmark = doc.Bookmarks.getByName("theDate")
    datefield = datebookmark.Anchor.TextField
    datefield.Adjust = 60*24

Regards.

Re: Identify TextField through iteration

PostPosted: Wed Mar 06, 2019 10:01 pm
by UnklDonald418
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.

Re: Identify TextField through iteration

PostPosted: Thu Mar 07, 2019 10:17 am
by Villeroy
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

Re: Identify TextField through iteration

PostPosted: Thu Mar 07, 2019 3:24 pm
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.)

Re: Identify TextField through iteration

PostPosted: Fri Mar 08, 2019 10:43 am
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

Re: Identify TextField through iteration

PostPosted: Fri Mar 08, 2019 11:00 am
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

Re: Identify TextField through iteration

PostPosted: Fri Mar 08, 2019 3:53 pm
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.