The easiest way is to add to the end of the macro
oWindow1.toFront
(tested for MS Windows).
In general, you need to take into account that after adding a second window, the document already has two controllers (and not one, as usual).
The easiest way is to add to the end of the macro
oWindow1.toFront
(tested for MS Windows).
In general, you need to take into account that after adding a second window, the document already has two controllers (and not one, as usual).
Yes, two controllers. But the toFront method doesnât work (Ubuntu) because the windows donât overlap. Focus does not move. Nothing happens.
So, here is the final code. @sokol92, thanks again.
REM ***** BASIC *****
Option Explicit
Sub ArrangeTwoWindowsVertical()
''' Open two windows of the same document,
''' displaying the first and second sheets in their windows (left and right).
'''
''' Calls: ActivateCell
''' Remarks:
''' â ⢠The height of each window was simply increased significantly
''' (without calculation, by a factor of 2), and only then did they fill
''' the entire height space. Bug, maybe. See: .Height * 2
''' ⢠Const: com.sun.star.awt.PosSize.POSSIZE = 15: Flags the x- and y-coordinate, width and height.
On Local Error GoTo HandleErrors
Dim oDoc, oDisp, oWin1, oWin2
Dim oPosSize As New com.sun.star.awt.Rectangle 'maximized window possize
Dim nWidthHalved& 'oWin1 width halved
Dim nRemainder% 'remainder after division by 2
oDoc = ThisComponent
oDisp = createUnoService("com.sun.star.frame.DispatchHelper")
' 1st Controller.
With oDoc.CurrentController
.ActiveSheet = oDoc.Sheets(0)
Call ActivateCell(.ActiveSheet.getCellByPosition(0, 1))
oWin1 = .Frame.ContainerWindow
With oWin1
.IsMaximized = True
Wait 100: oPosSize = .PosSize
nWidthHalved = oPosSize.Width \ 2: nRemainder = oPosSize.Width - nWidthHalved * 2
.IsMaximized = False
End With
Wait 100
With oPosSize
Rem oWin1.setPosSize .X, .Y, nWidthHalved, .Height * 2, 15
oWin1.setPosSize .X, .Y, nWidthHalved + nRemainder, .Height * 2, 15
End With
oDisp.executeDispatch(.frame, ".uno:NewWindow", "", 0, Array())
End With
' 2nd Controller
With oDoc.CurrentController
oWin2 = .Frame.ContainerWindow
oWin2.IsMaximized = False
Wait 100
With oPosSize
Rem oWin2.setPosSize .X + nRemainder + nWidthHalved, .Y, nWidthHalved, .Height * 2, 15
oWin2.setPosSize .X + nWidthHalved, .Y, nWidthHalved + nRemainder, .Height * 2, 15
End With
.ActiveSheet = oDoc.Sheets(1)
Call ActivateCell(.ActiveSheet.getCellByPosition(0, 1))
End With
oWin1.toFront
Exit Sub
HandleErrors:
Msgbox "Error " & Err & " in line " & Erl & ": " & Error _
, MB_ICONEXCLAMATION, "macro:ArrangeWindowsVertical"
End Sub
Sub ActivateCell(oCell As Object)
Dim oRanges As Object
With ThisComponent
.CurrentController.select(oCell)
' NOTE: Same with .SheetCell or .SheetCellRange doesnât work.
oRanges = .createInstance("com.sun.star.sheet.SheetCellRanges")
' Remove the highlight by passing the empty range collection.
.CurrentController.select(oRanges)
End With
End Sub
Well, Iâm a perfectionist, so I took into account one pixel lost from division (nRemainder).
My screen width is 1853 = 926 + 1 + 926. OS Linux makes so: 927 + 927, with an overlap of 1 pixel in the middle of the screen. However, the windows are glued (stuck together) there.
Try pressing the up/down arrows right after running the macro. In which window does the active cell change?
Right window
My left window becomes active in Ubuntu. You didnât forget at the end of the macro:
oWindow1.toFront
?
Sorry. It works. Letâs stop there. Decision received. Thank you.
Thanks for the interesting topic!