Difference between revisions of "Currently open documents"

From Apache OpenOffice Wiki
Jump to: navigation, search
Line 84: Line 84:
  
 
[[Category:Basic:Tutorials]]
 
[[Category:Basic:Tutorials]]
[[Category:Tutorials]]
 

Revision as of 17:52, 16 May 2006

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

[oobas] 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.

[oobas] 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:

[oobas] 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