# How to create a Macro to insert the current date in the current language.

I write documents in Thai and English sometimes using both in one document. I would like to create a macro to insert the current date for me in whatever language is currently being used in the document. I can do this manually with: Insert > Field > More Fields > Date > Date (Fixed) > Additional Formats > Language = Automatic I created a Macro to do this (see below) but it comes out only in English. What can I do to my Macro to make the date format come out in the language currently being used in the document? How can I insert as a field?

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

rem ----------------------------------------------------------------------
dim sText as string
sText = Format(Now(),"D MMMM YYYY")

dim args1(5) as new com.sun.star.beans.PropertyValue
args1(2).Name = "Text"
args1(2).Value = sText

dispatcher.executeDispatch(document, ".uno:InsertText", "", 0, args1())

end sub

edit retag close merge delete

Sort by » oldest newest most voted

Remark:
I'm afraid the answer won't help yo much because I don't know a way to get the Format fucntion of BASIC to apply locale specific format codes in a locale specific way. I post the answer nonetheless to encourage you or somone else to add the missing part. Sorry! The present state of the localization field (formatting and recognition of numbers and "special numbers") is a mess, imo. One clearly worded related question I found here. The thread did not lead to a usable answer in the end.

Based on the comments by @Mike Kaganski I reworked the answer below.

Your code line sText = Format(Now(),"D MMMM YYYY") needs to be replaced by code where the format code depends on the current language. I cannot test with Thai.

_...
currentLoc = getCurrentCharLocale(ThisComponent)
If TypeName(currentLoc)="Empty" Then Exit Sub
REM Otherwise the next statement would throw an error.
Select Case currentLoc.Language REM You may concatenate additional attributes if needed.
Case "en" : fmtC = "D MMMM YYYY"

sText = Format(Now(),"[$-41e]D MMMM YYYY") ' Thai  3. The LangIDs might be taken from this page. This, however, needs the parts from @Lupp's answer to get the current char locale to create complete solution. EDIT 2017-10-31 To go with field creation, you may use this code: sub Insert_Date_Auto dim document as object dim dispatcher as object dim oLocale as object oLocale = ThisComponent.CurrentController.GetViewCursor.CharLocale dim nFmtId as Long nFmtId = ThisComponent.NumberFormats.queryKey("DD MMMM YYYY", oLocale, False) if nFmtId = -1 then nFmtId = ThisComponent.NumberFormats.addNew("DD MMMM YYYY", oLocale) end if dim args1(3) as new com.sun.star.beans.PropertyValue args1(0).Name = "Type" args1(0).Value = 0 ' date args1(1).Name = "SubType" args1(1).Value = 0 ' fixed args1(2).Name = "Name" args1(2).Value = "" ' to avoid opening dialog args1(3).Name = "Format" args1(3).Value = nFmtId document = ThisComponent.CurrentController.Frame dispatcher = createUnoService("com.sun.star.frame.DispatchHelper") dispatcher.executeDispatch(document, ".uno:InsertField", "", 0, args1()) end sub  more ## Comments @Mike Kaganski Thanks! It was time for me to learn this usage of the [$-n] prefixes to format codes.
However, there is surely an internal reference to a lookup table (even more complete) containing the info from the MS page you linked?

( 2017-10-30 16:10:25 +0100 )edit

Yes, this is lang.h. The LCIDs there hexadecimals with leading 0x, which should be omitted in format strings.

( 2017-10-30 17:48:40 +0100 )edit

A running LibO also needs to lookup the pairs of lang-description in strings and the HEX representations, most likely in both directions. How does it? How can I do by user code?

( 2017-10-30 23:36:58 +0100 )edit

Well, C++ implementation is using mslangid.hxx (see also isolang.cxx). I don't see direct mapping of the class to UNO.

( 2017-10-31 06:10:49 +0100 )edit

Thanks to @Mike Kaganski, I looked at the Options - Language Settings - Languages in Writer. I changed the Locale setting to Thai. This brought up a Complex Text layout (CTL) with the default of Hindi; I changed it to Thai. I had tried to record a macro before and it inserted Hindi instead of Thai. This is why. So, I recorded a new macro. It works! Below is the code it generated. Perhaps someone can comment it so we can understand it better. It seems the trick is in the Format value of 20121. Where could we look this up? I must say I''m impressed by the caliber and depth of responses to my question - thanks!

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

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

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

end sub

more

I would suppose it inserts a date in one specific format defined for your documents and identified by the number 20120. This is what Mike Kaganski had introduced the variable nFmtId for. Concerning the correct assignment you need to complete the 5 respective lines of his code with an alternative based on the function I posted and with your correct Thai DateFormatCode.