[Solved] Put current date into Date Field

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
The_Immortal
Posts: 40
Joined: Thu Feb 13, 2014 12:46 am

[Solved] Put current date into Date Field

Post by The_Immortal »

Hello everybody!

I need to put current date into Date Field. I've found an official example how to do it:

Code: Select all

oDateFieldModel = oDialog.Model.DateField1
oDateFieldModel.Date = CDateToIso( Date() )
But I get the error "Object variable not set" on

Code: Select all

oDateFieldModel.Date = CDateToIso( Date() )
Could you tell me please what's the problem?
Last edited by The_Immortal on Fri Feb 28, 2014 9:44 pm, edited 1 time in total.
LibreOffice 4.2.0.4 - Windows 7
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Put current date into Date Field

Post by Villeroy »

Documentation on com/sun/star/awt/UnoControlDateFieldModel.html#Date
long Date;

Description
specifies the date displayed in the control.

The date must be specified in the format YYYYMMDD.
Binding a date control's change event to Basic MriLib.Module1.Mri I get:

Code: Select all

Sub Snippet(Optional oInitialTarget As Object)
  Dim oSource As Variant
  Dim oModel As Variant

  oSource = oInitialTarget.Source
  oModel = oSource.getModel()
End Sub
which shows all properties including this:
Date long 20140214 Maybevoid,Bound,Maybedefault 95

The Basic documentation is unclear about the return type of cDateToIso, however ...

Code: Select all

print TypeName(cDateToISO(Now())), cDateToISO(Now())
.. .reports 20140227 string

This might work:

Code: Select all

oDateFieldModel.Date = cInt(CDateToIso( Date()))
Someone should update the wiki and the F1 help too.

Of course, all this silly Basic hacking would be obsolete if you would use a database able to use the current date as default values for a date field.
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
The_Immortal
Posts: 40
Joined: Thu Feb 13, 2014 12:46 am

Re: Put current date into Date Field

Post by The_Immortal »

Villeroy, I do appreciate you for your investigation but this
Villeroy wrote: This might work:

Code: Select all

oDateFieldModel.Date = cInt(CDateToIso( Date()))
actually doesn't work:

Image

:cry:
LibreOffice 4.2.0.4 - Windows 7
User avatar
Charlie Young
Volunteer
Posts: 1559
Joined: Fri May 14, 2010 1:07 am

Re: Put current date into Date Field

Post by Charlie Young »

Code: Select all

oDateFieldModel = oDialog.Model.DateField1
Is not how controls are accessed.

Code: Select all

oDateFieldModel = oDialog.getControl("DateField1").getModel()
Should work.

I thought at first that Villeroy's hunch about converting to integer was right, but my test works fine with cDateToIso(Date()).

CInt() won't work because the number is too big, it works with CLng()
Apache OpenOffice 4.1.1
Windows XP
hanya
Volunteer
Posts: 885
Joined: Fri Nov 23, 2007 9:27 am
Location: Japan

Re: Put current date into Date Field

Post by hanya »

Is this related to the changes [1] on LibreOffice 4.1?
[1] https://wiki.documentfoundation.org/Rel ... o_UNO_APIs
Please, edit this thread's initial post and add "[Solved]" to the subject line if your problem has been solved.
Apache OpenOffice 4-dev on Xubuntu 14.04
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Put current date into Date Field

Post by Villeroy »

hanya wrote:Is this related to the changes [1] on LibreOffice 4.1?
[1] https://wiki.documentfoundation.org/Rel ... o_UNO_APIs
I don't understand anything of that. Does that mean that Basic function cInt does not work anymore?
And why do these changes in LibreOffice make it into OpenOffice?
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
karolus
Volunteer
Posts: 1160
Joined: Sat Jul 02, 2011 9:47 am

Re: Put current date into Date Field

Post by karolus »

Hallo
hanya wrote:Is this related to the changes [1] on LibreOffice 4.1?
[1] https://wiki.documentfoundation.org/Rel ... o_UNO_APIs
Yes, it is!
Villeroy wrote:I don't understand anything of that. Does that mean that Basic function cInt does not work anymore?
No, it means that the API for DateFields and TimeFields has changed.


