clearContents method not found

I’m trying to write a subroutine which would erase contents (numeric values and strings) of an existing named range. When I run it, I get an error saying that the clearContents property or method is not found. What am I doing wrong here?

sub ClearMyNamedRange()
	dim currDocRanges as object
	dim currRange as object

	currDocRanges = ThisComponent.NamedRanges
	currRange = currDocRanges.getByName("_ItemList")
	currRange.clearContents(5)
end sub

Any suggestions would be greatly appreciated.

1 Like

Add ref to cell range
currRange.ReferredCells

currRange is a bad name of variable → e.g. oNamedRg

NamedRange & DatabaseRange refer both to CellRange

Awesome, thank you.

I’m pretty new to LO and Basic (although pretty well versed in VBA - switched for moral reasons). Nevertheless, in case of LO Basic where would I find reference to objects classes, properties, methods, etc? Yes, it’s easier to just ask, but sometimes looking for stuff yourself can be very rewarding as well.

Download Bernard Marcelly’s XRay OOoBasic tool


Click the button inside the ODT file to install the library. The library can be loaded by expanding (unfolding) the corresponding folder every time you start LO or load it in code as shown below.
BasicLibraries.loadLibrary("XrayTool")
Xray ThisComponent

Wonderful, thanks again :slight_smile:

NamedRanges.getByName() obtains a sheet::XNamedRange interface that is not a cell range and has no clearContents() method. If you want a cell range that is defined by the name _ItemList then use either

currRange = ThisComponent.NamedRanges.getByName("_ItemList").getReferredCells()

(or its BASIC shorthand)

currRange = ThisComponent.NamedRanges("_ItemList").ReferredCells

or

currRange = ThisComponent.Sheets(0).getCellRangeByName("_ItemList")

where sheet::XSpreadsheet::getCellRangeByName() returns a table::XCellRange interface.

See API docs