如何使用 OLE 引用内置 MS Excel 类型

发布于 2024-12-18 09:30:07 字数 648 浏览 4 评论 0原文

我的应用程序应该在 Excel 中执行一些简单的操作,例如添加图表、列表对象等。我正在使用 OLE 连接。问题是,某些 Excel 方法采用内置类型(枚举)作为参数。我对提及它们没有任何想法。例如:

WorkBook.ActiveSheet.ListObjects.Add(xlSrcRange, Range("$D$5:$J$15"), , xlNo).Name = "Table1"

xlSrcRangexlNo属于内置枚举。我尝试按以下方式引用它们

ExcelApp.xlSrcRange
ExcelApp.XlListObjectSourceType.xlSrcRange
ExcelApp.XlListObjectSourceType

此代码会导致错误“对象不支持属性或方法ExcelApp.xlSrcRange

New XlListObjectSourceType.xlSrcRange
new xlSrcRange

此代码也会导致错误(未知变量XlListObjectSourceType和xlSrcRange)

我使用QTP,脚本语言是VB-script

my application should perform some simple actions in Excel, like adding charts, list objects and so on. I'm using OLE connection. The problem is, that some Excel methods taking built-in types (enumerations) as arguments. And I have no ideas about referring to them. For example:

WorkBook.ActiveSheet.ListObjects.Add(xlSrcRange, Range("$D$5:$J$15"), , xlNo).Name = "Table1"

xlSrcRange and xlNo belong to the built-in enumeration. I tried to refer to them in a following way

ExcelApp.xlSrcRange
ExcelApp.XlListObjectSourceType.xlSrcRange
ExcelApp.XlListObjectSourceType

this code causes error "Object doesn't support property or method ExcelApp.xlSrcRange"

New XlListObjectSourceType.xlSrcRange
new xlSrcRange

this code causes an error too (unknown variable XlListObjectSourceType and xlSrcRange)

I'm working with QTP and the script language is VB-script

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

擦肩而过的背影 2024-12-25 09:30:07

.wsf 脚本可以通过 Excel.Sheet 引用访问 xl* 常量:

type xlconst.wsf

<?xml version="1.0" standalone="yes" encoding="iso-8859-1" ?>
<package>
 <job id="xlconst">
  <reference object="Excel.Sheet" reference="true"/>
  <script language="VBScript">
   <![CDATA[
' ############################################################################
For Each arg In WScript.Arguments.Unnamed
    WScript.Echo "Const " & arg & " = " & Eval(arg)
Next
' ############################################################################
   ]]>
  </script>
 </job>
</package>

输出:

cscript xlconst.wsf xlNo xlYes

Const xlNo = 2
Const xlYes = 1

普通 VBScript 不能。如果 QTP 仅限于纯 VBScript,则您必须手动添加/定义常量。也许上面的 .wsf 会让这个任务变得更容易。

A .wsf script can access the xl* constants via a Excel.Sheet reference:

type xlconst.wsf

<?xml version="1.0" standalone="yes" encoding="iso-8859-1" ?>
<package>
 <job id="xlconst">
  <reference object="Excel.Sheet" reference="true"/>
  <script language="VBScript">
   <![CDATA[
' ############################################################################
For Each arg In WScript.Arguments.Unnamed
    WScript.Echo "Const " & arg & " = " & Eval(arg)
Next
' ############################################################################
   ]]>
  </script>
 </job>
</package>

output:

cscript xlconst.wsf xlNo xlYes

Const xlNo = 2
Const xlYes = 1

Plain VBScript can't. If QTP is restricted to plain VBScript, you'll have to add/define the constants manually. Perhaps the above .wsf will make this task easier.

暗地喜欢 2024-12-25 09:30:07

使用完全限定名称怎么样?

dim sourceType As Excel.XlListObjectSourceType
sourceType = Excel.XlListObjectSourceType.xlSrcRange

编辑:(在 vb 脚本中)

dim sourceType 
sourceType = 1

How about using fully qualified name?

dim sourceType As Excel.XlListObjectSourceType
sourceType = Excel.XlListObjectSourceType.xlSrcRange

EDIT: (in vbscript)

dim sourceType 
sourceType = 1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文