Karolus
AOO4, Libreoffice 6.1 on Rasbian OS (on ARM)
Libreoffice 7.4 on Debian 12 (Bookworm) (on RaspberryPI4)
Libreoffice 7.6 flatpak on Debian 12 (Bookworm) (on RaspberryPI4)
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Put current date into Date Field

Post by Villeroy »

OK, LibreOffice changed its API but OpenOffice did not.
My own mistake was using cInt instead of cLng. F*g Basic has 2 integer types. cInt has only 16 bytes (-32.767 to +32768) which is not enough.

Code: Select all

oDateFieldModel.Date = cLng(CDateToIso( Date()))
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
The_Immortal
Posts: 40
Joined: Thu Feb 13, 2014 12:46 am

Re: Put current date into Date Field

Post by The_Immortal »

Charlie Young,

Image

The same result without CLng()... What I'm doing wrong now? :(
LibreOffice 4.2.0.4 - Windows 7
User avatar
karolus
Volunteer
Posts: 1160
Joined: Sat Jul 02, 2011 9:47 am

Re: Put current date into Date Field

Post by karolus »

@Immortal
read the Answers you've get!!
AOO4, Libreoffice 6.1 on Rasbian OS (on ARM)
Libreoffice 7.4 on Debian 12 (Bookworm) (on RaspberryPI4)
Libreoffice 7.6 flatpak on Debian 12 (Bookworm) (on RaspberryPI4)
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Put current date into Date Field

Post by Villeroy »

Well, the Basic function to return the current date and time as a Basic value is Now(). Basic has no Date() function therefore Basic can not resulve what Date refers to.

So we convert the result of function Now() to an 8-digit string (cDateToIso ignores any hours, minutes and seconds) and then we convert the string to a long integer which should be assignable to the date control's Date property which requires a long integer with 8 digits representing YYYYMMDD.
All this is so confusing and dull at the same time that I strongly suggest to abandon all this Basic-in-Spreadsheets mess.
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
karolus
Volunteer
Posts: 1160
Joined: Sat Jul 02, 2011 9:47 am

Re: Put current date into Date Field

Post by karolus »

@Villeroy
+1
but here the half of the mess comes from the Sun-Hacker who early in the 90th decide for this courious "Format" to store the Values of DateFields.

Karolus
AOO4, Libreoffice 6.1 on Rasbian OS (on ARM)
Libreoffice 7.4 on Debian 12 (Bookworm) (on RaspberryPI4)
Libreoffice 7.6 flatpak on Debian 12 (Bookworm) (on RaspberryPI4)
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Put current date into Date Field

Post by Villeroy »

karolus wrote:@Villeroy
+1
but here the half of the mess comes from the Sun-Hacker who early in the 90th decide for this courious "Format" to store the Values of DateFields.

Karolus
In the early 90ies it was a hacker of the German "Star Division". Let's find out who it was ... I'll sue him. :mrgreen:
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
Charlie Young
Volunteer
Posts: 1559
Joined: Fri May 14, 2010 1:07 am

Re: Put current date into Date Field

Post by Charlie Young »

The_Immortal wrote:Charlie Young,

Image

The same result without CLng()... What I'm doing wrong now? :(
As discussed , this must be something peculiar to LibreOffice, which I've never ventured into.

On AOO, I was surprised to find that

Code: Select all

oDateFieldModel = oDialog.Model.DateField1
Is, in fact, valid. So The original code works for me, and then with or without CLng.
Apache OpenOffice 4.1.1
Windows XP
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Put current date into Date Field

Post by Villeroy »

Hanya's link to the LO site includes a Basic snippet which tests the supported type for a date control. It used to be the 8-digit integer YYYY-MM-DD and now they changed it into an UNO-struct which will break old macros and won't work with older versions of LibreOffice. The latter case can be handled like this:

[UNTESTED SNIPPET]

Code: Select all

Sub Blah()
[...] 
oDtCtrl = oDialog.getControl("MyDate")
basNow = Now()
bStruct = useDateStruct()
if bStruct then  
  varDate = createUnoStruct("com.sun.star.util.Date")
  varDate.Year = Year(basNow)
  varDate.Month = Month(basNow)
  varDate.Day = Day(basNow)
else
  varDate = cDbl(cDateToIso(basNow))
