A simple guide for you to learn how to debug LibreOffice basic macro using breakpoint and watch.
While writing complex macros to automate various tasks in LibreOffice, you definitely encounter errors. Some run-time errors are self-explanatory. But some of them are very generic. To debug those, you need to carefully put breakpoints and step through the code to see where the problem is in your code.
Hence this tutorial. These techniques apply to all the macros written in Calc, Writer or Impress. And should be applied to OpenOffice macros as well.
Table of Contents
Debug a LibreOffice Macro written in Basic
It’s easier to demonstrate this concept using an example.
Let’s define three variables which we would use for our exercise.
dim i, j, cnt
for loop, which would execute from 1 to 10. Inside the loop, increment two variables as below. This is just for just this demo; however, you can put any logic you want.
for cnt = 1 to 10 i = i + 1 j = i + 1 next cnt
Now, we want to put two breakpoints in the statement
"for cnt = 1 to 10" and
"j = i + 1". When you put a breakpoint inside your program, it runs in debug mode and holds the execution at the breakpoint.
To put a breakpoint in a LibreOffice Basic macro, put the cursor in the statement. And then, press
F9 or press the below button from the toolbar.
Once you do that, you will see a red circle on the left side of the statement, which means a breakpoint has been added to that statement. See the below image. In addition, you can add multiple breakpoints as per your needs.
If you want to remove a breakpoint from a statement, press
F9 again in the statement, OR you can
double-click the red circle.
Now, we would add a ‘watch’ to the variable
When the program executes in debug mode, the watch helps monitor a variable’s value between program steps. To add a watch on
"cnt" variable, select the variable and press
F7 or click the glass icon in the toolbar.
Once you do that, you will see the variable added to the watch list at the bottom of the editor.
Execute by Step
We are all set with tools.
Run the program by pressing
F5. As we already added breakpoints, you would see the execution halts at the first breakpoint with a little yellow arrow.
Now you have two options.
F5 again to continue the execution of the program, and it will halt again at the next breakpoint.
F8 (step execution), which would execute step by step, and you can see the ‘watched’ variable
'cnt' value is changing as below.
F8. You can see the yellow arrow comes to the next statement, and the compiler waits. Now the fun part, if you take a closer look at the watch window, you can see the
'cnt'variable’s value is 1.
So this way, you can debug, add breakpoints and add watch any LibreOffice or OpenOffice macro using its editor.
Furthermore, you can add many watch variables as you want and debug your program for successful execution.
Although the above example is specific to LibreOffice macros, the same concept applies to programming and debugging in general. I hope this article helps you to understand the basics of debugging, step execution and watch in programming and macros in LibreOffice.
Looking for Something Else?
If you are looking for something else in LibreOffice macro tutorials Or wants to learn more about it, please follow the below link for the complete Macro Tutorials Index: