I am trying to get the data in an Excel named range for an F# based add-in. I want to be able to use named ranges anywhere in the workbook, not just a specific worksheet.
let xlRange (xl:Microsoft.Office.Interop.Excel.Application, name:string) =
let name_list = xl.ThisWorkbook.Names:Microsoft.Office.Interop.Excel.Names
let mutable result=null
for n in name_list do
let nn = n :?> Microsoft.Office.Interop.Excel.Name
if nn.Name = name then
let range=nn.RefersToRange
result <- range.Value2 :?> obj[,]
result
The above code works, but I don't like it because I had to use a mutable and imperative style. The problem is the Excel.Names collection doesn't behave very well. For example
let name_seq = Seq.toList name_list
gives
The type 'Names' is not compatible with the type 'seq<'a>'
Is there a more F# idiomatic way of doing this?
Seq.toList (Seq.cast name_list)
, maybe even specify a type (i.e.let name_seq : ... = ...
).