Date Field Macro Problem

In this document:

image description

LibreOffice Writer inserted a date correctly formatted, according to my macro.

This is the format code in the macro:

image description

In a different document:

image description

Using the same macro, LibreOffice Writer inserted the date incorrectly, as a number.

This is the format code in the macro:

image description

The entire code of the macro, as viewed in each document, is identical.

Please tell me why the same code produces a correct format in one document, but not in the other. Both documents were recently created, using the most recent version of OpenOffice.

Please tell me how I fix this.

Number format codes are assigned dynamically based on used formats (including custom user formats) and languages, so using hardcoded codes is not good.

Please see this answer for how to get code for desired format at run time.

Essentially, it is

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

Of course, you might want to use some hardcoded locale, if required.

Thank you very much. Can I open the macro, delete the existing commands, and paste in your code, and save?

In both cases, the macro was one I had recorded, so I had no idea it was hard coded. “10030” is a hard-coded format?

I’m not going to learn LibreOffice Basic quickly, but is there a reference where I can find such information? None of the user documentation I can find on the LibreOffice site even comes close. Extensive Google searches have not turned up anything.

In your macro, paste my code before ~everything; then instead of 10030, use nFmtId.

That was fast. Thanks again.