我必须使用 Java 和 JACOB 通过 COM 自动化来自动化 XPedition 中的一些任务。当我尝试设置对象的索引属性时,收到错误:
com.jacob.com.ComFailException: A COM exception has been encountered:
At Invoke of: Pad
Description: 80020003 / Mitglied nicht gefunden.
我使用这个方法来处理索引属性:
public void setIndexedProperty(final String propertyName, final Variant[] indexes,
final Variant propValue) {
Variant[] parameters = new Variant[indexes.length + 1];
for (int i = 0; i < indexes.length; i++) {
parameters[i] = indexes[i];
}
parameters[indexes.length] = propValue;
Dispatch.invokev(activeXComponent, propertyName, Dispatch.Put, parameters,
new int[parameters.length]);
}
并这样称呼它:
ActiveXComponent pad = ... //Result of an other method;
padstack.setIndexedProperty("pad",new Variant[] { new Variant(-1)}, new Variant(pad) );
这会出现上述错误。
我创建了一个vbs脚本来检查代码是否存在一般问题。这给出了预期的结果(属性已设置)。
...
set pad = padstackDBObj.NewPad()
...
set padstack = padstackDBObj.NewPadstack()
set padstack.pad(-1)=pad
...
我检查了作为 java 代码中的值传递的 pad 对象,它是一个有效的对象。 我还可以成功查询 padstack 对象的当前 pad(-1) 属性。
我研究了我传递的参数的类型。我尝试了长,短,字节,字符串,但我得到了同样的错误。
我在其他几个位置设置了索引属性,没有任何问题。这种情况下的新之处在于参数是负整数并且值是对象。因此其中之一可能会导致问题。
使用 oleview 查看类型库表明该方法需要 propputref 调用。
所以正确的 jacob 调用使用
Dispatch.PutRef
而不是 Dispatch.Put
:
public void setIndexedRefProperty(final String propertyName, final Variant[] indexes,
final Variant propValue) {
Variant[] parameters = new Variant[indexes.length + 1];
for (int i = 0; i < indexes.length; i++) {
parameters[i] = indexes[i];
}
parameters[indexes.length] = propValue;
Dispatch.invokev(activeXComponent, propertyName, Dispatch.PutRef, parameters,
new int[parameters.length]);
}