<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.openoffice.org/w/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Hzpfly</id>
	<title>Apache OpenOffice Wiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.openoffice.org/w/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Hzpfly"/>
	<link rel="alternate" type="text/html" href="https://wiki.openoffice.org/wiki/Special:Contributions/Hzpfly"/>
	<updated>2026-11-07T09:16:58Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.35.14</generator>
	<entry>
		<id>https://wiki.openoffice.org/w/index.php?title=Extensions_development_basic&amp;diff=163953</id>
		<title>Extensions development basic</title>
		<link rel="alternate" type="text/html" href="https://wiki.openoffice.org/w/index.php?title=Extensions_development_basic&amp;diff=163953"/>
		<updated>2010-04-13T06:16:09Z</updated>

		<summary type="html">&lt;p&gt;Hzpfly: Code Error&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;#039;&amp;#039;&amp;#039;Getting started tutorial&amp;#039;&amp;#039;&amp;#039; &lt;br /&gt;
&lt;br /&gt;
This page provides a getting started guide for writing macros in OpenOffice.org Basic. It assumes some prior programming knowledge. Please edit this page to make it more readable. I hope you will find it useful.&lt;br /&gt;
&lt;br /&gt;
If you already know the basics then you may want the [[CookBook]] for wrappers and examples.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Where do I write the code?==&lt;br /&gt;
OpenOffice.org Basic code is stored in modules within libraries. A library can be: &lt;br /&gt;
&lt;br /&gt;
*Shared (for a network install - OOo Macros &amp;amp; Dialogs) &lt;br /&gt;
*Just for the current user (My Macros &amp;amp; Dialogs) &lt;br /&gt;
*Within a document or template, so that its code is only available when that document is open.&lt;br /&gt;
&lt;br /&gt;
Libraries stored in a document can easily be copied, transported and distributed with the document.&lt;br /&gt;
&lt;br /&gt;
Libraries not stored in a document or template (that is libraries that are shared or for the current user) are referred to as &amp;#039;&amp;#039;&amp;#039;OpenOffice.org libraries&amp;#039;&amp;#039;&amp;#039;. &lt;br /&gt;
&lt;br /&gt;
To determine the actual folders that contain the OpenOffice.org libraries see:&amp;lt;br&amp;gt; &amp;#039;&amp;#039;&amp;#039;Tools &amp;gt; Options… &amp;gt; OpenOffice.org &amp;gt; Paths &amp;gt; BASIC&amp;#039;&amp;#039;&amp;#039;. &lt;br /&gt;
&lt;br /&gt;
{| border=&amp;quot;1&amp;quot;&lt;br /&gt;
|&amp;#039;&amp;#039;&amp;#039;Note&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
|Do not copy or move libraries using operating system commands. Use the macro organizer or package manager instead.&lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Modules within libraries have a maximum size of 64kb. A library can contain up to 16,000 modules. &lt;br /&gt;
&lt;br /&gt;
For more information see online help for: Modules and Libraries.&lt;br /&gt;
&lt;br /&gt;
==Accessing the IDE==&lt;br /&gt;
To access the IDE for the first time: &lt;br /&gt;
&lt;br /&gt;
OpenOffice.org 1.1.x: &amp;#039;&amp;#039;&amp;#039;Tools &amp;gt; Macros &amp;gt; Macro… &amp;gt;&amp;#039;&amp;#039;&amp;#039; &lt;br /&gt;
&lt;br /&gt;
OpenOffice.org 1.9.x and above: &amp;#039;&amp;#039;&amp;#039;Tools &amp;gt; Macros &amp;gt; Organize macros &amp;gt; OpenOffice.org Basic… &amp;gt;&amp;#039;&amp;#039;&amp;#039; &lt;br /&gt;
&lt;br /&gt;
To get started we will use Module1 in the Standard library which is stored for the current user only: &lt;br /&gt;
&lt;br /&gt;
Type a name for the new macro: &amp;#039;&amp;#039;&amp;#039;HelloWorld&amp;#039;&amp;#039;&amp;#039; &lt;br /&gt;
&lt;br /&gt;
In the macro from listbox select &amp;#039;&amp;#039;&amp;#039;Standard&amp;#039;&amp;#039;&amp;#039; &lt;br /&gt;
&lt;br /&gt;
Click &amp;#039;&amp;#039;&amp;#039;New&amp;#039;&amp;#039;&amp;#039; &lt;br /&gt;
&lt;br /&gt;
You should now see something like: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;oobas&amp;quot;&amp;gt;&lt;br /&gt;
REM  *****  BASIC  *****&lt;br /&gt;
 &lt;br /&gt;
Sub Main&lt;br /&gt;
 &lt;br /&gt;
End Sub&lt;br /&gt;
 &lt;br /&gt;
Sub HelloWorld&lt;br /&gt;
 &lt;br /&gt;
End Sub&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
The cursor will be positioned at the start of the &amp;lt;tt&amp;gt;Sub HelloWorld&amp;lt;/tt&amp;gt; line. &lt;br /&gt;
&lt;br /&gt;
{| border=&amp;quot;1&amp;quot;&lt;br /&gt;
|&amp;#039;&amp;#039;&amp;#039;Note&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
|Now that while the IDE is running it can be accessed from the OpenOffice.org Window menu, or the task panel provided by the operating system.&lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Entering the code==&lt;br /&gt;
Select and delete: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;oobas&amp;quot;&amp;gt;&lt;br /&gt;
REM  *****  BASIC  *****&lt;br /&gt;
 &lt;br /&gt;
Sub Main&lt;br /&gt;
 &lt;br /&gt;
End Sub&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Below the line &amp;quot;Sub HelloWorld&amp;quot; type &amp;lt;tt&amp;gt;msgbox &amp;quot;Hello World!&amp;quot;&amp;lt;/tt&amp;gt;, so that the editor looks like: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;oobas&amp;quot;&amp;gt;&lt;br /&gt;
Sub HelloWorld&lt;br /&gt;
  msgbox &amp;quot;Hello World!&amp;quot;&lt;br /&gt;
End Sub&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
{| border=&amp;quot;1&amp;quot;&lt;br /&gt;
|&amp;#039;&amp;#039;&amp;#039;Notes:&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
|The IDE does not provide code completion or help with command syntax as you type, but to get help on a Basic command position the cursor in the command and press F1.&amp;lt;br&amp;gt; OpenOffice.org Basic commands are not case sensitive. Thus msgbox, MSGBOX and Msgbox will all work.&amp;lt;br&amp;gt; Strings are enclosed in double quotes.&lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Running the code==&lt;br /&gt;
There are several ways of running the Basic code, these include: &lt;br /&gt;
&lt;br /&gt;
*Directly from the IDE. There is a run button on the macro bar (by default the third control on the second toolbar). This will run the first macro in the current module. &lt;br /&gt;
&lt;br /&gt;
*From the tools menu:&lt;br /&gt;
&lt;br /&gt;
**(Version 1.1.x) &amp;#039;&amp;#039;&amp;#039;Tools &amp;gt; Macros &amp;gt; Macro…&amp;#039;&amp;#039;&amp;#039;;&lt;br /&gt;
&lt;br /&gt;
**(Version 1.9.x and above) &amp;#039;&amp;#039;&amp;#039;Tools &amp;gt; Macro &amp;gt; Run Macro…&amp;#039;&amp;#039;&amp;#039; &lt;br /&gt;
&lt;br /&gt;
*Assigning the macro to a [[ key press]]. &lt;br /&gt;
&lt;br /&gt;
*Assigning the macro to a [[ menu entry]]. &lt;br /&gt;
&lt;br /&gt;
*Assigning the macro to a toolbar button. &lt;br /&gt;
&lt;br /&gt;
*Creating a [[ control in a document]]. &lt;br /&gt;
&lt;br /&gt;
*Assigning the macro to an event. &lt;br /&gt;
&lt;br /&gt;
For now try running the &amp;quot;HelloWorld&amp;quot; subroutine by clicking the &amp;#039;&amp;#039;&amp;#039;Run&amp;#039;&amp;#039;&amp;#039; button. A small dialog titled &amp;quot;soffice&amp;quot; with the text &amp;quot;Hello World!&amp;quot; and an OK button should be displayed.&lt;br /&gt;
&lt;br /&gt;
==Saving the code==&lt;br /&gt;
The code gets automatically saved whenever the container for the code gets saved. Thus, if the code is in an OpenOffice.org library (shared or users) then it gets automatically saved when OpenOffice.org exits. If the code is in a library which is part of a document it gets saved whenever the document is saved. &lt;br /&gt;
&lt;br /&gt;
On the Standard toolbar (by default the top toolbar) in the IDE there is a save button. If the code is in a document or template then clicking this button saves the entire document or template. If the code is in an OpenOffice.org library then just the current library gets saved. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Variables==&lt;br /&gt;
It is possible to force variable declaration with &amp;lt;tt&amp;gt;Option Explicit&amp;lt;/tt&amp;gt; at the top of the module. For a discussion on whether to declare variables or not, see: [[http://www.oooforum.org/forum/viewtopic.phtml?t=5845 this discussion]]. &lt;br /&gt;
&lt;br /&gt;
In that discussion the initial author of this wiki thought that variables should always be declared. Since then he has changed to not declaring them. In short it is generally a personal preference. In either case, it is the initial authors preference that variables be named according to the following convention, which is used in the examples in this Wiki: &lt;br /&gt;
&lt;br /&gt;
The first letter of the variable name indicates the type of value that the variable is going to hold, as per the following table (based on a table in Tutorial.pdf by Sun) &lt;br /&gt;
&lt;br /&gt;
{| border=&amp;quot;1&amp;quot;&lt;br /&gt;
|&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;Letter&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
|&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;Meaning&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
|-&lt;br /&gt;
|a&lt;br /&gt;
|Structure&lt;br /&gt;
|-&lt;br /&gt;
|b&lt;br /&gt;
|Boolean (TRUE or FALSE)&lt;br /&gt;
|-&lt;br /&gt;
|e&lt;br /&gt;
|Enumeration. This variable can only have one of a limited set of values.&lt;br /&gt;
|-&lt;br /&gt;
|f&lt;br /&gt;
|Float (3.402823 x 1038 to 1.401298 x 10-45. A single variable can take up to four bytes)&amp;lt;br&amp;gt; Double (1.79769313486232 x 10308 to 4.94065645841247 x 10-324. A double variable can take up to eight bytes)&amp;lt;br&amp;gt; Currency (-922337203685477.5808 to +922337203685477.5807 and takes up to eight bytes of memory)&lt;br /&gt;
|-&lt;br /&gt;
|m&lt;br /&gt;
|Array (aka sequence aka matrix)&lt;br /&gt;
|-&lt;br /&gt;
|n&lt;br /&gt;
|Integer (-32768 to 32767.) or&amp;lt;br&amp;gt; Long (-2147483648 and 2147483647).&lt;br /&gt;
|-&lt;br /&gt;
|o&lt;br /&gt;
|Object, service, or interface&lt;br /&gt;
|-&lt;br /&gt;
|s&lt;br /&gt;
|String (A string variable can store up to 65535 Unicode characters).&lt;br /&gt;
|-&lt;br /&gt;
|x&lt;br /&gt;
|Interface, to indicate that only operations of a particular interface of an object are used&lt;br /&gt;
|-&lt;br /&gt;
|v&lt;br /&gt;
|Variant, Any&lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Use long descriptive variable names making use of camel case: &amp;lt;tt&amp;gt;nMsgBoxReturn&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{| border=&amp;quot;1&amp;quot;&lt;br /&gt;
|&amp;#039;&amp;#039;&amp;#039;Note:&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
|User-defined OpenOffice.org Basic variables are not case sensitive. But UNO-API constants &amp;#039;&amp;#039;are&amp;#039;&amp;#039; case sensitive.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Convention exceptions to long descriptive names are for index variables where &amp;lt;tt&amp;gt;i, j,&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;k&amp;lt;/tt&amp;gt; are commonly used, and for when a string is being built-up, where &amp;lt;tt&amp;gt;s&amp;lt;/tt&amp;gt; is commonly used. &lt;br /&gt;
&lt;br /&gt;
Edit sub HelloWorld so that it looks like the following and run it: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;oobas&amp;quot;&amp;gt;&lt;br /&gt;
sub HelloWorld&lt;br /&gt;
dim i as integer &amp;#039;This line is optional&lt;br /&gt;
  for i = 0 to 2&lt;br /&gt;
    &amp;#039;These lines are indented for ease of reading only.&lt;br /&gt;
    &amp;#039;All your code should be like this for long-term survival.&lt;br /&gt;
    msgbox &amp;quot;Hello World &amp;quot; &amp;amp; i&lt;br /&gt;
  next i&lt;br /&gt;
end sub&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
For more information on variables see the online help for &amp;quot;using variables&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
==Understanding the OpenOffice.org API==&lt;br /&gt;
This section will start with an example. The remainder of this section aims to give information so that the example can be understood and expanded upon. &lt;br /&gt;
&lt;br /&gt;
Try running the following code with different types of documents being active. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;oobas&amp;quot;&amp;gt;&lt;br /&gt;
sub main&lt;br /&gt;
&amp;#039;BasicLibraries.loadLibrary(&amp;quot;XrayTool&amp;quot;)&lt;br /&gt;
&amp;#039;xray thisComponent&lt;br /&gt;
msgbox fnWhichComponent(thisComponent)&lt;br /&gt;
end sub&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
function fnWhichComponent(oDoc) as string&lt;br /&gt;
if HasUnoInterfaces(oDoc, &amp;quot;com.sun.star.lang.XServiceInfo&amp;quot;) then &lt;br /&gt;
   if oDoc.supportsService (&amp;quot;com.sun.star.text.GenericTextDocument&amp;quot;) then&lt;br /&gt;
      fnWhichComponent = &amp;quot;Text&amp;quot;&lt;br /&gt;
   elseif oDoc.supportsService(&amp;quot;com.sun.star.sheet.SpreadsheetDocument&amp;quot;) then&lt;br /&gt;
      fnWhichComponent = &amp;quot;Spreadsheet&amp;quot;&lt;br /&gt;
   elseif oDoc.supportsService(&amp;quot;com.sun.star.presentation.PresentationDocument&amp;quot;) then&lt;br /&gt;
      fnWhichComponent = &amp;quot;Presentation&amp;quot;&lt;br /&gt;
   elseif oDoc.supportsService(&amp;quot;com.sun.star.drawing.GenericDrawingDocument&amp;quot;) then&lt;br /&gt;
      fnWhichComponent = &amp;quot;Drawing&amp;quot;&lt;br /&gt;
   else&lt;br /&gt;
      fnWhichComponent = &amp;quot;Oops current document something else&amp;quot;&lt;br /&gt;
   end if&lt;br /&gt;
else&lt;br /&gt;
   fnWhichComponent = &amp;quot;Not a document&amp;quot;&lt;br /&gt;
end if&lt;br /&gt;
End function&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Subroutine naming convention===&lt;br /&gt;
In the above example the user defined function has a name that starts with the letters &amp;quot;fn&amp;quot;. This is the initial author&amp;#039;s convention so that people know that this is a user defined function. Similarly, the initial author uses the convention that user defined subroutines start with the letters &amp;quot;sub&amp;quot;. When learning a language and an API it can be difficult to know what is built-in and what has been defined elsewhere. This document/wiki will use this convention for naming functions and subroutines.&lt;br /&gt;
&lt;br /&gt;
===Introducing the OpenOffice.org API===&lt;br /&gt;
This section introduces the following terms: &lt;br /&gt;
&lt;br /&gt;
* Interface &lt;br /&gt;
* Module &lt;br /&gt;
* Service &lt;br /&gt;
* Method &lt;br /&gt;
* Property &lt;br /&gt;
&lt;br /&gt;
Understanding the difference between an interface and a service and what a module is, is not essential to being able to write extensions for OpenOffice.org, but it does help interpreting the documentation, and for introspection purposes. You may need to read this section at least twice. &lt;br /&gt;
&lt;br /&gt;
An &amp;#039;&amp;#039;&amp;#039;interface&amp;#039;&amp;#039;&amp;#039; is a &amp;#039;&amp;#039;definition&amp;#039;&amp;#039; of a set of methods (and their arguments) that a service which implements that interface must have. &lt;br /&gt;
&lt;br /&gt;
Interfaces are grouped together in &amp;#039;&amp;#039;&amp;#039;modules&amp;#039;&amp;#039;&amp;#039; for naming purposes. All interfaces (and services) start with the name &amp;quot;com.sun.star&amp;quot; then the name of the module then the name of the interface (or service). &lt;br /&gt;
&lt;br /&gt;
For example most services provide the com.sun.star.beans.XPropertySet interface. This interface is stored in the module &amp;quot;beans&amp;quot; and provides access to the properties of a service. A &amp;#039;&amp;#039;&amp;#039;property&amp;#039;&amp;#039;&amp;#039; is a value whereas a &amp;#039;&amp;#039;&amp;#039;method&amp;#039;&amp;#039;&amp;#039; is an action. &lt;br /&gt;
&lt;br /&gt;
An OpenOffice.org object can have many services. &lt;br /&gt;
&lt;br /&gt;
An OpenOffice.org object may have a service, which implements an interface, in which a method description says that another OpenOffice.org object is returned.&lt;br /&gt;
&lt;br /&gt;
===Introspection===&lt;br /&gt;
&amp;lt;tt&amp;gt;HasUnoInterfaces&amp;lt;/tt&amp;gt; is an OpenOffice.org Basic function for introspection. See this [[http://www.oooforum.org/forum/viewtopic.phtml?t=7068 link]] for information on introspection in other languages. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;HasUnoInterfaces&amp;lt;/tt&amp;gt; returns true if all of the specified interfaces are available for the specified object. &lt;br /&gt;
&lt;br /&gt;
Most OpenOffice.org objects provide the method &amp;lt;tt&amp;gt;supportsService&amp;lt;/tt&amp;gt; because they have the interface &amp;lt;idl&amp;gt;com.sun.star.lang.XServiceInfo&amp;lt;/idl&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
In the above example, the OpenOffice.org Basic command &amp;lt;tt&amp;gt;HasUnoInterfaces&amp;lt;/tt&amp;gt; checks that the current document has the interface &amp;lt;idl&amp;gt;com.sun.star.lang.XServiceInfo&amp;lt;/idl&amp;gt;, because if it doesn&amp;#039;t have that interface then it doesn&amp;#039;t have the method &amp;lt;tt&amp;gt;supportsService&amp;lt;/tt&amp;gt;, and a run time error would occur if such an object tried to access its nonexistent method. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;SupportsService&amp;lt;/tt&amp;gt; is a method which returns true if the specified service is available. The above examples checks for a service to determine the type of document that is currently active.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===X-Ray tool===&lt;br /&gt;
Using &amp;lt;tt&amp;gt;HasUnoInterfaces&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;supportsService&amp;lt;/tt&amp;gt; gives information about an object at run time, but checking an object like this would be a nightmare for learning? Thankfully, Bernard Marcelly has come to our rescue with the X-Ray tool. The X-Ray tool is available from: [[http://ooomacros.org/dev.php#101416 ooomacros]]. Download the zip file, unzip the document, open the document in OpenOffice.org, follow the instructions for installation and set-up. &lt;br /&gt;
&lt;br /&gt;
Part of the X-Ray tool set-up is to specify a local copy of the OpenOffice.org SDK. Download the [http://www.openoffice.org/dev_docs/source/sdk/ OpenOffice.org SDK] and extract it. &lt;br /&gt;
&lt;br /&gt;
In the above example, at the start of the code, there are two commented lines (comments start with an apostrophe): &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;oobas&amp;quot;&amp;gt;&lt;br /&gt;
&amp;#039;BasicLibraries.loadLibrary(&amp;quot;XrayTool&amp;quot;)&lt;br /&gt;
&amp;#039;xray thisComponent&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Now that you have the X-Ray tool installed, uncomment these lines (remove the apostrophes) and rerun the macro.&lt;br /&gt;
 &lt;br /&gt;
[[image:xray_5.2_en.png]]&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;BasicLibraries&amp;#039;&amp;#039;&amp;#039; is an OpenOffice.org Basic command that returns an object for accessing the OpenOffice.org libraries. The &amp;lt;tt&amp;gt;loadLibrary&amp;lt;/tt&amp;gt; method ensures that the routines in that library are available for use. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;xray&amp;lt;/tt&amp;gt; specifies the &amp;lt;tt&amp;gt;xray&amp;lt;/tt&amp;gt; subroutine within the library &amp;lt;tt&amp;gt;XrayTool&amp;lt;/tt&amp;gt;. &amp;lt;tt&amp;gt;thisComponent&amp;lt;/tt&amp;gt; is the object that is being passed to &amp;lt;tt&amp;gt;xray&amp;lt;/tt&amp;gt; for introspection. &lt;br /&gt;
&lt;br /&gt;
To find the object that you want to work with often requires finding or creating it starting from either &amp;lt;tt&amp;gt;StarDesktop&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;thisComponent&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===Desktop, documents, and current selection===&lt;br /&gt;
&amp;lt;tt&amp;gt;StarDesktop&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;thisComponent&amp;lt;/tt&amp;gt; are OpenOffice.org Basic commands that refer to the application and currently active document respectively. &lt;br /&gt;
&lt;br /&gt;
Unlike Microsoft Office, OpenOffice.org is one application with different components. When running some code it maybe useful to check which component is currently active. The above code demonstrates how that checking process can be done. &lt;br /&gt;
&lt;br /&gt;
The Desktop that &amp;lt;tt&amp;gt;StarDesktop&amp;lt;/tt&amp;gt; refers to is conceptual (historically it actually existed) and can be thought of as the OpenOffice.org application.&lt;br /&gt;
&lt;br /&gt;
==Creating new documents==&lt;br /&gt;
To create a new text document: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;oobas&amp;quot;&amp;gt;&lt;br /&gt;
oDoc = StarDesktop.loadComponentFromURL(&amp;quot;private:factory/swriter&amp;quot;, &amp;quot;_blank&amp;quot;, 0, Array())&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
To create a new spreadsheet document: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;oobas&amp;quot;&amp;gt;&lt;br /&gt;
oDoc = StarDesktop.loadComponentFromURL(&amp;quot;private:factory/scalc&amp;quot;, &amp;quot;_blank&amp;quot;, 0, Array())&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
An easier approach would be to write a simple function: &lt;br /&gt;
&amp;lt;source lang=&amp;quot;oobas&amp;quot;&amp;gt;&lt;br /&gt;
function fnNewDoc(sDocType as string)&lt;br /&gt;
fnNewDoc = StarDesktop.loadComponentFromURL(&amp;quot;private:factory/&amp;quot; &amp;amp; sDocType , &amp;quot;_blank&amp;quot;, 0, Array())&lt;br /&gt;
end function&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Then creating new documents can be achieved with: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;oobas&amp;quot;&amp;gt;&lt;br /&gt;
oDoc = fnNewDoc(&amp;quot;swriter&amp;quot;)&lt;br /&gt;
oDoc = fnNewDoc(&amp;quot;scalc&amp;quot;)&lt;br /&gt;
oDoc = fnNewDoc(&amp;quot;simpress&amp;quot;)&lt;br /&gt;
oDoc = fnNewDoc(&amp;quot;sdraw&amp;quot;)&lt;br /&gt;
oDoc = fnNewDoc(&amp;quot;smath&amp;quot;)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
See http://api.openoffice.org/docs/common/ref/com/sun/star/frame/XComponentLoader.html .&lt;br /&gt;
&lt;br /&gt;
==To open a document==&lt;br /&gt;
The following example shows how to open a file. For information on URLs in OpenOffice.org see [[ URL Basics]]. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;oobas&amp;quot;&amp;gt;&lt;br /&gt;
sFile = &amp;quot;C:\Documents and Settings\danny\Desktop\MyCalc.sxc&amp;quot; &amp;#039; Windows&lt;br /&gt;
sFile = &amp;quot;/home/danny/Desktop/MyCalc.sxc&amp;quot; &amp;#039; Linux&lt;br /&gt;
sURL = ConvertToURL(sFile)&lt;br /&gt;
oDoc = StarDesktop.loadComponentFromURL(sURL, &amp;quot;_blank&amp;quot;, 0, Array())&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Again it may make sense to make this easier by writing a simple function: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;oobas&amp;quot;&amp;gt;&lt;br /&gt;
function fnOpenDoc(sFile)&lt;br /&gt;
sURL = ConvertToURL(sFile)&lt;br /&gt;
fnOpenDoc = StarDesktop.loadComponentFromURL(sURL, &amp;quot;_blank&amp;quot;, 0, Array())&lt;br /&gt;
end function&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Examples of calling the function: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;oobas&amp;quot;&amp;gt;&lt;br /&gt;
oDoc = fnOpenDoc(&amp;quot;C:\Documents and Settings\danny\Desktop\MyCalc.sxc&amp;quot;) &amp;#039; Windows&lt;br /&gt;
oDoc = fnOpenDoc(&amp;quot;/home/danny/Desktop/MyCalc.sxc&amp;quot;) &amp;#039; Linux&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Current Selection==&lt;br /&gt;
It is common to want to run some code that affects the current selection. &amp;lt;tt&amp;gt;ThisComponent&amp;lt;/tt&amp;gt; has the method &amp;lt;tt&amp;gt;getCurrentSelection&amp;lt;/tt&amp;gt;. Since many different types of objects could possibly be selected it is common to check that the currently selected object has the service that contains the method that we want to apply to the object. &lt;br /&gt;
&lt;br /&gt;
Edit the main subroutine to the following and rerun it on a text document with different selections. (To select more than one block of text hold down the control key). &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;oobas&amp;quot;&amp;gt;&lt;br /&gt;
sub main&lt;br /&gt;
BasicLibraries.loadLibrary(&amp;quot;XrayTool&amp;quot;)&lt;br /&gt;
if fnWhichComponent(thisComponent) = &amp;quot;Text&amp;quot; then&lt;br /&gt;
        oCurSelection = thisComponent.getCurrentSelection()&lt;br /&gt;
        &amp;#039;xray oCurSelection&lt;br /&gt;
        if oCurSelection.supportsService(&amp;quot;com.sun.star.text.TextRanges&amp;quot;) then&lt;br /&gt;
                msgbox &amp;quot;There are &amp;quot; &amp;amp; oCurSelection.getCount() &amp;amp; _&lt;br /&gt;
                 &amp;quot; selections in the current text document.&amp;quot;&lt;br /&gt;
        end if&lt;br /&gt;
end if&lt;br /&gt;
end sub&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
With nothing selected the number of selections is one - the insertion point, with one block of text selected the count is still one, but with two blocks of text the count is three - the insertion point and the two blocks of selected text. &lt;br /&gt;
&lt;br /&gt;
Exercise 1: Modify the above code so that it works on selected cell ranges in a spreadsheet. &lt;br /&gt;
&lt;br /&gt;
Question1: For two blocks of cells selected, what would be the count for the number of selections?&lt;br /&gt;
&lt;br /&gt;
==Properties==&lt;br /&gt;
Uncomment &amp;lt;tt&amp;gt;&amp;#039;xray oCurSelection&amp;lt;/tt&amp;gt; so that xray runs, to see that the object that &amp;lt;tt&amp;gt;oCurSelection&amp;lt;/tt&amp;gt; points to has a &amp;quot;property&amp;quot; called &amp;lt;tt&amp;gt;Count&amp;lt;/tt&amp;gt; with a description of &amp;quot;pseudo-prop, read only&amp;quot;. It is possible in OpenOffice.org Basic to write &amp;lt;tt&amp;gt;oCurSelection.count&amp;lt;/tt&amp;gt;, but as this is not possible in all other languages accessing the OpenOffice.org API, this Wiki will try to always use the method approach. (I say try because I have not been in the habit of doing this and sometimes I may forget). &lt;br /&gt;
&lt;br /&gt;
This next example demonstrates changing a property value for the current selections. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;oobas&amp;quot;&amp;gt;&lt;br /&gt;
sub main&lt;br /&gt;
BasicLibraries.loadLibrary(&amp;quot;XrayTool&amp;quot;)&lt;br /&gt;
if fnWhichComponent(thisComponent) = &amp;quot;Text&amp;quot; then&lt;br /&gt;
   oCurSelection = thisComponent.getCurrentSelection()&lt;br /&gt;
   if oCurSelection.supportsService(&amp;quot;com.sun.star.text.TextRanges&amp;quot;) then&lt;br /&gt;
      nCount = oCurSelection.Count&lt;br /&gt;
      &amp;#039;xray oCurSelection.getByIndex(0)&lt;br /&gt;
      &amp;#039;Warning: The insertion point will have the same action applied twice&lt;br /&gt;
      &amp;#039;in this case it doesn&amp;#039;t matter, but in others it might.&lt;br /&gt;
      for i = 0 to nCount - 1&lt;br /&gt;
         oCurSelection.getByIndex(i).setPropertyValue(&amp;quot;CharStyleName&amp;quot;, &amp;quot;Strong Emphasis&amp;quot;)&lt;br /&gt;
      next&lt;br /&gt;
   end if&lt;br /&gt;
end if&lt;br /&gt;
end sub&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
In OpenOffice.org Basic it is possible to shorten the assignment line to: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;oobas&amp;quot;&amp;gt;&lt;br /&gt;
oCurSelection(i).CharStyleName = &amp;quot;Strong Emphasis&amp;quot;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This wiki will try to use the full methods for both indexing and assigning properties. The rationale is that this makes converting the code to other languages easier and also helps the learner to understand what is happening (again I have not been in the habit of doing this so some examples may slip past me). &lt;br /&gt;
&lt;br /&gt;
Exercise 2: Rewrite the above code so that the warning can be removed. &lt;br /&gt;
&lt;br /&gt;
See [[ Current selection]].&lt;br /&gt;
&lt;br /&gt;
==Iterative Access to Subordinate Objects (Enumeration access)==&lt;br /&gt;
Sometimes to access the desired object an enumeration is required. For example paragraphs, within a document or within a selection. &lt;br /&gt;
&lt;br /&gt;
When a Writer document is active and some text is selected, both &amp;lt;tt&amp;gt;thisDocument.getText()&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;thisComponent.getCurrentSelection().getByIndex(i)&amp;lt;/tt&amp;gt; have the service: &amp;lt;idl&amp;gt;com.sun.star.text.TextRange&amp;lt;/idl&amp;gt;, which has the interface: &amp;lt;idl&amp;gt;com.sun.star.container.XContentEnumerationAccess&amp;lt;/idl&amp;gt;. It is possible to create an enumeration of the paragraphs for the current document or for a particular selection. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;oobas&amp;quot;&amp;gt;&lt;br /&gt;
&amp;#039; Create enumeration object&lt;br /&gt;
oTextElementEnum = thisComponent.getText().createEnumeration()&lt;br /&gt;
&amp;#039;or thisComponent.getCurrentSelection().getByIndex(i).createEnumeration()&lt;br /&gt;
 &lt;br /&gt;
&amp;#039; loop over all text elements&lt;br /&gt;
while oTextElementEnum.hasMoreElements()&lt;br /&gt;
        oTextElement = oTextElementEnum.nextElement&lt;br /&gt;
        if oTextElement.supportsService(&amp;quot;com.sun.star.text.TextTable&amp;quot;) then&lt;br /&gt;
                MsgBox &amp;quot;The current block contains a table.&amp;quot;&lt;br /&gt;
        end if&lt;br /&gt;
        if oTextElement.supportsService(&amp;quot;com.sun.star.text.Paragraph&amp;quot;) then&lt;br /&gt;
                MsgBox &amp;quot;The current block contains a paragraph.&amp;quot;&lt;br /&gt;
        end if&lt;br /&gt;
wend&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Exercise 3: Extend the above example to display in a message box all text portions that are bold.&lt;br /&gt;
&lt;br /&gt;
==Named access==&lt;br /&gt;
Some objects provide named access to a particular type of subordinate object, some others indexed access, and some both named and indexed access. &lt;br /&gt;
&lt;br /&gt;
For example if the current document in OpenOffice.org is a spreadsheet then to access a particular sheet can be done by index access: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;oobas&amp;quot;&amp;gt;&lt;br /&gt;
oSheet = thisComponent.getSheets.getByIndex(0)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
or named access: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;oobas&amp;quot;&amp;gt;&lt;br /&gt;
oSheet = thisComponent.getSheets.getByName(&amp;quot;Sheet1&amp;quot;)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
To check if an object with a particular name already exists use &amp;lt;tt&amp;gt;hasByName&amp;lt;/tt&amp;gt;, for example: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;oobas&amp;quot;&amp;gt;&lt;br /&gt;
if thisComponent.getSheets.hasByName(&amp;quot;Sheet1&amp;quot;) then&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
To loop through all the available object names can be done like: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;oobas&amp;quot;&amp;gt;&lt;br /&gt;
mNames = thisComponent.getSheets.getElementnames&lt;br /&gt;
for i = lbound(mNames) to ubound(mNames)&lt;br /&gt;
        msgbox mNames(i)&lt;br /&gt;
next&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Some named subordinate objects also provide the interface:  &amp;lt;idl&amp;gt;com.sun.star.container.XNameContainer&amp;lt;/idl&amp;gt;. This interface defines that such objects should have the following methods: &amp;lt;tt&amp;gt;insertByName&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;insertNewByName&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;replaceByname&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;removeByName&amp;lt;/tt&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
E.g. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;oobas&amp;quot;&amp;gt;&lt;br /&gt;
thisComponent.getSheets.insertNewByName(&amp;quot;NewSheet&amp;quot;, 0)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Create new objects==&lt;br /&gt;
Some objects have services which implement interfaces to provide specific methods for creating a particular type of object. &lt;br /&gt;
&lt;br /&gt;
For example if the current document is a Writer document then &amp;lt;tt&amp;gt;thisComponent.getText&amp;lt;/tt&amp;gt; is an object that provides the service &amp;lt;idl&amp;gt;com.sun.star.text.Text&amp;lt;/idl&amp;gt;, which implements the interface &amp;lt;idl&amp;gt;com.sun.star.text.XSimpleText&amp;lt;/idl&amp;gt;, which defines the methods &amp;lt;tt&amp;gt;createTextCursor&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;createTextCursorByRange&amp;lt;/tt&amp;gt;. Both of these methods create a text cursor for accessing the text of the document. These cursors are quite independent of the view cursor. The view cursor is visible on the screen and is manipulated by the user (and can be manipulated by program control), whereas a text cursor is not visible on the screen and is solely used by program control. The following code snippet demonstrates creating a new text cursor, such that it starts at the same location as the view cursor and is then moved independent of the view cursor. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;oobas&amp;quot;&amp;gt;&lt;br /&gt;
oVC = thisComponent.getCurrentController.getViewCursor&lt;br /&gt;
oCursor = oVC.getText.createTextCursorByRange(oVC)&lt;br /&gt;
oCursor.gotoStartOfSentence(false)&lt;br /&gt;
oCursor.gotoEndOfSentence(true)&lt;br /&gt;
msgbox oCursor.getString&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
The string returned by the &amp;lt;tt&amp;gt;oCursor.getString&amp;lt;/tt&amp;gt; method is the text of the sentence in which the view cursor currently resides.&lt;br /&gt;
&lt;br /&gt;
Some objects are context dependent and get created using the method &amp;lt;tt&amp;gt;createInstance&amp;lt;/tt&amp;gt; which is defined in the interface &amp;lt;idl&amp;gt;com.sun.star.lang.XMultiServiceFactory&amp;lt;/idl&amp;gt;. For example to add a rectangle to the first page of a drawing document: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;oobas&amp;quot;&amp;gt;&lt;br /&gt;
dim aPoint as new com.sun.star.awt.Point&lt;br /&gt;
dim aSize as new com.sun.star.awt.Size&lt;br /&gt;
 &lt;br /&gt;
aPoint.x = 1000&lt;br /&gt;
aPoint.y = 1000&lt;br /&gt;
 &lt;br /&gt;
aSize.Width = 10000&lt;br /&gt;
aSize.Height = 10000&lt;br /&gt;
 &lt;br /&gt;
oRectangleShape = thisComponent.createInstance(&amp;quot;com.sun.star.drawing.RectangleShape&amp;quot;)&lt;br /&gt;
oRectangleShape.Size = aSize&lt;br /&gt;
oRectangleShape.Position = aPoint&lt;br /&gt;
 &lt;br /&gt;
thisComponent.getDrawPages.getByIndex(0).add(oRectangleShape)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This example also uses &amp;#039;&amp;#039;UNO structs&amp;#039;&amp;#039;. See below for more information on UNO structs. &lt;br /&gt;
&lt;br /&gt;
Some objects are context independent; to create these objects, use the OpenOffice.org Basic command &amp;lt;tt&amp;gt;createUnoService&amp;lt;/tt&amp;gt;. For example, to create the equivalent to &amp;lt;tt&amp;gt;StarDesktop&amp;lt;/tt&amp;gt;: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;oobas&amp;quot;&amp;gt;&lt;br /&gt;
oDesktop = createUnoService(&amp;quot;com.sun.star.frame.Desktop&amp;quot;)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The process that I use to determine how to access or create an object is as follows: &lt;br /&gt;
&lt;br /&gt;
Does the object already exist, if so I should be able to access it from something like &amp;lt;tt&amp;gt;thisComponent&amp;lt;/tt&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
Will the new object belong to another object, if so does the owner have a specific method for creating the object, if so use it. &lt;br /&gt;
&lt;br /&gt;
The new object will belong to another object, but that object doesn&amp;#039;t provide a specific method for creating it, but does provide &amp;lt;tt&amp;gt;createInstance&amp;lt;/tt&amp;gt;. If the object doesn&amp;#039;t provide &amp;lt;tt&amp;gt;createInstance&amp;lt;/tt&amp;gt; are you sure you are using the correct object, or is it context independent. &lt;br /&gt;
&lt;br /&gt;
I have found working out how to create an object to be quite difficult with existing documentation so I hope that this document/wiki will eventually make this clear. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===UNO structs===&lt;br /&gt;
UNO structures can be declared using the OpenOffice.org Basic command &amp;lt;tt&amp;gt;dim as new&amp;lt;/tt&amp;gt;: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;oobas&amp;quot;&amp;gt;&lt;br /&gt;
dim aPoint as new com.sun.star.awt.Point&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Or by using the OpenOffice.org Basic command &amp;lt;tt&amp;gt;createUnoStruct&amp;lt;/tt&amp;gt;: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;oobas&amp;quot;&amp;gt;&lt;br /&gt;
aPoint = createUnoStruct(&amp;quot;com.sun.star.awt.Point&amp;quot;)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{| border=&amp;quot;1&amp;quot;&lt;br /&gt;
|&amp;#039;&amp;#039;&amp;#039;Note:&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
|When declaring UNO structs, case is important. Note that everything up to the name of the struct is lowercase, and that the name of the struct is in TitleCase.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Creating Listeners and Handlers===&lt;br /&gt;
Through the user interface it is possible to assign macros to some events: &lt;br /&gt;
&lt;br /&gt;
OpenOffice.org versions 1.1.x: &amp;#039;&amp;#039;&amp;#039;Tools &amp;gt; Configure… &amp;gt; Events&amp;#039;&amp;#039;&amp;#039;. &lt;br /&gt;
&lt;br /&gt;
OpenOffice.org versions 1.9.x and above: &amp;#039;&amp;#039;&amp;#039;Tools &amp;gt; Customize… &amp;gt; Events&amp;#039;&amp;#039;&amp;#039;. &lt;br /&gt;
&lt;br /&gt;
It is also possible to assign macros to a wider range of events using the OpenOffice.org Basic command &amp;lt;tt&amp;gt;CreateUnoListener&amp;lt;/tt&amp;gt;. This same command is used for creating both listeners and handlers. A listener checks for an event and always allows other listeners to respond to the event as well. A handler listens for an event, and can optionally consume the event so that other listeners don&amp;#039;t get to act on it. &lt;br /&gt;
&lt;br /&gt;
The following example creates a keyHandler: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;oobas&amp;quot;&amp;gt;&lt;br /&gt;
global IannzExampleKeyHandler&lt;br /&gt;
 &lt;br /&gt;
sub SetupKeyHandler&lt;br /&gt;
oController = thisComponent.currentController&lt;br /&gt;
IannzExampleKeyHandler = CreateUnoListener(&amp;quot;KeyHandler_&amp;quot;,&amp;quot;com.sun.star.awt.XKeyHandler&amp;quot;)&lt;br /&gt;
oController.addKeyHandler(IannzExampleKeyHandler) &amp;#039; Register the listener&lt;br /&gt;
end sub&lt;br /&gt;
&lt;br /&gt;
sub RemoveKeyHandler&lt;br /&gt;
thisComponent.currentController.removeKeyHandler(IannzExampleKeyHandler)&lt;br /&gt;
end sub&lt;br /&gt;
&lt;br /&gt;
sub KeyHandler_disposing&lt;br /&gt;
end sub&lt;br /&gt;
 &lt;br /&gt;
function KeyHandler_keyReleased(oKeyEvent as new com.sun.star.awt.KeyHandler) as boolean&lt;br /&gt;
        KeyHandler_keyReleased = False    &lt;br /&gt;
end function&lt;br /&gt;
 &lt;br /&gt;
function KeyHandler_keyPressed(oKeyEvent as new com.sun.star.awt.KeyHandler) as boolean&lt;br /&gt;
KeyHandler_keyPressed = false   &amp;#039;Let other listeners handle the event&lt;br /&gt;
if oKeyEvent.modifiers = com.sun.star.awt.KeyModifier.MOD2 then &amp;#039;Control key was pressed&lt;br /&gt;
        if oKeyEvent.keyCode = com.sun.star.awt.Key.Q then&lt;br /&gt;
                msgbox &amp;quot;Alt + Q was pressed&amp;quot;&lt;br /&gt;
                KeyHandler_keyPressed = true    &amp;#039;Don&amp;#039;t let other listeners process this event&lt;br /&gt;
        end if&lt;br /&gt;
end if&lt;br /&gt;
end function&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
A variable declared as global keeps its value even after the macro exits. In this case we want to be able to use this variable later to remove the handler. As variables declared globally could be used in other libraries, to try and avoid conflict I start all my global variables with Iannz my registered name for the OpenOffice.org web site. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;sub SetupKeyHandler&amp;lt;/tt&amp;gt; sets up the handler. The first parameter to &amp;lt;tt&amp;gt;CreateUnoListener&amp;lt;/tt&amp;gt; is the starting name for the methods that will be called when that type of event occurs: in this example, &amp;lt;tt&amp;gt;&amp;quot;KeyHandler_&amp;quot;&amp;lt;/tt&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
The second parameter is the name of the interface for the listener or handler, &amp;quot;&amp;lt;idl&amp;gt;com.sun.star.awt.XKeyHandler&amp;lt;/idl&amp;gt;&amp;quot;. The name is case sensitive, everything up to and including the module name is always lowercase, the name of the interface always starts with &amp;quot;X&amp;quot; and the remainder is in TitleCase. &lt;br /&gt;
&lt;br /&gt;
Use the SDK to find out what methods the interface must supply. You must supply routines for all of these methods even if you don&amp;#039;t intend to use them. You also need to supply a disposing method. The names of these routines start with the string given in the first parameter to &amp;lt;tt&amp;gt;CreateUnoListener&amp;lt;/tt&amp;gt;, in this example &amp;lt;tt&amp;gt;&amp;quot;KeyHandler_&amp;quot;&amp;lt;/tt&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
Thus in the example there is &amp;lt;tt&amp;gt;KeyHandler_disposing&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;KeyHandler_keyReleased&amp;lt;/tt&amp;gt; which don&amp;#039;t actually do anything but are required, and &amp;lt;tt&amp;gt;KeyHandler_keyPressed&amp;lt;/tt&amp;gt; which actually does the job. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;sub RemoveKeyHandler&amp;lt;/tt&amp;gt; demonstrates how to remove the handler.&lt;br /&gt;
&lt;br /&gt;
==OpenOffice.org constants==&lt;br /&gt;
The above example uses OpenOffice.org constants. &lt;br /&gt;
&lt;br /&gt;
E.g. &amp;lt;tt&amp;gt;com.sun.star.awt.KeyModifier.MOD2&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
OpenOffice.org constants are case sensitive. Everything up to and including the module name is always lowercase. The constant group is in TitleCase. The actual constant name is always UPPERCASE. &lt;br /&gt;
&lt;br /&gt;
Programmers not using OpenOffice.org Basic may not have access to these constants. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Using the recorder==&lt;br /&gt;
See [[ The OpenOffice.org recorder and UNO dispatch calls]] section for a discussion on recording UNO Dispatch commands versus writing API calls.&lt;br /&gt;
&lt;br /&gt;
== See also==&lt;br /&gt;
* [[CookBook|CookBook]]&lt;br /&gt;
* [[Text_cursor|Text Cursor in OOoBasic]]&lt;br /&gt;
* [[Framework/Article/Easy_To_Use_Message_Boxes|Easy To Use Message Boxes]]&lt;br /&gt;
* [[Programming_OooWriter |Programming OOoWriter in C++]] where OOoBasic records are used in C++&lt;br /&gt;
* A [[XIntrospection_Interface|XIntrospection Interface]] short description. XRay uses this interface.&lt;br /&gt;
* Download Bernard Marcelly&amp;#039;s [http://www.ooomacros.org/dev.php#101416 XRay OOoBasic tool] &lt;br /&gt;
* See [[Object Inspector|The New Object Inspector]] and [[BASIC/UNO_Object_Browser|The BASIC UNO Object Browser]]&lt;br /&gt;
&lt;br /&gt;
[[Category:Extensions]]&lt;br /&gt;
[[Category:StarBasic]]&lt;br /&gt;
[[Category:Documentation/Candidate]]&lt;/div&gt;</summary>
		<author><name>Hzpfly</name></author>
	</entry>
	<entry>
		<id>https://wiki.openoffice.org/w/index.php?title=User:Hzpfly/Books/OOoMacro&amp;diff=141385</id>
		<title>User:Hzpfly/Books/OOoMacro</title>
		<link rel="alternate" type="text/html" href="https://wiki.openoffice.org/w/index.php?title=User:Hzpfly/Books/OOoMacro&amp;diff=141385"/>
		<updated>2009-09-28T14:45:07Z</updated>

		<summary type="html">&lt;p&gt;Hzpfly: Created page with &amp;#039;{{saved_book}}  == OOoMacro == === TextCursor === :Text cursor  OOoMacro&amp;#039;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{saved_book}}&lt;br /&gt;
&lt;br /&gt;
== OOoMacro ==&lt;br /&gt;
=== TextCursor ===&lt;br /&gt;
:[[Text cursor]]&lt;br /&gt;
&lt;br /&gt;
[[Category:Books|OOoMacro]]&lt;/div&gt;</summary>
		<author><name>Hzpfly</name></author>
	</entry>
</feed>