Proper way to find an existing number format style.

Andrew Douglas Pitonyak <[email protected]> Tue, 31 May 2011 01:34:57 -0400
Newsgroups gmane.comp.openoffice.devel.api
Message-ID <[email protected]>
(Peter, please test these, but my primary question is directed at the=20
dev mailing list.....)

Tested on 3.3.0 en-US using the 64-bit Linux version on Fedora 15.

I use the following macro to find an existing number format, and I=20
create it if it does not exist. The problem is that=20
oDoc.getNumberFormats().queryKey(sFormat, aLocale, True) fails to find=20
user created numeric formats, it only returns a value for for formats=20
strings contained in the document.

Function FindCreateNumberFormatStyle (sFormat As String, Optional doc,=20
Optional locale)
   Dim oDoc As Object
   Dim aLocale As New com.sun.star.lang.Locale
   Dim oFormats As Object
   Dim formatNum As Integer
   oDoc =3D IIf(IsMissing(doc), ThisComponent, doc)
   oFormats =3D oDoc.getNumberFormats()
   If ( Not IsMissing(locale)) Then
     aLocale =3D locale
   End If
   formatNum =3D oFormats.queryKey (sFormat, aLocale, True)
   MsgBox "Current Format number for " & sFormat & " is " & formatNum
   'If the number format does not exist then add it
   If (formatNum =3D -1) Then
     formatNum =3D oFormats.addNew(sFormat, aLocale)
     If (formatNum =3D -1) Then formatNum =3D 0
     MsgBox "New Format number for " & sFormat & " is " & formatNum
   End If
   FindCreateNumberFormatStyle =3D formatNum
End Function

So, here is my test macro. The first time, the macro runs fine. The=20
second time, the macro fails.

Sub TestFormat
   Dim dummy1$
   Dim i As Integer
   Dim n As Long
   Dim formats()
   formats() =3D Array("0", "#,##0", "$#,##0.--;[RED]-$#,##0.--",=20
"##0,0#\h", "##0,0#\=C2=B0C", "#0,0#\W\h", "##0,0#\k\W\h")
   For i =3D LBound(formats()) To UBound(formats())
     n =3D FindCreateNumberFormatStyle (formats(i))
   Next
End Sub

For certain the macro adds the formats that it fails to find. This macro=20
lists all the formats in the document.

Sub ListFormatsInCurrentDocument()
   Dim oDoc           ' Document created to hold the format strings.
   Dim oFormats       ' Formats in the current document.
   Dim oFormat        ' Current format object.
   Dim oData          ' Keys queried from the formats.
   Dim i%             ' General index variable.
   Dim sFormat$       ' Current format string.
   Dim sPrevFormat$   ' Previous format string.
   Dim aLocale as new com.sun.star.lang.Locale

   oFormats =3D ThisComponent.getNumberFormats()

   ' Create an output document.
   oDoc =3D StarDesktop.loadComponentFromURL( "private:factory/swriter",=20
"_blank", 0, Array() )
   oData =3D oFormats.queryKeys(com.sun.star.util.NumberFormat.ALL,=20
aLocale, False)
   For i =3D LBound(oData) To UBound(oData)
     oFormat=3DoFormats.getbykey(oData(i))
     sFormat=3DoFormat.FormatString
     If sFormat<>sPrevFormat Then
       sPrevFormat=3DsFormat
       oDoc.getText().insertString(oDoc.getText().End, _
                          CStr(oData(i)) & CHR$(9) & sFormat & CHR$(10),=20
False)
     End If
   Next
End Sub


Certainly I can first use the quick check and if that fails, then troll=20
the entire list before creating a format, but that just feels silly. Off=20
hand, I do believe that this macro used to work as expected.

--=20
-----------------------------------------------------------------
To unsubscribe send email to [email protected]
For additional commands send email to [email protected]
with Subject: help