| View previous topic :: View next topic |
| Author |
Message |
Mitsu General User

Joined: 21 Mar 2008 Posts: 20
|
Posted: Fri Mar 21, 2008 1:06 pm Post subject: Macro that brings cursor back to active cell |
|
|
I have macros that changes a calc sheet (adds or removes rows, changes numbers etc.). Now I want the cursor to go back to wherever it happend to be when I started the macro. From OpenOffice.org Macros Explained/Pitonyak, p. 371 I found the macro below that prints the address (location) of the starting cell (AvtiveCell), but it does not bring the cursor back there. I inserted this code into my macro and there it also prints a message to the screen about where the cursor was at the start.
sub RetrieveTheActiveCell()
REM Author Paolo Mantovani
REM email: mantovani.paolo@tin.it
REM from p. 371-372 of Pitonyak
Dim oOldSelection 'The original selection of cell ranges
Dim oRanges'A blank range created by the document
Dim oActiveCell'The current active cell
Dim oConv' The cell address conversion service
Dim oDoc
oDoc = ThisComponent
REM store the current selection
oOldSeleceion = oDoc.CurrentSelection
REM Create an empty SheetCellRanges service and then select it
REM this leaves ONLY the active cell selected
oRanges = oDoc.createInstance("com.sun.star.sheet.SheetCellRanges")
oDoc.CurrentController.Select(oRanges)
REM get the active cell
oActiveCell = oDoc.CurrentSelection
here comes the code for changing the sheet
oConv = oDoc.createInstance("com.sun.star.table.CellAddressConversion")
oConv.Address = oActiveCell.getCellAddress
Print oConv.UserInterfaceRepresentation
print oConv.PersistentRepresentation
REM Restore the old selection, but loose the previously actve cell
oDoc.CurrentController.Select(oOldSelection)
end sub
Can anybody help me make the macro bring the cursor back to the starting cell inrtead of just printing its address? |
|
| Back to top |
|
 |
Villeroy Super User


Joined: 04 Oct 2004 Posts: 10065 Location: Germany
|
Posted: Fri Mar 21, 2008 1:22 pm Post subject: |
|
|
As far as I know, there is no way to activate any other cell within a range selection but the first one. _________________ Rest in peace, oooforum.org
Get help on http://forum.openoffice.org |
|
| Back to top |
|
 |
uros Super User


Joined: 22 May 2003 Posts: 601 Location: Slovenia
|
Posted: Sat Mar 22, 2008 5:10 am Post subject: Re: Macro that brings cursor back to active cell |
|
|
Hi Mitsu!
I think there is a typo in variable names...
| Mitsu wrote: | ...
REM store the current selection
oOldSeleceion = oDoc.CurrentSelection
...
REM Restore the old selection, but loose the previously actve cell
oDoc.CurrentController.Select(oOldSelection)
...
|
Uros |
|
| Back to top |
|
 |
Mitsu General User

Joined: 21 Mar 2008 Posts: 20
|
Posted: Sun Mar 23, 2008 1:06 pm Post subject: |
|
|
| Villeroy wrote: | | As far as I know, there is no way to activate any other cell within a range selection but the first one. |
Hi, I got the cod working, after correcting a typo.
The macro now reads:
sub RetrieveTheActiveCell()
REM Author Paolo Mantovani
REM email: mantovani.paolo@tin.it
REM from p. 371-372 of Pitonyak
Dim oOldSelection 'The original selection of cell ranges
Dim oRanges'A blank range created by the document
Dim oActiveCell'The current active cell
Dim oConv' The cell address conversion service
Dim oDoc
oDoc = ThisComponent
REM store the current selection
oOldSelection = oDoc.CurrentSelection ' the typo was in this line
REM Create an empty SheetCellRanges service and then select it
REM this leaves ONLT the active cell selected
oRanges = oDoc.createInstance("com.sun.star.sheet.SheetCellRanges")
oDoc.CurrentController.Select(oRanges)
REM get the active cell
oActiveCell = oDoc.CurrentSelection
‘ here comes the code that changes the calc sheet (adds or removes rows, changes numbers etc.)
oConv = oDoc.createInstance("com.sun.star.table.CellAddressConversion")
oConv.Address = oActiveCell.getCellAddress
Print oConv.UserInterfaceRepresentation
print oConv.PersistentRepresentation
REM Restore the old selection, but loose the previously actve cell
oDoc.CurrentController.Select(oOldSelection)
'MsgBox oOldSelection,' oRanges, oConv
end sub
Try it, it works!
Regards,
Mitsu
T |
|
| Back to top |
|
 |
Villeroy Super User


Joined: 04 Oct 2004 Posts: 10065 Location: Germany
|
Posted: Sun Mar 23, 2008 1:24 pm Post subject: |
|
|
Yes, but you can not restore the previous selection exactly if it was more than one cell with the active cell somewhere behind the first one.
Another problem is the inconsistency between Null and active cell:
| Code: |
' select active cell only without highlighting:
oView.select(Null)
' highlight a cell:
oView.select(oCellObj)
' gets the active cell indistinguishable from a single highlighted cell:
oView.getSelection()
|
_________________ Rest in peace, oooforum.org
Get help on http://forum.openoffice.org |
|
| Back to top |
|
 |
|