How to find a parent component

Hello everybody,

I am trying to check programmatically if a Calc spreadsheet is already open. I’m sure there is a much easier method of doing this, but the way I have come up with is to list all the active frames, and it worked. However, it also brought me to another question below:

I have listed all the frames currently active using StarDesktop.getFrames.

Currently, in my particular case, StarDesktop.Frames(2) is equivalent to ThisComponent.CurrentController.Frame.

With respect to the StarDesktop.Frames(2) statement, how would I reach ThisComponent or ThisComponent.CurrentController? I tried using “.parent” but with no success.

Thanks in advance for all suggestions.

WHY withdrawn??
If you found a solution yourself, post it!

Sub Main()
frs = Stardesktop.Frames
uFrs = frs.Count - 1
For j = 0 To uFrs
  j_fr = frs(j)
  comp = getSpreadsheetComponentForFrame(j_fr)
  If NOT IsNull(comp) Then
    MsgBox(j_fr.Title)
  Else
    MsgBox("Not a spreadsheet component!")
  EndIf
Next j
End Sub

Function getSpreadsheetComponentForFrame(Optional pFrame)
REM Based on info by "Villeroy"
comp = Nothing
If IsMissing(pFrame) Then pFrame = ThisComponent.CurrentController.Frame
ctrl = pFrame.Controller
comp = Nothing
On Local Error Goto fail
If ctrl.supportsService("com.sun.star.sheet.SpreadsheetView") Then
  comp = ctrl.ActiveSheet.DrawPage.Forms.Parent
EndIf
fail:
getSpreadsheetComponentForFrame = comp
End Function
1 Like

@Lupp You are absolutely correct. I should not have done that. However, when opening a file, I realized that by using “_default” flag, instead of “_blank”, basic will utilize the open copy of the file, instead of opening a new one. Thus, allowing full control by accessing it through the new object created. I simply have concluded that my question does not really make sense. Hence the decision.

However, now that you have posted the solution, I’d like to ask how did you go about finding this information. I’m reading Mr. Pitonyak’s file, plus other online sources and the amount of information is overwhelming to say the least. How did you deduce this particular answer? Where do I go to?