Page 1 of 1
[Solved] Convert simple VBA script to OpenOffice Basic
Posted: Tue Dec 10, 2024 9:47 pm
by quest78234t72t23
I'm sorry for stupid question, this is my first attempt to create script to OpenOffice. In this case I just need to implement in OpenOffice very simple idea, forbid to accept existing data. This is my VBA script, what I try to realize in OpenOffice
Code: Select all
Private Sub Workbook_Open()
StartAppEvents
End Sub
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
MsgBox "App_WorksheetChange"
' Disable events to prevent infinite loop
Application.EnableEvents = False
On Error GoTo ErrorHandler
Dim i As Long, initialRow As Long
initialRow = Target.Row
For i = 1 To initialRow - 1
If Sh.Cells(i, Target.Column) = Sh.Cells(initialRow, Target.Column) Then
Target.ClearContents
Sh.Cells(initialRow, Target.Column).Select
Exit For
Else
Sh.Cells(initialRow, Target.Column) = Target.Value
Sh.Cells(initialRow + 1, Target.Column).Select
End If
Next i
IgnoreInput:
' Re-enable events
Application.EnableEvents = True
Exit Sub
ErrorHandler:
If Err.Number <> 0 Then
Msg = "Error # " & Str(Err.Number) & " was generated by " _
& Err.Source & Chr(13) & "Error Line: " & Erl & Chr(13) & Err.Description
MsgBox Msg, , "Error", Err.HelpFile, Err.HelpContext
End If
Resume IgnoreInput
End Sub
Unfortunately, I don't understand how to make this. I don't see project explorer, instead of project explorer I see something dropdownlist. What I should select in this dropdownlist? How to implement this event "Workbook_SheetChange" in OpenOffice Basic? Do this VBA workflow correct for OpenOffice?
Re: Convert simplest VBA script to OpenOffice Basic
Posted: Wed Dec 11, 2024 12:16 am
by JeJe
Select the entry ending in "Standard" that's the standard library, you need to create a module in that library for your code.
Write "Option VBASupport 1" at the top for greater VBA compatability. Then write your code underneath.
Here's a good place to start learning about OO Basic programming
https://www.pitonyak.org/oo.php
Re: Convert simplest VBA script to OpenOffice Basic
Posted: Wed Dec 11, 2024 12:03 pm
by quest78234t72t23
Thank, @JeJe, but unfortunately, I'm faced with a lot of troubles to create my first script.
1. I found some script what make function I want to do, firstly I need to catch any changing in worksheet. I found this function
Code: Select all
Option Explicit
' Declare a global variable to hold the listener
Private sheetChangeListener As Object
' This function initializes the sheet change listener
Sub InitializeSheetChangeListener()
' Check if the listener is already initialized
If Not IsNull(sheetChangeListener) Then Exit Sub ' prevent multiple listeners
' Create an instance of the listener class
Set sheetChangeListener = CreateObject("com.sun.star.sheet.SheetChangeListener")
' Get the current document
Dim document As Object
document = ThisComponent
' Get the active sheet
Dim sheet As Object
sheet = document.CurrentController.ActiveSheet
' Add the listener to the sheet
sheet.addSheetChangeListener(sheetChangeListener)
End Sub
' Implement the SheetChangeListener interface's "modified" method
Sub sheetChangeListener_modified(event As Object)
' Get the affected sheet
Dim affectedSheet As Object
affectedSheet = event.Sheet
' Unfortunately, there's no direct equivalent to VBA's "Target" range in OpenOffice.
' The following demonstrates how to get *all* changed cells since the last event.
' This is NOT the same as Target, as it doesn't give the cells directly involved in the current change.
Dim changedCells As Object
changedCells = affectedSheet.getChangedCells()
' Example: Print the address of each changed cell to the console.
Dim cell As Object
For Each cell In changedCells
Print cell.getCellAddress().AbsoluteName
Next cell
' Example: Check if cell A1 was among the changed cells
Dim a1 As Object
a1 = affectedSheet.getCellByPosition(0, 0) ' Column A, Row 1 (zero-based)
If NOT IsNull(changedCells) Then
Dim cellRange As Object
For Each cellRange in changedCells
If cellRange.getCellAddress().AbsoluteName = a1.getCellAddress().AbsoluteName Then
msgbox "Cell A1 was changed!"
Exit For ' Cell A1 found among changes
End If
next cellRange
End If
' IMPORTANT FOR PERFORMANCE: Clear the changed cells after processing
' Otherwise, they accumulate and checking change gets slow!
affectedSheet.clearChangedCells()
End Sub
' This Sub can be called from "Tools -> Macros -> Organize Macros -> OpenOffice Basic"
' to initialize the listener. It needs to be run only once per document session.
Sub StartListeningForSheetChanges()
InitializeSheetChangeListener
End Sub
Is this function looking good?
2. When I press button like PLAY in Toolbar I get this error "Error creating object"

