[Solved] Calc Macro - DAYSINMONTH gives error

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
hopeman
Posts: 3
Joined: Tue Jan 04, 2022 4:13 pm

[Solved] Calc Macro - DAYSINMONTH gives error

Post by hopeman »

I am trying to create a macro which uses spreadsheet function DAYSINMONTH() but it gives me error. Here is my code :

Code: Select all

Dim spreadsheetService As Object
Dim daysInMonth As Integer 

spreadsheetService = createUnoService("com.sun.star.sheet.FunctionAccess")
daysInMonth=spreadsheetService.callFunction("DAYSINMONTH", "2021-01-01") 

Why the last line gives "Runtime Error : Object variable not set" error ?
Last edited by MrProgrammer on Tue Jan 04, 2022 6:15 pm, edited 3 times in total.
Reason: Added green tick [RoryOF, Moderator]
hopeman : a man with high hopes : OpenOffice 7.0.4.2 on Debian
FJCC
Moderator
Posts: 9280
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Calc - Macro Program - why DAYSINMONTH gives error ?

Post by FJCC »

The second argument of callFunction needs to be an array.

Code: Select all

spreadsheetService = createUnoService("com.sun.star.sheet.FunctionAccess")
daysInMonth=spreadsheetService.callFunction("DAYSINMONTH", array("2021-01-01"))
print daysInMonth
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
hopeman
Posts: 3
Joined: Tue Jan 04, 2022 4:13 pm

Re: Calc - Macro Program - why DAYSINMONTH gives error ?

Post by hopeman »

Hi FJCC,
Thanks for fast answer! But this time I got this error :
Attachments
error
error
hopeman : a man with high hopes : OpenOffice 7.0.4.2 on Debian
hopeman
Posts: 3
Joined: Tue Jan 04, 2022 4:13 pm

Re: Calc - Macro Program - why DAYSINMONTH gives error ?

Post by hopeman »

FJCC wrote:The second argument of callFunction needs to be an array.

Code: Select all

spreadsheetService = createUnoService("com.sun.star.sheet.FunctionAccess")
daysInMonth=spreadsheetService.callFunction("DAYSINMONTH", array("2021-01-01"))
print daysInMonth

OK!! Thanks FJCC, your solution works ! I did not know that I can use function's localized names (for instance my office if in Turkish and DAYSINMONTH replaced by AYDAKİGÜNLER) . So when I used Turkish function name, it worked !
I learnt that :
1 . callFunction always use array for function parameters
2. I can use function names of my local language in OpenOffice
Thanks.
hopeman : a man with high hopes : OpenOffice 7.0.4.2 on Debian
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: [Solved]Calc - Macro Program - why DAYSINMONTH gives err

Post by Villeroy »

The true name of DAYSINMONTH is "com.sun.star.sheet.addin.DateFunctions.getDaysInMonth" because it is an additional function. This function is not part of the Open Document Standard.

Enter =AYDAKİGÜNLER("2021-01-01") into some cell, select the cell and call this macro:

Code: Select all

Sub showFormula()
inputbox "","", ThisComponent.CurrentSelection.getFormula()
End Sub
This should work with all user interface languages:

Code: Select all

daysInMonth=spreadsheetService.callFunction("com.sun.star.sheet.addin.DateFunctions.getDaysInMonth", array("2021-01-01"))
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
Post Reply