Hi all, I have some Basic code that jumps to a certain point in a document, then deletes some lines. This works fine, but I need to add some intelligence into this. Sometimes there are no lines to delete, sometimes there are 1, 2 or 3 lines to remove. In every case, there is a table that I need to stop at.
I’ve tried to use the text cursor method, but I don’t understand how you can determine where it puts this cursor, also when I tell the cursor to move, then inspect the string, it doesn’t change, telling me the cursor isn’t actually moving around.
The table text is fixed. I have created a test document to try and get it working properly. In the real document, there is a ToC, hence the search loop.
TableProblemDocTest.odt (12.3 KB)
This is my working code. I’m sure it can be written better, there was a lot of copy/pasting going on here from lots of different sources. But I’m struggling to understand how the text cursor works.
My thinking is, search for the correct place in the document, jump to it, read the next line, if it does not start with “Version” (the table), delete the line, loop. The only thing I want is the heading and the table on the next line.
LO Version: 7.6.4.1 (X86_64)
Windows 10
Sub DeleteLines
Dim runNumber As integer
Dim document As object
Dim dispatcher As Object
Dim linePos(1) As new com.sun.star.beans.PropertyValue
Dim findHeading(0) As new com.sun.star.beans.PropertyValue
document = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
'Define the search pattern for finding "Change History"
findHeading(0).Name = "SearchItem.SearchString"
findHeading(0).Value = "Change History"
'Run the search twice, because the first result is in the Contents
For runNumber = 1 To 2
dispatcher.executeDispatch(document, ".uno:ExecuteSearch", "", 0, findHeading())
Next runNumber
'Define the line movement
linePos(0).Name = "Count"
linePos(0).Value = 1
linePos(1).Name = "Select"
linePos(1).Value = false
dispatcher.executeDispatch(document, ".uno:GoDown", "", 0, linePos())
dispatcher.executeDispatch(document, ".uno:Delete", "", 0, Array())
dispatcher.executeDispatch(document, ".uno:Delete", "", 0, Array())
dispatcher.executeDispatch(document, ".uno:Delete", "", 0, Array())
End Sub