How to find an open document

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.

Thanks Ratslinger. If finally cracked it by READING Villeroy’s post (doh! Slaps forehead). I am still debugging, but the following code is returning reasonable results. (I have note used the assignment of the model to my object yet … crossing fingers). Here is my loop code so far:

	'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)
	objController = objFrame.Controller
	'Does the controller have a model object?
	objModel = objController.getModel()
	If (Not IsNull(objModel)) Then
		strURL = objModel.getURL()
		If strURL = strDocumentName Then
			'<<<here is where I want to assign the document to my objDocument >>>
			objDocument= objModel
			boolResult = True
			GoTo hello
		End If
	End If
Next

Important mental health tip: read posts PROPERLY before asking foolish questions.

Kind regards, Doug

You found the proper way.

This algorithm was always located in the standard library “Tools” in the module “Misc”. So after GlobalScope.BasicLibraries.LoadLibrary("Tools") you can use function TaskOnDesktop() like as your fncFindOpenDocument. Or simple “get document to variable” with OpenDocument()