OpenOffice.org Forum at OOoForum.orgThe OpenOffice.org Forum
 
 [Home]   [FAQ]   [Search]   [Memberlist]   [Usergroups]   [Register
 [Profile]   [Log in to check your private messages]   [Log in

Macro that brings cursor back to active cell

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API
View previous topic :: View next topic  
Author Message
Mitsu
General User
General User


Joined: 21 Mar 2008
Posts: 20

PostPosted: Fri Mar 21, 2008 1:06 pm    Post subject: Macro that brings cursor back to active cell Reply with quote

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
View user's profile Send private message
Villeroy
Super User
Super User


Joined: 04 Oct 2004
Posts: 10106
Location: Germany

PostPosted: Fri Mar 21, 2008 1:22 pm    Post subject: Reply with quote

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 https://forum.openoffice.org
Back to top
View user's profile Send private message
uros
Super User
Super User


Joined: 22 May 2003
Posts: 601
Location: Slovenia

PostPosted: Sat Mar 22, 2008 5:10 am    Post subject: Re: Macro that brings cursor back to active cell Reply with quote

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
View user's profile Send private message
Mitsu
General User
General User


Joined: 21 Mar 2008
Posts: 20

PostPosted: Sun Mar 23, 2008 1:06 pm    Post subject: Reply with quote

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
View user's profile Send private message
Villeroy
Super User
Super User


Joined: 04 Oct 2004
Posts: 10106
Location: Germany

PostPosted: Sun Mar 23, 2008 1:24 pm    Post subject: Reply with quote

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 https://forum.openoffice.org
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group