繁体   English   中英

F#Excel UsedRange未定义

[英]F# Excel UsedRange is not Defined

错误:“未定义字段,构造函数或成员'UsedRange'”。

代码摘录:

open Microsoft.Office.Interop.Excel
open Microsoft.Office.Tools.Excel // Onorio's excellent suggestion didn't fix code until this line was added.
// Once the above line was added, Onorio's suggestion worked,
// but now F# says "The field, constructor or member 'EntireColumn' is not defined".

let xl = ApplicationClass()
xl.Workbooks.OpenText(...
let wb = xl.Workbooks.Item(1)
let ws = wb.ActiveSheet :?> Worksheet // Per Onorio's suggestion.

ws.UsedRange.EntireColumn.AutoFit()
// "The field, constructor or member 'EntireColumn' is not defined".

// Here's another example of UsedRange now working,
// but Properties and Methods of UsedRange not working:
// (uniqueID is simply a Boolean set earlier based on ini file values)
if uniqueID then
   xl.Range("A1").EntireColumn.Insert() |> ignore
   xl.Range("A1").Value <- "UniqueID"
   xl.Range("A2").Value <- 1
   xl.Range("A2").AutoFill(xl.Range("A2:A" + ws.UsedRange.Rows.Count),
                           XlAutoFillType.xlFillSeries) |> ignore
// Here it's "The field, constructor or member 'Rows' is not defined".

谢谢你的帮助!

尝试添加此:

let wb = xl.Workbooks.Item(1)
let actSheet = wb.ActiveSheet :?> Worksheet  //downcast obj to Worksheet
actSheet.UsedRange.EntireColumn.AutoFit()

wb.ActiveSheet返回一个“obj”,您需要将其向下转换为工作表,以便正确解析UsedRange方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM