[Solved] Put current date into Date Field

Creating a macro - Writing a Script - Using the API

[Solved] Put current date into Date Field

Postby The_Immortal » Thu Feb 27, 2014 9:48 am

Hello everybody!

I need to put current date into Date Field. I've found an official example how to do it:
Code: Select all   Expand viewCollapse view
oDateFieldModel = oDialog.Model.DateField1
oDateFieldModel.Date = CDateToIso( Date() )


But I get the error "Object variable not set" on
Code: Select all   Expand viewCollapse view
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
The_Immortal
 
Posts: 40
Joined: Thu Feb 13, 2014 12:46 am

Re: Put current date into Date Field

Postby Villeroy » Thu Feb 27, 2014 10:52 am

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   Expand viewCollapse view
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   Expand viewCollapse view
print TypeName(cDateToISO(Now())), cDateToISO(Now())

.. .reports 20140227 string

This might work:
Code: Select all   Expand viewCollapse view
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
User avatar
Villeroy
Volunteer
 
Posts: 29697
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Put current date into Date Field

Postby The_Immortal » Thu Feb 27, 2014 11:11 am

Villeroy, I do appreciate you for your investigation but this
Villeroy wrote:This might work:
Code: Select all   Expand viewCollapse view
oDateFieldModel.Date = cInt(CDateToIso( Date()))


actually doesn't work:

Image

:cry:
LibreOffice 4.2.0.4 - Windows 7
The_Immortal
 
Posts: 40
Joined: Thu Feb 13, 2014 12:46 am

Re: Put current date into Date Field

Postby Charlie Young » Thu Feb 27, 2014 11:27 am

Code: Select all   Expand viewCollapse view
oDateFieldModel = oDialog.Model.DateField1


Is not how controls are accessed.

Code: Select all   Expand viewCollapse view
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
User avatar
Charlie Young
Volunteer
 
Posts: 1559
Joined: Fri May 14, 2010 1:07 am

Re: Put current date into Date Field

Postby hanya » Thu Feb 27, 2014 11:31 am

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
hanya
Volunteer
 
Posts: 885
Joined: Fri Nov 23, 2007 9:27 am
Location: Japan

Re: Put current date into Date Field

Postby Villeroy » Thu Feb 27, 2014 11:41 am

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
Villeroy
Volunteer
 
Posts: 29697
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Put current date into Date Field

Postby karolus » Thu Feb 27, 2014 11:45 am

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)
User avatar
karolus
Volunteer
 
Posts: 866
Joined: Sat Jul 02, 2011 9:47 am

Re: Put current date into Date Field

Postby Villeroy » Thu Feb 27, 2014 11:54 am

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   Expand viewCollapse view
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
User avatar
Villeroy
Volunteer
 
Posts: 29697
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Put current date into Date Field

Postby The_Immortal » Thu Feb 27, 2014 12:01 pm

Charlie Young,

Image

The same result without CLng()... What I'm doing wrong now? :(
LibreOffice 4.2.0.4 - Windows 7
The_Immortal
 
Posts: 40
Joined: Thu Feb 13, 2014 12:46 am

Re: Put current date into Date Field

Postby karolus » Thu Feb 27, 2014 12:32 pm

@Immortal
read the Answers you've get!!
AOO4, Libreoffice 6.1 on Rasbian OS (on ARM)
User avatar
karolus
Volunteer
 
Posts: 866
Joined: Sat Jul 02, 2011 9:47 am

Re: Put current date into Date Field

Postby Villeroy » Thu Feb 27, 2014 12:38 pm

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
Villeroy
Volunteer
 
Posts: 29697
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Put current date into Date Field

Postby karolus » Thu Feb 27, 2014 1:33 pm

@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)
User avatar
karolus
Volunteer
 
Posts: 866
Joined: Sat Jul 02, 2011 9:47 am

Re: Put current date into Date Field

Postby Villeroy » Thu Feb 27, 2014 3:44 pm

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
Villeroy
Volunteer
 
Posts: 29697
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Put current date into Date Field

Postby Charlie Young » Thu Feb 27, 2014 4:44 pm

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   Expand viewCollapse view
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
Charlie Young
Volunteer
 
Posts: 1559
Joined: Fri May 14, 2010 1:07 am

Re: Put current date into Date Field

Postby Villeroy » Thu Feb 27, 2014 6:56 pm

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   Expand viewCollapse view
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
User avatar
Villeroy
Volunteer
 
Posts: 29697
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Put current date into Date Field

Postby B Marcelly » Fri Feb 28, 2014 5:55 pm

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   Expand viewCollapse view
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   Expand viewCollapse view
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
B Marcelly
Volunteer
 
Posts: 1160
Joined: Mon Oct 08, 2007 1:26 am
Location: France, Paris area

Re: Put current date into Date Field

Postby Villeroy » Fri Feb 28, 2014 7:18 pm

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
User avatar
Villeroy
Volunteer
 
Posts: 29697
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Put current date into Date Field

Postby The_Immortal » Fri Feb 28, 2014 7:32 pm

B Marcelly, could you tell me please how to adapte your code
Code: Select all   Expand viewCollapse view
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
The_Immortal
 
Posts: 40
Joined: Thu Feb 13, 2014 12:46 am

Re: Put current date into Date Field

Postby B Marcelly » Fri Feb 28, 2014 9:30 pm

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
B Marcelly
Volunteer
 
Posts: 1160
Joined: Mon Oct 08, 2007 1:26 am
Location: France, Paris area

Re: Put current date into Date Field

Postby The_Immortal » Fri Feb 28, 2014 9:44 pm

B Marcelly, thank you very much!

And thank you all guys!!!
LibreOffice 4.2.0.4 - Windows 7
The_Immortal
 
Posts: 40
Joined: Thu Feb 13, 2014 12:46 am


Return to Macros and UNO API

Who is online

Users browsing this forum: Burnart and 4 guests