Good Morning,
I have an issue when opening OGD files from a folder using a loop. There is nothing wrong with the files. I can open them manually/individually with no problem. However when i make a macro to open the files, analyze them and close them, then inevitable some problem occurs, like LO unable to read the file due to XML problem, or even LO crashes. This is true whether I write the code in Basic or Python. Here is my basic code:
Option Explicit
Dim dtp As Object
Dim doc As object
Sub Main
Dim strPath As String, strFileName As String
dtp= GetProcessServiceManager().createInstance("com.sun.star.frame.Desktop")
strPath ="K:\folderpath\odg\"
strFileName = Dir(strPath, 0)
Do While (strFileName <> "")
strFileName = Dir()
If strFileName Like "*odg" Then
doc = getDoc(strPath & strFileName)
closeIt(doc)
End if
Loop
End Sub
Function getDoc(fName As String) As Object
Dim _doc As Object
Dim url As String
Dim docs As Object
On Error GoTo check_window:
url = ConvertToUrl(fName)
_doc =dtp.loadComponentFromURL(url, "_default", 0, Array()) 'Crash/error occurs here
'Sometimes the problem (which does not raise an error) is that the document is open, but _doc variable is not set.
'When this happen then i check for which DrawingDocuments are open and set the
'_doc variable to that instance
If isDrawing(_doc) then
getDoc=_doc
Exit function
Else
check_window:
'If an error is raised, then again i go through the same procedure of check which if
'any drawing documents are open and set the _doc variable to that instance
docs = dtp.getComponents().createEnumeration()
While docs.hasMoreElements()
_doc = docs.nextElement()
If isDrawing(_doc) Then
getDoc=_doc
Exit function
End if
Wend
End If
End Function
Function isDrawing(doc)
If doc Is Nothing Then
isDrawing = false
Else
isDrawing = doc.SupportsService("com.sun.star.drawing.DrawingDocument")
End if
End Function
Sub closeIt(_doc As Object)
If isDrawing(_doc) Then
_doc.close(false)
_doc.dispose()
_doc=Nothing
End If
End sub
The version I use is:
Version: 7.3.1.3 (x86) / LibreOffice Community
Build ID: a69ca51ded25f3eefd52d7bf9a5fad8c90b87951
CPU threads: 8; OS: Windows 10.0 Build 19042; UI render: Skia/Raster; VCL: win
Locale: en-US (en_US); UI: en-US
Calc: CL