endif
oDtCtrl.setDate(varDate)
[...]
End Sub

Function useDateStruct()
REM returns true if a date control uses c.s.s.util.Date
REM returns false if a date control uses an 8-digit integer YYYY-MM-DD
Dim OOoReflection 
    Set OOoReflection = CreateUnoService("com.sun.star.reflection.CoreReflection")
    Dim gD as Object
    Set gD = OOoReflection.forName("com.sun.star.awt.XDateField").getMethod("getDate").ReturnType
    if gD.TypeClass = com.sun.star.uno.TypeClass.LONG then
        gbDateIsStruct = false
    elseif gD.TypeClass = com.sun.star.uno.TypeClass.STRUCT And gD.name="com.sun.star.util.Date" then
        gbDateIsStruct = true
    else
        MsgBox "Unknown situation"
    end if
End Function
Of course our VBA heroes will complain that working with UNO structs is even more complicated (more typing effort) than working with a strange integer.
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
B Marcelly
Volunteer
Posts: 1160
Joined: Mon Oct 08, 2007 1:26 am
Location: France, Paris area

Re: Put current date into Date Field

Post by B Marcelly »

Villeroy's code does not work because it uses varDate as an object and as an ordinary variable.
OpenOffice Basic does not like this ! Example :

Code: Select all

Option Explicit

Sub Main2
Dim a As Variant
a = 3
if a = 0  then
  a = ThisComponent.CharLocale
  a.Language = "zzz"
end if
a = 4  ' error : undefined object variable
End Sub
This is a variant of the bug 70616 that I issued in 2006, and still valid...

Villeroy's code may be simplified and adapted to avoid the bug :

Code: Select all

Dim varDate As Variant

oDtCtrl = oDialog.getControl("MyDate")
basNow = Now()
varDate = oDtCtrl.Date
if IsUnoStruct(varDate) then ' LibreOffice >= 4.1.1
  varDate.Year = Year(basNow)
  varDate.Month = Month(basNow)
  varDate.Day = Day(basNow)
  oDtCtrl.Date = varDate
else ' Apache OpenOffice
  oDtCtrl.Date = CLng(cDateToIso(basNow))
endif
In a general case, use a Variant variable to handle the case of object, and another variable to handle the case of a numeric type.
Bernard

OpenOffice.org 1.1.5 / Apache OpenOffice 4.1.1 / LibreOffice 5.0.5
MS-Windows 7 Home SP1
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Put current date into Date Field

Post by Villeroy »

Thank you, Bernard.
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
The_Immortal
Posts: 40
Joined: Thu Feb 13, 2014 12:46 am

Re: Put current date into Date Field

Post by The_Immortal »

B Marcelly, could you tell me please how to adapte your code

Code: Select all

Dim varDate As Variant

oDtCtrl = oDialog.getControl("MyDate")
basNow = Now()
varDate = oDtCtrl.Date
if IsUnoStruct(varDate) then ' LibreOffice >= 4.1.1
  varDate.Year = Year(basNow)
  varDate.Month = Month(basNow)
  varDate.Day = Day(basNow)
  oDtCtrl.Date = varDate
else ' Apache OpenOffice
  oDtCtrl.Date = CLng(cDateToIso(basNow))
endif
for LibreOffice < 4.1.1 ?
Or the provided code will work fine?
LibreOffice 4.2.0.4 - Windows 7
B Marcelly
Volunteer
Posts: 1160
Joined: Mon Oct 08, 2007 1:26 am
Location: France, Paris area

Re: Put current date into Date Field

Post by B Marcelly »

My code works for all known variants of OpenOffice API.
The part indicated "Apache OpenOffice" is also used by all versions of OpenOffice.org and by LibreOffice of versions < 4.1.1
Bernard

OpenOffice.org 1.1.5 / Apache OpenOffice 4.1.1 / LibreOffice 5.0.5
MS-Windows 7 Home SP1
The_Immortal
Posts: 40
Joined: Thu Feb 13, 2014 12:46 am

Re: Put current date into Date Field

Post by The_Immortal »

B Marcelly, thank you very much!

And thank you all guys!!!
LibreOffice 4.2.0.4 - Windows 7
Post Reply