Ask Your Question

Date Field Macro Problem [closed]

asked 2017-11-19 08:00:40 +0100

Case Sensitive gravatar image

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.

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by Alex Kemp
close date 2020-10-20 16:53:43.412535

1 Answer

Sort by » oldest newest most voted

answered 2017-11-19 08:20:51 +0100

updated 2017-11-19 08:23:47 +0100

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.

edit flag offensive delete link more


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.

Case Sensitive gravatar imageCase Sensitive ( 2017-11-27 09:47:02 +0100 )edit

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

Mike Kaganski gravatar imageMike Kaganski ( 2017-11-27 09:53:42 +0100 )edit

That was fast. Thanks again.

Case Sensitive gravatar imageCase Sensitive ( 2017-11-27 10:09:46 +0100 )edit

Question Tools

1 follower


Asked: 2017-11-19 08:00:40 +0100

Seen: 457 times

Last updated: Nov 19 '17