On Wed, Jan 09, 2013 at 11:59:09AM -0300, Ariel Constenla-Haile wrote:
> Hi *
> 
> On Wed, Jan 9, 2013 at 11:40 AM, Jürgen Schmidt <jogischm...@gmail.com> wrote:
> > On 12/19/12 3:04 PM, Robert Barbey wrote:
> >> Hi everyone,
> >>
> >> is it possible to get the kind of document currently opened in Writer via 
> >> the API? Ideally, this would be a MIME type kind of thing.
> >>
> >
> > sorry for answering so late but I forget it and stumbled over this now
> > again. But the bad news is that I don't know it for sure but it seems
> > that it not possible.
> 
> The model has a getArgs() method
> http://www.openoffice.org/api/docs/common/ref/com/sun/star/document/TypeDetection.html
> 
> that returns a MediaDescriptor, look for "FilterName" in that array
> http://www.openoffice.org/api/docs/common/ref/com/sun/star/document/MediaDescriptor.html#FilterName

Things are more complicated, that documentation should direct to the
FilterFactory server, not the TypeDetection.


> then use the TypeDetection to get the filter information by name, and
> search for "MediaType"
> http://www.openoffice.org/api/docs/common/ref/com/sun/star/document/TypeDetection.html

Use the "FilterName" found in getArgs() to get the filter by name in the
FilterFactory service.

From the filter, get the "Type" property, and use it to get the type by
name in the TypeDetection service.

From the type, get the "MediaType" property. The previous version of the
marco didn't work with a .doc, the following should:



REM  *****  BASIC  *****
Option Explicit

Sub Main
    Dim oDoc as Object
    oDoc = ThisComponent

    Dim aArgs()
    aArgs = oDoc.getArgs()

    Dim sFilterName$
    SearchPropArray( aArgs, "FilterName", sFilterName)

    Dim sMime$
    GetMimeFromFilterName( sFilterName, sMime)
End Sub


Sub GetMimeFromFilterName(ByVal sFilterName$, sMime$)
    Dim oTypeDetection as Object
    Dim oFilterFactory as Object
    Dim aFilter(), aType()
    Dim sType$
    Dim i%

    oTypeDetection = CreateUnoService(_
        "com.sun.star.document.TypeDetection")
    oFilterFactory = CreateUnoService(_
        "com.sun.star.document.FilterFactory")

    If NOT oFilterFactory.hasByName(sFilterName) Then
        Exit Sub
    End If

    aFilter = oFilterFactory.getByName(sFilterName)
    SearchPropArray(aFilter, "Type", sType)


    If NOT oTypeDetection.hasByName(sType) Then
        Exit Sub
    End If

    aType = oTypeDetection.getByName(sType)
    SearchPropArray(aType, "MediaType", sMime)

    MsgBox sMime
End Sub


Sub SearchPropArray(aArray, sName$, aValue)
    Dim i%
    For i = 0 To UBound(aArray)
        If aArray(i).Name = sName Then
            aValue = aArray(i).Value
            Exit For
        End If
    Next
End Sub

-- 
Ariel Constenla-Haile
La Plata, Argentina

Attachment: pgphWvk6xcr05.pgp
Description: PGP signature

Reply via email to