OLE自动化:如何检查变量是否引用自动化对象

bee*_*win 1 delphi powerpoint ole ole-automation delphi-5

我想知道如何确定变量是否引用OLE自动化对象.

我正在将一些Excel图表导出到Powerpoint.

我有这个代码:

var PptFile: Variant;

....

// PptFile _might_ be initialized:

PptFile:=pptApp.Presentations.Open(pptFilename);

// It depends on whether the export has items which need to be exported to 
// Powerpoint or not

....

// I would like to determine if PptFile does reference an OLE automated object or not
PptFile.SaveAs(excelFileName+'.pptx');
Run Code Online (Sandbox Code Playgroud)

我知道,可以通过放置代码的最后一行(使用saveAs)来完成try...except...end,但我觉得这种方法不够好.

我在读有关VarIsEmpty,VarIsEmptyParam,没有什么,这个问题,但我不知道这件事.

Dav*_*nan 6

你应该VarIsClear用于这个测试.

指示指定的变量是否具有未定义的值.如果给定变量的值未定义,则VarIsClear返回true.由于以下几个原因,该值可能未定义:

  • Variant可能将其值设置为Unassigned.
  • Variant的值可以是已设置为nil(Delphi)或NULL(C++)的接口类型.
  • Variant可以是从其IsClear方法返回true的自定义变体.

在所有其他情况下,函数结果为false.

注意:不要将未分配的变体与Null变体混淆.仍然分配了Null变量,但其值为Null.与未分配的变体不同,Null变体可以在表达式中使用,并且可以转换为其他类型的变体.


但是,我怀疑是否需要它.怎么可能PptFile没有分配?只有在调用pptApp.Presentations.Open()失败时才会发生这种情况,这会引发异常.或者我误解了这个?我不能在目前看在你能达到的通话任何情况下PptFile.SaveAs()对这些PptFile尚未分配.