Number Format Style NumberFormat GetNumberFormats FormatString QueryKey QueryKeys Andrew Pitonyak Laurent Godard Michael HŮnnig Tom Schindl How can I find and create number formats?

If you want a particular number format, then you can see if you have it and create it if you do not. For more information on valid formats, see the help contents on topic "number formats; formats". They can be very complex. The following macro creates a number format if it does not exist. The appropriate number format is returned.

'****************************************************************** 'Author: Andrew Pitonyak 'email: andrew@pitonyak.org Function FindCreateNumberFormatStyle (_ sFormat As String, Optional doc, Optional locale) Dim oDocument As Object Dim aLocale as new com.sun.star.lang.Locale Dim oFormats As Object oDocument = IIf(IsMissing(doc), ThisComponent, doc) oFormats = oDocument.getNumberFormats() 'If you choose to query on types, you need to use the type 'com.sun.star.util.NumberFormat.DATE 'I could set the locale from values stored at 'http://www.ics.uci.edu/pub/ietf/http/related/iso639.txt 'http://www.chemie.fu-berlin.de/diverse/doc/ISO_3166.html 'I use a NULL locale and let it use what ever it likes. 'First, see if the number format exists If ( Not IsMissing(locale)) Then aLocale = locale End If formatNum = oFormats.queryKey (sFormat, aLocale, TRUE) MsgBox "Current Format number is" & formatNum 'If the number format does not exist then add it If (formatNum = -1) Then formatNum = oFormats.addNew(sFormat, aLocale) If (formatNum = -1) Then formatNum = 0 MsgBox "new Format number is " & formatNum End If FindCreateNumberFormatStyle = formatNum End Function

If you want to see all of your existing number formats, try something like the following, which is a macro that I modified from Laurent Godard. The following macro enumerates the current number format styles. The style numbers (keys) and their text representation are inserted into the current document. The disadvantage to this version is that it enumerates the styles based on the locale. The original version enumerated the key from 0 to 1000, ignoring errors. This will find all formats regardless of locale, but I consider this macro a slightly cleaner solution.

'****************************************************************** 'Author: Andrew Pitonyak 'email: andrew@pitonyak.org Function FindCreateNumberFormatStyle (_ sFormat As String, Optional doc, Optional locale) Dim oDocument As Object Dim aLocale as new com.sun.star.lang.Locale Dim oFormats As Object oDocument = IIf(IsMissing(doc), ThisComponent, doc) oFormats = oDocument.getNumberFormats() 'If you choose to query on types, you need to use the type 'com.sun.star.util.NumberFormat.DATE 'I could set the locale from values stored at 'http://www.ics.uci.edu/pub/ietf/http/related/iso639.txt 'http://www.chemie.fu-berlin.de/diverse/doc/ISO_3166.html 'I use a NULL locale and let it use what ever it likes. 'First, see if the number format exists If ( Not IsMissing(locale)) Then aLocale = locale End If formatNum = oFormats.queryKey (sFormat, aLocale, TRUE) MsgBox "Current Format number is" & formatNum 'If the number format does not exist then add it If (formatNum = -1) Then formatNum = oFormats.addNew(sFormat, aLocale) If (formatNum = -1) Then formatNum = 0 MsgBox "new Format number is " & formatNum End If FindCreateNumberFormatStyle = formatNum End Function

If you want to see all of your existing number formats, try something like the following, which is a macro that I modified from Laurent Godard. The following macro enumerates the current number format styles. The style numbers (keys) and their text representation are inserted into the current document. The disadvantage to this version is that it enumerates the styles based on the locale. The original version enumerated the key from 0 to 1000, ignoring errors. This will find all formats regardless of locale, but I consider this macro a slightly cleaner solution.

Sub enumFormats() 'Author : Laurent Godard 'e-mail : listes.godard@laposte.net 'Modified : Andrew Pitonyak Dim vText Dim vFormats, vFormat Dim vTextCursor, vViewCursor Dim iMax As Integer, i As Integer Dim s$ Dim PrevChaine$, Chaine$ Dim aLocale as new com.sun.star.lang.Locale vFormats = ThisComponent.getNumberFormats() RunSimpleObjectBrowser(vFormats) vText = ThisComponent.Text vViewCursor = ThisComponent.CurrentController.getViewCursor() vTextCursor = vText.createTextCursorByRange(vViewCursor.getStart()) Dim v v = vFormats.queryKeys(com.sun.star.util.NumberFormat.ALL, aLocale, False) For i = LBound(v) To UBound(v) vFormat=vFormats.getbykey(v(i)) chaine=VFormat.FormatString If Chaine<>Prevchaine Then PrevChaine=Chaine chaine=CStr(v(i)) & CHR$(9) & CHR$(9) & chaine & CHR$(10) vText.insertString(vTextCursor, Chaine, FALSE) End If Next MsgBox "Finished" End Sub
Modified to match new snippet-DTD