Libre Office CheckBox1, macro variable - libreoffice

How to get the value from CheckBox1 from the dialog in librecalc? I need to put this value in a variable,
I searched on the forum but did not find it
please help :)

You can try with a code like this
Sub Check1
If Dlg.GetControl("CheckBox1").State = 1 then
msgbox("OK")
else
msgbox("NO")
End If
End Sub

Related

LibreOffice Calc Goal Seek does not work from Basic Macro

I am trying to run a goal seek from a Calc Basic Macro.
Public Sub Goal_Seek()
oVariableCell = detailSheet.getCellRangeByName("VariableCell")
oFormulaCell = detailSheet.getCellRangeByName("FormulaCell")
oTargetCell = detailSheet.getCellRangeByName("TargetCell")
ThisComponent.seekGoal(oFormulaCell.CellAddress, oVariableCell.CellAddress, oTargetCell.getValue)
End Sub
When I run the Macro nothing is updated. When I run Goal Seek from the spreadsheet menu Tools->Goal Seek and enter the same cells in the form, the goal seek functions normally.
My Macro is in My Macros-Standard-Module1. The Named ranges return the right cell addresses. What could be causing the goal seek to not update when called from the Macro?
The reason it didn't work is I have to set the result myself when I call goal set from a Macro. I should have guessed that. The new code is
detailSheet = ThisComponent.Sheets(7)
oVariableCell = detailSheet.getCellRangeByName("VariableCell")
oFormulaCell = detailSheet.getCellRangeByName("FormulaCell")
oTargetCell = detailSheet.getCellRangeByName("TargetCell")
oGoal = ThisComponent.seekGoal(oFormulaCell.CellAddress, oVariableCell.CellAddress, oTargetCell.getValue)
oVariableCell.setValue(oGoal.Result)
End Sub

LibreOffice Macro always show #NULL! after reopening the file

I wrote a macro in LibreOffice Calc and it is able to run correctly. But if I close the file and reopen, it always show #NULL! instead of the correct value. What am I missing here?
My macro code
Rem Attribute VBA_ModuleType=VBAModule
Option VBASupport 1
Function Calculate(CalType As String) As Double
'
' Calculate Macro
'
Dim i As Integer
Calc = 0
i = 1
Do While Not IsEmpty(Cells(i, 2))
If (Cells(i, 3).Value = CalType And (Cells(i,2) = "A" Or Cells(i,2) = "B")) Then
Calculate = Calculate + Cells(i, 4).Value
ElseIf (Cells(i, 3).Value = CalType And Cells(i,2) = "C") Then
Calculate = Calculate - Cells(i, 4).Value
End If
i = i + 1
Loop
'
End Function
The calling function will be something like =Calculate(J6)
The file is saved as .ods format.
The Cells call did not work at all for me. It is from VBA, not LO Basic. However I do not think that is the main problem.
LibreOffice expects that user-defined functions will be simple, only accessing the cell that contains the formula. Since the spreadsheet has not been fully loaded yet when the function is called, it is not possible to read other cells.
The workaround is to ignore errors and wait until the document is fully loaded before running the function. Take the following code as an example:
Function ReadOtherCell(row, col)
On Error GoTo ErrorHandler
oSheet = ThisComponent.CurrentController.ActiveSheet()
oCell = oSheet.getCellByPosition(row, col)
ReadOtherCell = "value is '" & oCell.getString() & "'"
Exit Function
ErrorHandler:
Reset
End Function
Sub RecalculateAll
' This is for user-defined functions that need to read the spreadsheet.
' Assign it to the "View created" event,
' because before that, the spreadsheet is not fully loaded.
ThisComponent.calculateAll
End Sub
Enter foo in A1, and =ReadOtherCell(0,0) in A2. So far, this has the same problem -- It will fail when the document is first opened.
Now, go to Tools -> Customize. In the Events tab, highlight View created. Press Macro... and find the RecalculateAll function. Then press OK.
Now when the document is closed and reopened, cell A2 should show the result value is 'foo'.
This is derived from B. Marcelly's answer at http://ooo-forums.apache.org/en/forum/viewtopic.php?f=20&t=73090&sid=f92a89d676058ab597b4b4494833b2a0.
I had the same problem.
I noticed that in the module, i had an empty Sub Main
After i 've erased it, the functions started working again

