VB.Net Examples 2 - Select and Delete Text Table in Writer

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

Hi,
in this forum, there are many people who are able and probably also willing to help you with the LO API. There are very few people, however, who can support you with VB.NET questions, and the overlap between these two groups is zero.
This is why I suggest that you first create a minimum working sample in LO BASIC, attach it to this post, and in a second step ask for help in translation to VB.NET
Good luck,
ms777

MS777, sadly my experience has told me what you have put into words. It also seems forum administrators are curiously quiet on the subject (maybe I’ve not seen their replies). This worries me.

Paradoxically however, I note that in the one day its been posted here there have been 66 views of it. This puts it among the most highly viewed posts posted in the same time frame. As I believe that what you’ve said is true, this paradox confirms to me what I also believe; that there are many folks trying to use VBNet with the LO API and (Googling for help and) looking for help here. My thought is simply that there haven’t been many folks who have used it successfully because of the lack of support, not because it cannot be used successfully with VBNET beyond the functionality demonstrated in the (very good) VBNET WriterDemo.

That said, you provide a worthy suggestion. I’ll hope that there’ll be responses to it that are helpful. Notably I’ve seen other such posts here and in other forums, who’ve initiated questions just that way, some with accepted solutions (that don’t actually work, for me anyway) and some questions linger on with no solutions.

Here, in this post, I actually provide a VBNet solution to my 4th of 4 original queries. It seems obvious to me now, but of course nothing is simple when you do not have a solution and have no way to find one.

So, in a small way, I hope to gain solutions for all 4 queries, post them ~here~ and provide a means to build up support for the folks trying to use the LO API with VBNet. There’s nothing wrong with that is there?

There’s nothing wrong in discussing any language bindings here. There were some posts on Lua, on C#, JavaScript, etc. Indeed, given the huge complexity of the UNO API itself, the ~small relative share of use of LibreOffice and its automation, the absolute number of people using non-default (~bundled with LibreOffice itself) languages is not that high, hence you would indeed have better results splitting your tasks to parts specific to UNO API (which would be ~equal in any language, modulo syntax), and binding-specific parts (how to shape the code in one language into another).

There is ~no interference here on this Ask site from admins. It would rather be strange, if your questions received some admin response.

Thanks Mike