简体   繁体   English

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

[英]OLE automation: How to check if a variant references an automation object

I would like to know how can i determine, whether a variant is referencing an OLE automation object, or not. 我想知道如何确定变量是否引用OLE自动化对象。

I'm exporting some Excel graphs to Powerpoint. 我正在将一些Excel图形导出到Powerpoint。

I have this code: 我有以下代码:

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');

I know, it could be done by placing the last line of the code (with saveAs) between try...except...end , but i don't feel that approach is good enough. 我知道,可以通过在try...except...end之间放置代码的最后一行(带有saveAs)来完成此操作,但是我认为这种方法不够好。

I was reading about VarIsEmpty , VarIsEmptyParam , Nothing , this question , but i'm not sure about this. 我正在阅读有关VarIsEmptyVarIsEmptyParamNothing的 问题 ,但我对此不确定。

You should use VarIsClear for this test. 您应该使用VarIsClear进行此测试。

Indicates whether the specified variant has an undefined value. 指示指定的变量是否具有未定义的值。 VarIsClear returns true if the given variant's value is undefined. 如果给定变量的值未定义,则VarIsClear返回true。 The value can be undefined for any of several reasons: 出于以下几种原因,可以不确定该值:

  • The Variant may have had its value set to Unassigned. 变量可能已将其值设置为“未分配”。
  • The Variant's value may be an interface type that has been set to nil (Delphi) or NULL (C++). Variant的值可以是已设置为nil(Delphi)或NULL(C ++)的接口类型。
  • The Variant may be a custom variant that returns true from its IsClear method. Variant可能是一个自定义变量,从其IsClear方法返回true。

In all other cases, the function result is false. 在所有其他情况下,函数结果均为false。

Note : Do not confuse an unassigned variant with a Null variant. 注意 :请勿将未分配的变量与Null变量混淆。 A Null variant is still assigned, but has the value Null. 仍分配Null变体,但其值为Null。 Unlike unassigned variants, Null variants can be used in expressions and can be converted to other types of variants. 与未分配的变体不同,Null变体可以在表达式中使用,并且可以转换为其他类型的变体。


However, I question whether or not it is needed. 但是,我质疑是否需要它。 How could it be that PptFile was not assigned? 没有分配PptFile怎么可能? That can only happen if the call to pptApp.Presentations.Open() fails, and that would raise an exception. 只有在对pptApp.Presentations.Open()的调用失败时才会发生,并且会引发异常。 Or am I mis-understanding this? 还是我误会了这一点? I cannot at the present see any scenario in which you could reach the call to PptFile.SaveAs() for which PptFile had not been assigned. 目前,我看不到任何可以调用尚未分配PptFile PptFile.SaveAs()的情况。

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

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