[Solved] Convert simple VBA script to OpenOffice Basic

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Locked
quest78234t72t23
Posts: 4
Joined: Tue Dec 10, 2024 9:24 pm

[Solved] Convert simple VBA script to OpenOffice Basic

Post 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?
Attachments
OpenOfficeMenu-12102024_214230_1.png
OpenOfficeMenu-12102024_214230_1.png (90.63 KiB) Viewed 9923 times
Last edited by MrProgrammer on Sun Dec 29, 2024 9:18 pm, edited 1 time in total.
Reason: Tagged ✓ [Solved] quest78234t72t23 says: first macro is working fine -- MrProgrammer, forum moderator
Apache OpenOffice 4.1.13 for Microsoft and LibreOffice Community Version: 24.8.3.2 (X86_64) for Windows 11 Pro Version 10.0.22631 Build 22631
JeJe
Volunteer
Posts: 3064
Joined: Wed Mar 09, 2016 2:40 pm

Re: Convert simplest VBA script to OpenOffice Basic

Post 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
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
quest78234t72t23
Posts: 4
Joined: Tue Dec 10, 2024 9:24 pm

Re: Convert simplest VBA script to OpenOffice Basic

Post 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
OpenOffice-12112024_114428_1.gif (30.1 KiB) Viewed 9818 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
OpenOffice-12112024_111205_1.gif (62.98 KiB) Viewed 9818 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
OpenOffice-12112024_113353_1.gif (67.48 KiB) Viewed 9818 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.
Apache OpenOffice 4.1.13 for Microsoft and LibreOffice Community Version: 24.8.3.2 (X86_64) for Windows 11 Pro Version 10.0.22631 Build 22631
User avatar
Hagar Delest
Moderator
Posts: 33346
Joined: Sun Oct 07, 2007 9:07 pm
Location: France

Re: Convert simplest VBA script to OpenOffice Basic

Post 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.
LibreOffice 25.2 on Linux Mint Debian Edition (LMDE Faye) and 24.8 portable on Windows 11.
User avatar
Zizi64
Volunteer
Posts: 11476
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Convert simplest VBA script to OpenOffice Basic

Post 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.
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: 11476
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Convert simplest VBA script to OpenOffice Basic

Post 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
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: 11476
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Convert simplest VBA script to OpenOffice Basic

Post by Zizi64 »

Maybe the built-in SheetEvent (of the LibreOffice) is enough for you (Right click on the Sheet TAB):
ContentChangedEvent.png
ContentChangedEvent.png (22.93 KiB) Viewed 9791 times
SheetChanged.ods
(64.74 KiB) Downloaded 229 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.
quest78234t72t23
Posts: 4
Joined: Tue Dec 10, 2024 9:24 pm

Re: Convert simplest VBA script to OpenOffice Basic

Post 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.
Apache OpenOffice 4.1.13 for Microsoft and LibreOffice Community Version: 24.8.3.2 (X86_64) for Windows 11 Pro Version 10.0.22631 Build 22631
quest78234t72t23
Posts: 4
Joined: Tue Dec 10, 2024 9:24 pm

Re: Convert simplest VBA script to OpenOffice Basic

Post 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
OpenOffice-12112024_220039.gif (64.15 KiB) Viewed 9625 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
OpenOffice-12112024_220849_1.gif (127.09 KiB) Viewed 9625 times

Do this 3 points means that LibreOffice is better than OpenOffice or there are another explanations?
Apache OpenOffice 4.1.13 for Microsoft and LibreOffice Community Version: 24.8.3.2 (X86_64) for Windows 11 Pro Version 10.0.22631 Build 22631
User avatar
Zizi64
Volunteer
Posts: 11476
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Convert simplest VBA script to OpenOffice Basic

Post 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
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
Mr.Dandy
Posts: 461
Joined: Tue Dec 11, 2012 4:22 pm

Re: Convert simplest VBA script to OpenOffice Basic

Post 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.
OpenOffice 4.1.12 - Windows 10
Locked