简体   繁体   English

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

[英]How could I refer to built-in MS Excel type with OLE

my application should perform some simple actions in Excel, like adding charts, list objects and so on. 我的应用程序应该在Excel中执行一些简单的操作,例如添加图表,列出对象等。 I'm using OLE connection. 我正在使用OLE连接。 The problem is, that some Excel methods taking built-in types (enumerations) as arguments. 问题在于,某些Excel方法采用内置类型(枚举)作为参数。 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. xlSrcRangexlNo属于内置枚举。 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 " 此代码导致错误“对象不支持属性或方法ExcelApp.xlSrcRange

New XlListObjectSourceType.xlSrcRange
new xlSrcRange

this code causes an error too (unknown variable XlListObjectSourceType and xlSrcRange) 此代码也会导致错误(未知变量XlListObjectSourceType和xlSrcRange)

I'm working with QTP and the script language is VB-script 我正在使用QTP,脚本语言是VB脚本

A .wsf script can access the xl* constants via a Excel.Sheet reference: .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>

output: 输出:

cscript xlconst.wsf xlNo xlYes

Const xlNo = 2
Const xlYes = 1

Plain VBScript can't. 普通的VBScript不能。 If QTP is restricted to plain VBScript, you'll have to add/define the constants manually. 如果QTP限于纯VBScript,则必须手动添加/定义常量。 Perhaps the above .wsf will make this task easier. 上面的.wsf也许会使此任务更容易。

How about using fully qualified name? 如何使用完全限定名称?

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

EDIT: (in vbscript) 编辑:(在vbscript中)

dim sourceType 
sourceType = 1

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

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