Ask Your Question
0

Date Field Macro Problem

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

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 close merge delete

1 Answer

Sort by » oldest newest most voted
1

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

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

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

Comments

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 +0200 )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 +0200 )edit

That was fast. Thanks again.

Case Sensitive gravatar imageCase Sensitive ( 2017-11-27 10:09:46 +0200 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

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

Seen: 200 times

Last updated: Nov 19 '17