- OpenOffice-12112024_114428_1.gif (30.1 KiB) Viewed 9821 times
3. Also I try to start this script as "Tools->Macros->Run Macro", in this case I'm faced with another troubles.

- OpenOffice-12112024_111205_1.gif (62.98 KiB) Viewed 9821 times
4. And finally, I try to set JRE environment in my computer, I have all of last version and and get trouble again. So, OpenOffice can accept my JRE environment - why?

- OpenOffice-12112024_113353_1.gif (67.48 KiB) Viewed 9821 times
(*) I used last version of Windows and last version of OpenOffice, registration on this forum is extremely painful and my current signature is wrong, there is no combobox/dropdown list to select OpenOffice version, any text value I try to set was wrong, except one, therefore sorry signature is incorrect, I already used all fresh version of OpenOffice and Windows.
Re: Convert simplest VBA script to OpenOffice Basic
Posted: Wed Dec 11, 2024 12:14 pm
by Hagar Delest
For the JRE, you have to install the 32bit version because there is no 64bit version of AOO for Windows.
quest78234t72t23 wrote: ↑Wed Dec 11, 2024 12:03 pm
registration on this forum is extremely painful and my current signature is wrong, there is no combobox/dropdown list to select OpenOffice version, any text value I try to set was wrong, except one, therefore sorry signature is incorrect, I already used all fresh version of OpenOffice and Windows.
Plenty of users have managed to do it. Put "OpenOffice 4.15 on Windows 11" for example.
Look at mine, there is not even a reference to OpenOffice!
See:
How to update your software information signature.
Re: Convert simplest VBA script to OpenOffice Basic
Posted: Wed Dec 11, 2024 12:31 pm
by Zizi64
(*) I used last version of Windows and last version of OpenOffice, registration on this forum is extremely painful and my current signature is wrong, there is no combobox/dropdown list to select OpenOffice version, any text value I try to set was wrong, except one, therefore sorry signature is incorrect, I already used all fresh version of OpenOffice and Windows.
You can edit manually your signature in your Forum-profile.
Re: Convert simplest VBA script to OpenOffice Basic
Posted: Wed Dec 11, 2024 12:34 pm
by Zizi64
4. And finally, I try to set JRE environment in my computer, I have all of last version and and get trouble again. So, OpenOffice can accept my JRE environment - why?
Try the LibreOffice. The LO has 32 and 64 bit versions for the Windows, and the 64 bit version can accept the 64 bit version JAVA
Re: Convert simplest VBA script to OpenOffice Basic
Posted: Wed Dec 11, 2024 12:37 pm
by Zizi64
Maybe the built-in SheetEvent (of the LibreOffice) is enough for you (Right click on the Sheet TAB):

- ContentChangedEvent.png (22.93 KiB) Viewed 9794 times
Re: Convert simplest VBA script to OpenOffice Basic
Posted: Wed Dec 11, 2024 1:12 pm
by quest78234t72t23
Many thanks moderator and @Zixi64. I have Updated profile, understand issue with 32/64 JRE version for OpenOffice.
And understand about LibreOffice, Will try.
Re: Convert simplest VBA script to OpenOffice Basic
Posted: Wed Dec 11, 2024 10:15 pm
by quest78234t72t23
Thanks for help, @Zizi64. I have write my first macro and it working fine, but a couple of my is questions still unresolved.
1. Visually OpenOffice and LibreOffice looks as clone. But macro really working only in LibreOffice, it look strange - why?
2. Fonts on LibreOffice is more clear than in OpenOffice, it's strange.

- OpenOffice-12112024_220039.gif (64.15 KiB) Viewed 9628 times
3. And any my experiment to set Java environment for Open Office was failed, however Libre Office has found all my Java installation, select Java 19 version (because I use this Version on my Android Studio and I define this version in JAVA_HOME).

- OpenOffice-12112024_220849_1.gif (127.09 KiB) Viewed 9628 times
Do this 3 points means that LibreOffice is better than OpenOffice or there are another explanations?
Re: Convert simplest VBA script to OpenOffice Basic
Posted: Wed Dec 11, 2024 11:15 pm
by Zizi64
1. Visually OpenOffice and LibreOffice looks as clone. But macro really working only in LibreOffice, it look strange - why?
The OpenOffice is the ancestor of the LibreOffice.
The LO was forked at version 3.3.
And the development of the LO is more active today.
https://en.wikipedia.org/wiki/LibreOffice#History
Re: Convert simplest VBA script to OpenOffice Basic
Posted: Thu Dec 12, 2024 9:55 am
by Mr.Dandy
quest78234t72t23 wrote: ↑Wed Dec 11, 2024 10:15 pmAnd any my experiment to set Java environment for Open Office was failed,
you should read what you read more carefully
Hagar Delest wrote: ↑Wed Dec 11, 2024 12:14 pm
For the JRE, you have to install the 32bit version because there is no 64bit version of AOO for Windows.