Currently open documents

From Apache OpenOffice Wiki
Jump to: navigation, search

The following function returns an array of the currently open documents. It calls fnWhichComponent which is available from Currently active document.

function fnCurrentDocuments(optional sType as string)
dim oEnum as object, oPosDoc as object, i as integer
dim oListboxDocs as object
 
oEnum = StarDesktop.getComponents.createEnumeration
 
'Count windows
i = -1
while oEnum.hasMoreElements
        oPosDoc = oEnum.nextElement
        ' The check for interface XserviceInfo necessary as some windows, e.g. Help
        ' don't have the interface and need to be excluded anyway.
        If HasUnoInterfaces(oPosDoc, "com.sun.star.lang.XServiceInfo") Then
                if isMissing(sType) then
                        i = i + 1
                elseif  fnWhichComponent(oPosDoc) = Stype then
                        i = i + 1
                end if
        end if
wend
 
if i = -1 then
        'no documents of specified type currently open
        exit function
end if
 
dim mDocs(i)
 
i = -1
oEnum = StarDesktop.getComponents.createEnumeration
while oEnum.hasMoreElements
        oPosDoc = oEnum.nextElement
        If HasUnoInterfaces( oPosDoc, "com.sun.star.lang.XServiceInfo") Then 
                If isMissing(sType) then
                        i = i + 1
                        mDocs(i) = oPosDoc
                elseif fnWhichComponent(oPosDoc) = Stype then
                        i = i + 1
                        mDocs(i) = oPosDoc
                end if
        end if
wend
fnCurrentDocuments = mDocs()
end function

An example of calling the above function. This function returns an array of the titles for the currently open documents.

function fnCurrentDocTitles(optional sType as string)
if isMissing(sType) then
        mDocs = fnCurrentDocuments()
else
        mDocs = fnCurrentDocuments(SType)
end if
 
if isEmpty(mDocs) then
        exit function
end if
 
nDocs = uBound(mDocs)
 
dim mTitles(nDocs)
 
for i = 0 to nDocs
        mTitles(i) = mDocs(i).getCurrentController.getFrame.getPropertyValue("Title")
next
fnCurrentDocTitles = mTitles()
End function

An example of displaying the titles of the currently open documents:

sText = "Text"
mCurTitles = fnCurrentDocTitles(sText)
if not isEmpty(mCurTitles) then
        msgbox join(mCurTitles, chr(10)), 0, "Currently open " & sText & "documents."
else
        msgbox "No Text documents currently open."
end if
Personal tools