Macro update from LO 6 to 7

I like to use a macro to insert the current date in documents, formatted as “Friday, 31 December 1999”.
Using LO 6 I had a macro to do this easily, with an assigned shortcut key.
After upgrading to LO 7.4.5.1, my macro now inserts a numeric value, not a date.
Please, what formatting instruction do I need to include in the macro to help this work as intended ?

sub InsertDate
rem ----------------------------------------------------------------------
rem define variables
dim document   as object
dim dispatcher as object
rem ----------------------------------------------------------------------
rem get access to the document
document   = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")

rem ----------------------------------------------------------------------

dim oLocale as object
oLocale = ThisComponent.CurrentController.GetViewCursor.CharLocale
dim nFmtId as Long
nFmtId = ThisComponent.NumberFormats.queryKey("DD/MM/YY", oLocale, False)
if nFmtId = -1 then
    nFmtId = ThisComponent.NumberFormats.addnew("DD/MM/YY", oLocale)
end if

rem ----------------------------------------------------------------------

dim args2(5) as new com.sun.star.beans.PropertyValue
args2(0).Name = "Type"
args2(0).Value = 0
args2(1).Name = "SubType"
args2(1).Value = 0
args2(2).Name = "Name"
args2(2).Value = ""
args2(3).Name = "Content"
args2(3).Value = "0"
args2(4).Name = "Format"
args2(4).Value = 10079
args2(5).Name = "Separator"
args2(5).Value = " "

dispatcher.executeDispatch(document, ".uno:InsertField", "", 0, args2())

rem ----------------------------------------------------------------------
dispatcher.executeDispatch(document, ".uno:InsertPara", "", 0, Array())

end sub

Always? Regardless of which locale is used?

Looks like the format value must be changed from 10079 to 10044.
Is this the-right-way to do it ?

No. The number format id is a dynamic thing, and will change from session to session, depending on which documents with which custom number formats had been open.

See How to create a Macro to insert the current date in the current language, which shows how to find the actual format id for a specified format string.

1 Like

For the “always English” and “hard defined format” as given in the question case:

' Use en-US locale.
Dim oLocale as New com.sun.star.lang.Locale
oLocale.Language = "en"
oLocale.Country = "US"

' Obtain a format key for "Friday, 31 December 1999", or insert format.
sFormat = "DDDD, D MMMM YYYY"
nFmtId = ThisComponent.NumberFormats.queryKey(sFormat, oLocale, False)
if nFmtId = -1 then
    nFmtId = ThisComponent.NumberFormats.addnew(sFormat, oLocale)
end if
[...]
' Use that format key.
args2(4).Value = nFmtId
1 Like