This is a continuation of my original Post “VB.Net Examples.” I originally posted four queries about using the LibreOffice API. I’ve searched for a very long time (dare I say ‘weeks’) to find some examples of how to do any of this in VB.Net, so hopefully I’m saving someone time if they see this. The LibreOffice API is challenging at best, even for the simple stuff.
The responses to the original post solved part of one of my queries.
This Post addresses another one of the queries, “How do you select and Delete a TextTable.” This post is in TWO PARTS. The first part is to show a solution to Deleting a table, this is the easy part.
The following is directly from the very good example ‘WriterDemo’ found elsewhere in the LibreOffice Website. If you are getting started with the API and using VB.Net you should start there. I include it here for completeness and am not taking credit for it. The following shows how to create a TextTable (you need to have a table to delete one! And also a document that the table lives in (separate issue)!)
'Create instance of a text table with 4 columns and 4 rows
Dim objTextTable As Object
objTextTable = DirectCast(xTextDocument, unoidl.com.sun.star.lang.XMultiServiceFactory).createInstance(“com.sun.star.text.TextTable”)
Dim xTextTable As unoidl.com.sun.star.text.XTextTable
xTextTable2 = DirectCast(objTextTable, unoidl.com.sun.star.text.XTextTable)
xTextTable = DirectCast(objTextTable, unoidl.com.sun.star.text.XTextTable)
xTextTable.initialize(4, 4)
xText.insertTextContent(xCursor, xTextTable, False)
Once you ‘have’ a TextTable and want to delete it the code is simply
xTextTable.Dispose.
Now for the only slightly more challenging part, selecting an existing table. In my use of the LibreOffice API I am forced to used templates (which isn’t a terrible way to go, just limiting) and so I am often selecting a table to insert into or delete if the template contains too many.
To use the code below, you must already have Global Variables defined and filled for various objects in the document (I know ‘Global Variables’ will pique some comments. I use them whenever I’m ~trying~ to figure out how to employ difficult constructs). *** Continuing, See my first post for the function ‘getExistingWriterFile’*** this code will show you how to build a document and make available certain key objects. I don’t claim this set of objects to be all you’ll ever need, just enough to get you started.
And of course, you must also have a table in your document. Then the following code can be used to ‘get’ it.
Public Shared Function GetExistingTable(ByRef strTableName As String) As unoidl.com.sun.star.text.XTextTable
Dim xTableSupplier As XTextTablesSupplier = DirectCast(loDocXComponent, XTextTablesSupplier)
Dim xTables As XNameAccess = xTableSupplier.getTextTables()
Try
loDocXTextTable = CType(xTables.getByName(strTableName).Value, XTextTable)
Return loDocXTextTable
Catch ex As System.Exception
Return Nothing 'Or something if you choose to
End Try
End Function