At How to check if the file is open? (View topic) • Apache OpenOffice Community Forum Villeroy gave a brief description of a solution to this problem, but I am having trouble making it work.
The problem is to find if a given document is open and, if so, assign it to an object for further processing.
The solution Villeroy gave was
Loop through the frames of the StarDesktop, get the controller of each frame. If the controller has a model object, then this model is a document with all the methods and properties of a document. Help files and Basic code are documents as well.
I found the Desktop object has a findFrame() method and decided to use this. Maybe I am wrong.
I have tried looping through the frames of the Desktop, but cannot find my document. Here is my code:
`'******************************************************************************************
'// Stub to exercise fncFindOpenDocument
'******************************************************************************************
Sub subStub
MsgBox "fncFindOpenDocument(""Parameters.ods"") = " & fncFindOpenDocument("Parameters.ods")
End Sub
'******************************************************************************************
'// Find a given document in the desktop
'******************************************************************************************
Function fncFindOpenDocument(strDocumentName As String) As Boolean
Dim objDesktop As Object
Dim objFrames As Object
Dim objFrame As Object
Dim objModel As Object
Dim objDocument As Object
Dim intLoop As Integer
Dim boolResult As Boolean
On Error GoTo hell
boolResult = False
'Iterating the Desktop frames until we find the document of interest
objDesktop = createUnoService("com.sun.star.frame.Desktop")
objFrames = objDesktop.getFrames()
For intLoop = 0 To objFrames.getCount - 1
objFrame = objFrames(intLoop)
If objFrame.getName() = strDocumentName Then
'<<<here is where I want to assign the document to my objDocument >>>
boolResult = True
GoTo hello
End If
Next
GoTo hello
hell:
MsgBox "Error " & Err & ": " & Error$ + chr(13) + "At line : " + Erl + chr(13) + Now , 16 ,"An error occurred in fncFindOpenDocument"
hello:
'Goodbye
fncFindOpenDocument = boolResult
End Function
`
The code does not fail and the loop is iterated (6 times in my case), but the open document is not found.
I am sure it is easy, but the solution is eluding me.