[Solved] "Writing Macros for Dummies" tutorials/docs?

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
Fafhrd
Posts: 4
Joined: Mon Oct 03, 2022 3:14 am

[Solved] "Writing Macros for Dummies" tutorials/docs?

Post by Fafhrd »

Hi. I'm trying to write some Basic macros and am looking for some documentation and/or examples of simple Basic macros. I've read the Apache documentation, but am finding it difficult to navigate the modules/interfaces to create objects correctly. I observe that the examples provided seem not to work. For example, if I try to run code like this:

dim Doc as Object
dim Sheet as Object

Doc = ThisComponent
Sheet = Doc.Sheets.getByName("Whatever")

... I get a runtime error "Property or Methods Not Found: Sheets". My guess is that lines are omitted to simplify the documentation, but I need a few complete examples.

Can someone point me in a helpful direction?

Thanks in advance.
Last edited by Fafhrd on Tue Oct 04, 2022 9:12 pm, edited 2 times in total.
Fafhrd
OpenOffice 4.1.13 on Windows 10
FJCC
Moderator
Posts: 9248
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: "Writing Macros for Dummies" tutorials/docs?

Post by FJCC »

Check the documents at Andrew Pitonyak's web site: OpenOffice.org Macros Explained and the English Macro Document

However, the macro code you posted should work if run from a Calc document that has a sheet named Whatever. How are you using the code?
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
JeJe
Volunteer
Posts: 2764
Joined: Wed Mar 09, 2016 2:40 pm

Re: "Writing Macros for Dummies" tutorials/docs?

Post by JeJe »

Provided the current component is a spreadsheet document with a sheet named "Whatever" your code should work and not give an error.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
User avatar
Zizi64
Volunteer
Posts: 11353
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: "Writing Macros for Dummies" tutorials/docs?

Post by Zizi64 »

Here is a working example
MyFirstSub.ods
(9.98 KiB) Downloaded 89 times
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
User avatar
Villeroy
Volunteer
Posts: 31269
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: "Writing Macros for Dummies" tutorials/docs?

Post by Villeroy »

Dummies may be able to write VBA code for Excel whereas writing StarBasic for UNO is completely out of reach. Forget it, Dummy.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
JeJe
Volunteer
Posts: 2764
Joined: Wed Mar 09, 2016 2:40 pm

Re: "Writing Macros for Dummies" tutorials/docs?

Post by JeJe »

The best source for simple macros is possibly here - there's often someone's had the same problem and a search will find it.

You might these tools invaluable:

https://extensions.openoffice.org/en/pr ... -ide-tools

https://extensions.openoffice.org/en/pr ... ction-tool
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Fafhrd
Posts: 4
Joined: Mon Oct 03, 2022 3:14 am

Re: "Writing Macros for Dummies" tutorials/docs?

Post by Fafhrd »

FJCC wrote: Mon Oct 03, 2022 6:41 am Check the documents at Andrew Pitonyak's web site: OpenOffice.org Macros Explained and the English Macro Document

However, the macro code you posted should work if run from a Calc document that has a sheet named Whatever. How are you using the code?
' Begin module

dim Doc as Object
dim Sheet as Object

sub Main
' Need to call this function to enable "Private" variables/objects.
CompatibilityMode(True)

' Initialize objects and variables.
Init

...
end sub

sub Init

Doc = ThisComponent
If Doc.Sheets.hasByName("Whatever") Then
Sheet = Doc.Sheets.getByName("Whatever")
Else
exit sub
End If

...
end sub
Fafhrd
OpenOffice 4.1.13 on Windows 10
Fafhrd
Posts: 4
Joined: Mon Oct 03, 2022 3:14 am

Re: "Writing Macros for Dummies" tutorials/docs?

Post by Fafhrd »

Never mind, figured it out. I had my macro defined in an (otherwise empty) separate doc, opened that doc from another, and ran the macro. Context is still that of the empty doc, apparently.
Fafhrd
OpenOffice 4.1.13 on Windows 10
User avatar
Zizi64
Volunteer
Posts: 11353
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: "Writing Macros for Dummies" tutorials/docs?

Post by Zizi64 »

Please use the CODE tag for formatting the macro code lista in this forum.

Code: Select all

 Begin module

dim Doc as Object
dim Sheet as Object

sub Main
' Need to call this function to enable "Private" variables/objects.
CompatibilityMode(True)

' Initialize objects and variables.
Init

...
end sub

sub Init

Doc = ThisComponent
If Doc.Sheets.hasByName("Whatever") Then
Sheet = Doc.Sheets.getByName("Whatever")
Else
exit sub
End If

...
end sub
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
User avatar
Zizi64
Volunteer
Posts: 11353
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: "Writing Macros for Dummies" tutorials/docs?

Post by Zizi64 »

Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
Fafhrd
Posts: 4
Joined: Mon Oct 03, 2022 3:14 am

Re: "Writing Macros for Dummies" tutorials/docs?

Post by Fafhrd »

Thanks everyone, I'm good (for now).
Fafhrd
OpenOffice 4.1.13 on Windows 10
User avatar
Zizi64
Volunteer
Posts: 11353
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: "Writing Macros for Dummies" tutorials/docs?

Post by Zizi64 »

Zizi64 wrote: Tue Oct 04, 2022 6:44 am Please use the CODE tag for formatting the macro code list in this forum.

Code: Select all

 Begin module

dim Doc as Object
dim Sheet as Object

sub Main
' Need to call this function to enable "Private" variables/objects.
CompatibilityMode(True)

' Initialize objects and variables.
Init

...
end sub

sub Init

Doc = ThisComponent
If Doc.Sheets.hasByName("Whatever") Then
Sheet = Doc.Sheets.getByName("Whatever")
Else
exit sub
End If

...
end sub
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
Post Reply