Currently active document

From Apache OpenOffice Wiki
Revision as of 23:53, 1 May 2006 by Iannz (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

The currently active document can be accessed using thisComponent.

An almost equivalent to the BASIC command thisComponent is:

[oobas]

StarDesktop.CurrentComponent

In OpenOffice.org basic it is generally preferable to use thisComponent.

By using StarDesktop.CurrentComponent, a macro in one document can be run on another document.

Another exception is when a routine is run by the OpenOffice.org event "Document Open". In this case use StarDesktop.CurrentComponent and a check on the document type. The reason for not using thisComponent is that when a library is opened the event fires and thus runs a second time on the currently active document.

To determine the type of document that is currently active you could use the following function:

[oobas] function fnWhichComponent(oDoc) as string if HasUnoInterfaces(oDoc, "com.sun.star.lang.XServiceInfo") then

  if thisComponent.supportsService ("com.sun.star.text.GenericTextDocument") then
     fnWhichComponent = "Text"
  elseif thisComponent.supportsService("com.sun.star.sheet.SpreadsheetDocument") then
     fnWhichComponent = "Spreadsheet"
  elseif thisComponent.supportsService("com.sun.star.presentation.PresentationDocument") then
     fnWhichComponent = "Presentation"
  elseif thisComponent.supportsService("com.sun.star.drawing.GenericDrawingDocument") then
     fnWhichComponent = "Drawing"
  else
     fnWhichComponent = "Oops current document something else"
  end if

else

  fnWhichComponent = "Not a document"

end if End function


An example of calling this function is:

[oobas] if fnWhichComponent(thisComponent) <> "Text" then

 msgbox "Sorry - this only works for text documents", 16, "Error"
 exit sub

end if

Personal tools