Difference between revisions of "Documentation/OOo3 User Guides/Calc Guide/Write your own functions"

From Apache OpenOffice Wiki
Jump to: navigation, search
(New page: {{DISPLAYTITLE:Write your own functions}} {{Documentation/CG3Ch12TOC |ShowPrevNext=block |PrevPage=Documentation/OOo3_User_Guides/Calc Guide/Using the macro recorder |NextPage=Documentatio...)
 
Line 9: Line 9:
 
<ol>
 
<ol>
 
<li>Create a new Calc document named CalcTestMacros.ods.</li>
 
<li>Create a new Calc document named CalcTestMacros.ods.</li>
<li>Use '''Tools > Macros > Organize Macros > OpenOffice.org Basic''' to open the OpenOffice.org Basic Macros dialog. The ''Macro from'' box lists available macro library containers. ''My Macros'' contains macros that you write or add to OOo. ''OpenOffice.org Macros'' contains macros included with OOo and should not be changed. All other library containers are currently open OOo documents.</li>
+
<li>Use '''Tools > Macros > Organize Macros > OpenOffice.org Basic''' to open the OpenOffice.org Basic Macros dialog. The ''Macro from'' box lists available macro library containers including currently open OOo documents. ''My Macros'' contains macros that you write or add to OOo. ''OpenOffice.org Macros'' contains macros included with OOo and should not be changed. </li>
  
 
[[Image:CG3Ch12F7.png|thumb|none|500px|''OpenOffice.org Basic Macros dialog.'']]
 
[[Image:CG3Ch12F7.png|thumb|none|500px|''OpenOffice.org Basic Macros dialog.'']]
  
<li>Click '''Organizer''' to open the OpenOffice.org Basic Macro Organizer dialog.</li>
+
<li>Click '''Organizer''' to open the OpenOffice.org Basic Macro Organizer dialog. On the '''Libraries''' tab, select the document to contain the macro.</li>
  
 
[[Image:CG3Ch12F8.png|thumb|none|500px|''OpenOffice.org Basic Macro Organizer.'']]
 
[[Image:CG3Ch12F8.png|thumb|none|500px|''OpenOffice.org Basic Macro Organizer.'']]
  
<li>Click the '''Libraries''' tab.</li>
 
<li>Select the document to contain the macro.</li>
 
 
<li>Click '''New''' to open the New Library dialog.</li>
 
<li>Click '''New''' to open the New Library dialog.</li>
  
Line 31: Line 29:
 
[[Image:CG3Ch12F11.png|thumb|none|500px|''Basic Integrated Development Environment (IDE).'']]
 
[[Image:CG3Ch12F11.png|thumb|none|500px|''Basic Integrated Development Environment (IDE).'']]
  
<li>Modify the code so that it is the same as that shown in Listing 2. The important addition is the creation of the NumberFive function, which returns the number five. The statement Option Explicit forces all variables to be declared before they are used. If Option Explicit is omitted, variables are automatically defined at first use as type Variant.</li>
+
<li>Modify the code so that it is the same as that shown in Listing 2. The important addition is the creation of the NumberFive function, which returns the number five. The Option Explicit statement forces all variables to be declared before they are used. If Option Explicit is omitted, variables are automatically defined at first use as type Variant.</li>
 +
<li>Save the modified Module 1.</li>
 
</ol>
 
</ol>
  

Revision as of 17:49, 16 January 2011



Calc can call macros as Calc functions. Use the following steps to create a simple macro:

  1. Create a new Calc document named CalcTestMacros.ods.
  2. Use Tools > Macros > Organize Macros > OpenOffice.org Basic to open the OpenOffice.org Basic Macros dialog. The Macro from box lists available macro library containers including currently open OOo documents. My Macros contains macros that you write or add to OOo. OpenOffice.org Macros contains macros included with OOo and should not be changed.
  3. File:CG3Ch12F7.png
    OpenOffice.org Basic Macros dialog.
  4. Click Organizer to open the OpenOffice.org Basic Macro Organizer dialog. On the Libraries tab, select the document to contain the macro.
  5. File:CG3Ch12F8.png
    OpenOffice.org Basic Macro Organizer.
  6. Click New to open the New Library dialog.
  7. File:CG3Ch12F9.png
    New Library dialog.
  8. Enter a descriptive library name (such as AuthorsCalcMacros) and click OK to create the library. The new library name is shown the library list, but the dialog may show only a portion of the name.
  9. File:CG3Ch12F10.png
    The library is shown in the organizer.
  10. Select AuthorsCalcMacros and click Edit to edit the library. OOo automatically creates a module named Module1 and a macro named Main.
  11. File:CG3Ch12F11.png
    Basic Integrated Development Environment (IDE).
  12. Modify the code so that it is the same as that shown in Listing 2. The important addition is the creation of the NumberFive function, which returns the number five. The Option Explicit statement forces all variables to be declared before they are used. If Option Explicit is omitted, variables are automatically defined at first use as type Variant.
  13. Save the modified Module 1.

Listing 2. Function that returns five.

 REM  *****  BASIC  *****
 Option Explicit

 Sub Main

 End Sub

 Function NumberFive()
   NumberFive = 5
 End Function 


Content on this page is licensed under the Creative Common Attribution 3.0 license (CC-BY).
Personal tools