Sub- or Function Procedure not defined

I am writing my first macro for Libre Office right now and I have come into a bit of a problem: My code throws the error: BASIC Runtime error; Sub- or Function procedure not defined.
The line with the "If Cells (RowCnt,ChkCol......) throws the error.
I've looked through other entries on here, but I could not find the error... can anyone help me?
REM ***** BASIC *****
Sub Zeilennausblenden_Nullsummen
BeginRow=4
EndRow = 46
ChkCol= D
For RowCnt = BeginRow To EndRow step 1
If Cells(RowCnt,ChkCol).Value > 1 Then
Cells(RowCnt,ChkCol).EntireRow.Hidden = True
End if
Next
End Sub
PS: The function should hide all rows in which an integer higher than "1" appears in column "D"
Thanks in advance
Here is what the code looks like in LibreOffice Basic (aka StarBasic):
Sub Zeilennausblenden_Nullsummen
BeginRow=4
EndRow = 46
ChkCol= 3
oSheet = ThisComponent.Sheets(0)
For RowCnt = BeginRow To EndRow step 1
oCell = oSheet.getCellByPosition(ChkCol,RowCnt)
If oCell.Value > 1 Then
oRow = oSheet.getRows().getByIndex(RowCnt)
oRow.IsVisible = False
End if
Next
End Sub
I wasn't sure if BeginRow should be 3 or 4, because it's zero-based. You can test it and decide.
Note that a macro is not necessary in order to accomplish this task. The easiest way is to go to Data -> More Filters -> Standard Filter.
That's because CELLS isn't a StarBasic function.
It's VBA (different programming language). Some versions of OpenOffice support the use of it if a statement (Option VBASupport 1) is put in first line of source code.
Check the net for Andrew Pitonyak's "OpenOffice Macros Explained" document - very good for learning and available in German translation, too.

Get acces to handles while in a Callback function

I have a Problem with my callback functions.
I wrote a small Example For it :
function drawMeAnImrect
Numbers = [1,2,3];
h = imrect();
h.addNewPositionCallback(#(h)Randomfunc(Numbers));
h.wait();
Numbers = [1,2,3,4];
end
function Randomfunc(Numbers)
disp(Numbers)
end
everytime i move the rect i will print the Numbers 1,2 and 3, even if i change the Numbers. Ist there a way to avoid this ? It works with global variables but i dont wanna use many global variables. I hope someone can help me. Thx

Execute code when form is closed in VBA (Excel 2007)

I would like to execute some code when a user closes a form using the x button in the top right corner of the window (I have the form load when the excel spreadsheet is opened, and it hides Excel. I want to exit excel once the form is closed, or at least show excel again so the user may exit it manually)
Looking at the form properties, the Unload property is not present, nor am I able to figure out how to make a function which executes when the form is closed.
Unfortunately, coding this in VB is not an option, it must be VBA.
I'm aware of the code needed to unhide Excel or quit it outright, just not how to tie it to the unload event.
You can use QueryClose event of the UserForm as follows:
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = 0 Then
' Your codes
' Tip: If you want to prevent closing UserForm by Close (×) button in the right-top corner of the UserForm, just uncomment the following line:
' Cancel = True
End If
End Sub
You can also use vbFormControlMenu like this:
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = vbFormControlMenu Then
'Your code goes here
End If
End Sub
A colleague was able to provide the answer, including example here for everybody else
Private Sub userform_terminate()
'Code goes here
End Sub
You can use Unload Me in VBA to close a form. Just put the code to close Excel immediately following that.
Private Sub Form_Unload(Cancel As Integer)
Dim msgRes As VbMsgBoxResult
msgRes = MsgBox("Exit form ?", vbYesNo)
If msgRes = vbYes Then
'optional code
ElseIf msgRes = vbNo Then
Cancel = True
End If
End Sub
I was able to prevent the form from closing when the X button was click using the following:
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
Cancel = MsgBox("Please confirm cancellation", vbOKCancel + vbQuestion) = vbCancel
End Sub
try something like this:-
Private Sub Form1_FormClosing(sender as Object, e as FormClosingEventArgs) _
Handles Form1.FormClosing
//Code you want to execute
End Sub