Sheet name vs. sheet index

Hi there,

I need a macro that check if a sheet exists by its index instead of its name

For example:

oSheets = ThisComponent.Sheets()

If oSheets.hasByName(“Invoice”) Then

[…]

works fine

Instead I need something like this (that obviously doesn’t work):

If oSheets.hasByIndex(2) Then

[…]

Anyone can help me?

Thanks.

Sub Main
target=2 rem looking for sheet number 2 (not sheet index 2)
oSheets=ThisComponent.Sheets()
i=oSheets.getcount() rem i is the number of sheets
if (i >= target) then
   msgbox "Found it"
else
   msgbox "Not found"
end if   
End Sub

@Max74, Did @w_whalley’s answer work for you? Please let us know so we can resolve this question.