Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To work with the current selection, you can use the method .getCurrentSelection()

oCurrentSelection = ThisComponent.getCurrentSelection()

However, there is a slight difficulty. A method can return a single cell, a range of cells, or several ranges of cells. Additional analysis and branching of the algorithm will be required for different cases. If you are only interested in the active cell (the one that is surrounded by a black frame), then you can use the following function:

Function ActiveCell(Optional iSheet As Long, Optional oDoc As Variant) As Variant
Dim arrayOfString()
Dim lRow&, lColumn&
Dim tmpString$
Dim oCurrentController
Dim oSheets As Variant
Dim oSheet As Variant
   ActiveCell = Nothing
   On Error GOTO unknownErr
   If IsMissing(oDoc) Then oDoc = ThisComponent 
   If NOT oDoc.SupportsService("") Then Exit Function
   oCurrentController = oDoc.getCurrentController() 
   If IsNull(oCurrentController) Or IsEmpty(oCurrentController) Then Exit Function
   If IsMissing(iSheet) Then 
      oSheet = oCurrentController.getActiveSheet() 
      iSheet = oSheet.getRangeAddress().Sheet 
      If (iSheet < 0) Then Exit Function 
      oSheets = oDoc.getSheets() 
      If (iSheet >= oSheets.getCount()) Then Exit Function 
      oSheet = oSheets.getByIndex(iSheet) 
   tmpString = oCurrentController.getViewData() 
   arrayOfString() = Split(tmpString, ";") 
   If UBound(arrayOfString) < (3 + iSheet) Then Exit Function 
   tmpString = arrayOfString(3 + iSheet) 
   If InStr(tmpString,"+") > 0 Then 
      arrayOfString() = Split(tmpString, "+") 
      arrayOfString() = Split(tmpString, "/") 
   lColumn = CLng(arrayOfString(0)) 
   lRow = CLng(arrayOfString(1)) 
   On Error GOTO 0
   ActiveCell = oSheet.getCellByPosition(lColumn, lRow)
End Function

Example of use:

oCell = ActiveCell()
sText = oCell.getString()
oCell.setString("Old value " & sText)