Can't close LibreOffice after filling document via VB.net

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
VB_Noob
Posts: 1
Joined: Sat Jul 24, 2021 2:45 pm

Can't close LibreOffice after filling document via VB.net

Post by VB_Noob »

I use a VB.net-Form to open a .odt-document with LibreOffice, replace a few words and save the document. Now I have a small problem: I can close the odt-document, but I can't close LibreOffice-Writer anymore. I can close Writer only if I close my VB.net-Form too - what is annoying.

There must be a "invisible" connection kept between my VB-form an LibreOffice-Writer even if the odt-document is closed already. The "invisible" connection gets only terminated if I close the VB.net-form. Can anybody help me?

Code: Select all

Sub letter_libreoffice()

Try

Dim oSM, oDesk, oDoc, oReplace As Object

Dim aNoArgs(-1) As Object

oSM = CreateObject("com.sun.star.ServiceManager")
oDesk = oSM.createInstance("com.sun.star.frame.Desktop")
oDoc = oDesk.loadComponentFromURL("file:///" & Replace(var_pfad_musterbriefe, "\", "/") & "/" & var_Dokumenten_Art, "_blank", 0, aNoArgs)
oReplace = oDoc.createReplaceDescriptor

oReplace.SearchString = "{Vorname}"
oReplace.ReplaceString = CStr(var_vorname)
oDoc.replaceAll(oReplace)

oReplace.SearchString = "{Nachname}"
oReplace.ReplaceString = CStr(var_nachname)
oDoc.replaceAll(oReplace)

If System.IO.File.Exists(CStr(var_worddokument_pfad_doc)) = False Then
     
     var_worddokument_pfad_doc = Replace(var_worddokument_pfad_doc, "\", "/")
     oDoc.storeAsURL("file:///" & var_worddokument_pfad_doc, aNoArgs)

Else
    Me.Focus()
    Me.Activate()
    MsgBox("Document allready exists")
End If
         
oDoc = Nothing
oDesk = Nothing
oSM = Nothing
oReplace = Nothing

Catch ex As Exception
     MsgBox("An exception occurred:" & vbCrLf & ex.Message)

End Try

End Sub
OpenOffice 3.1 on Windows Vista
Mountaineer
Posts: 311
Joined: Sun Sep 06, 2020 8:27 am

Re: Can't close LibreOffice after filling document via VB.ne

Post by Mountaineer »

Is this your complete code?

I see you save the document, but afterwards your

Code: Select all

oDoc = Nothing
oDesk = Nothing
oSM = Nothing
is only "forgetting" all cleanup.
You just erase the refererences, but never close the document nor stop the service.
With scheme (a variant of LISP) you could wait for the garbage-collector but with BASIC?

So cleanup has to be done for you, when your program terminates by the system-library.

J.
OpenOffice 3.1 on Windows Vista
Post Reply