Difference between revisions of "Currently active document"

From Apache OpenOffice Wiki
Jump to: navigation, search
m
 
(6 intermediate revisions by 4 users not shown)
Line 1: Line 1:
'''The currently active document can be accessed using thisComponent.'''  
+
'''The currently active document can be accessed using <tt>thisComponent</tt>.'''  
  
An almost equivalent to the BASIC command thisComponent is:  
+
An almost equivalent to the BASIC command <tt>thisComponent</tt> is:  
  
<code>[oobas]
+
<syntaxhighlight lang="oobas">
 
  StarDesktop.CurrentComponent
 
  StarDesktop.CurrentComponent
</code>
+
</syntaxhighlight>
In OpenOffice.org basic it is generally preferable to use <tt>thisComponent</tt>.  
+
In {{AOo}} Basic it is generally preferable to use <tt>thisComponent</tt>.  
  
 
By using <tt>StarDesktop.CurrentComponent</tt>, a macro in one document can be run on another document.  
 
By using <tt>StarDesktop.CurrentComponent</tt>, 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 <tt>StarDesktop.CurrentComponent</tt> and a check on the document type. The reason for not using <tt>thisComponent</tt> is that when a library is opened the event fires and thus runs a second time on the currently active document.  
+
Another exception is when a routine is run by the {{AOo}} event "Document Open". In this case use <tt>StarDesktop.CurrentComponent</tt> and a check on the document type. The reason for not using <tt>thisComponent</tt> 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:  
 
To determine the type of document that is currently active you could use the following function:  
  
<code>[oobas]
+
<syntaxhighlight lang="oobas">
 
function fnWhichComponent(oDoc) as string
 
function fnWhichComponent(oDoc) as string
 
if HasUnoInterfaces(oDoc, "com.sun.star.lang.XServiceInfo") then  
 
if HasUnoInterfaces(oDoc, "com.sun.star.lang.XServiceInfo") then  
Line 32: Line 32:
 
end if
 
end if
 
End function
 
End function
</code>
+
</syntaxhighlight>
 
   
 
   
 
 
An example of calling this function is:  
 
An example of calling this function is:  
  
<code>[oobas]
+
<syntaxhighlight lang="oobas">
 
if fnWhichComponent(thisComponent) <> "Text" then
 
if fnWhichComponent(thisComponent) <> "Text" then
 
   msgbox "Sorry - this only works for text documents", 16, "Error"
 
   msgbox "Sorry - this only works for text documents", 16, "Error"
 
   exit sub
 
   exit sub
 
end if
 
end if
</code>
+
</syntaxhighlight>
  
 
[[Category:Basic:Tutorials]]
 
[[Category:Basic:Tutorials]]

Latest revision as of 12:49, 18 February 2021

The currently active document can be accessed using thisComponent.

An almost equivalent to the BASIC command thisComponent is:

 StarDesktop.CurrentComponent

In Apache OpenOffice 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 Apache OpenOffice 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:

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:

if fnWhichComponent(thisComponent) <> "Text" then
  msgbox "Sorry - this only works for text documents", 16, "Error"
  exit sub
end if
Personal tools