Ask Your Question
0

How to find an open document

asked 2017-10-06 03:08:19 +0200

owlbrudder gravatar image

At https://forum.openoffice.org/en/forum/viewtopic.php?f=45&t=78338 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.

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
0

answered 2017-10-06 04:34:25 +0200

owlbrudder gravatar image

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

edit flag offensive delete link more

Comments

You found the proper way.

Ratslinger gravatar imageRatslinger ( 2017-10-06 05:58:09 +0200 )edit

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()

JohnSUN gravatar imageJohnSUN ( 2017-10-06 08:43:09 +0200 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2017-10-06 03:08:19 +0200

Seen: 225 times

Last updated: Oct 06 '17