Copy Selection
This example shows how to copy the current selection into a target cell. This assumes you already know the basics of macro creation and management.
' Copies text from the selection cell to the Cell Named "Target"
' A few commented out options show some other possibilities
Sub CopyText
'A starting object/sel
oDoc = ThisComponent
oSourceCell = ThisComponent.CurrentSelection.getCellByPosition(0,0)
'Retrieve the destination sheet by name
'oSheet = oDoc.Sheets.getByName("Sheet2")
'oTargetCell = oSheet.getCellByPosition(1,0)
'get the target by a name
oTargetCell = GetNamedRange( "Target" ).getReferredCells().getCellByPosition(0,0)
'for the Numeric value
'oTargetCel.value = oSourceSel.value
'for the text value
oTargetCell.formula = oSourceCell.formula
End Sub This following function is a simpler version of the GetNamedCell function from basics, it returns the entire range rather than just the cell. This is included just to show variation -- and it is required by the previous function.
'Quick function to return the named range from the document Function GetNamedRange( nameRef as String ) On Error Goto ErrorHandler 'reference the current document Dim oDocument as Object oDocument = ThisComponent 'get the NamedRange wanted Dim oNamedRange as Object oNamedRange = oDocument.NamedRanges.getByName( nameRef ) GetNamedRange = oNamedRange ErrorHandler: if Err <> 0 then GetNamedRange = ReportErr( Err ) end if End Function
Goto Sheet by Name
The following code implements a quick function that switches to another
sheet by name. The example GotoSheet2 is included to be
attached to a Form Button -- form buttons cannot pass parameters so you will
need to create a special function for each set of parameters you want.
' Activates the sheet with the given name Sub GotoSheet( nameRef as String ) oDocument = ThisComponent oView = oDocument.CurrentController oSheet = oDocument.Sheets.getByName( nameRef ) oView.setActiveSheet( oSheet ) End Sub ' Example to activate sheet with specific name Sub GotoSheet2 GotoSheet( "Sheet2" ) End Sub
Goto Prev/Next Sheet
This function was done as a special request. It demonstrates how
to change the currently active sheet. The two shortcut functions
GotoNextSheet and GotoPrevSheet are included since
you'll probably need those more than the generic offset version.
' Activates a sheet either before or after the current active sheet ' This is meant to be called only with a 1 or -1 ' it merely abstracts going forward or backward Sub GotoOffsetSheet( offset as integer ) Dim sheetCount as integer 'Thanks to Marko Schade (from who I got the original next sheet code) oDocument = ThisComponent oView = oDocument.CurrentController oSheets = oDocument.getSheets() sheetCount = oSheets.Count oSheet = oDocument.CurrentController.getActiveSheet() 'Find the current sheet number For i = 0 To sheetCount-1 If (StrComp(oSheet.Name, oSheets(i).Name ) = 0) Then Exit For End If Next i i = i + offset if i < 0 then i = sheetCount-1 end if if i >= sheetCount then i = 0 end if oSheet = oDocument.Sheets.getByIndex( i ) oView.setActiveSheet(oSheet) End Sub ' Activate the next sheet Sub GotoNextSheet GotoOffsetSheet( 1 ) End Sub ' Activate the previous sheet Sub GotoPrevSheet GotoOffsetSheet( -1 ) End Sub
