Application.ActiveWindow.Selection.ShapeRange 没有选择时抛出异常

Application.ActiveWindow.Selection.ShapeRange Throw an Exception when nothing selected

我试图在没有 try/catch 的情况下处理异常以获取选定的形状,下面的代码抛出异常 !=null 在这里不起作用,任何想法

PowerPoint.Selection selection = Application.ActiveWindow.Selection;
PowerPoint.ShapeRange shapeRange = selection?.ShapeRange; // Throw Ex 
if (shapeRange != null)
{
    // do some code
}

异常消息:

Selection (unknown member) : Invalid request. Nothing appropriate is currently selected.

Microsoft 对 PowerPoint 保持沉默,但是 Excel:"The returned object type depends on the current selection (for example, if a cell is selected, this property returns a Range object). The Selection property returns Nothing if nothing is selected."

https://docs.microsoft.com/en-us/office/vba/api/excel.application.selection

我希望 PowerPoint 也一样。

我用 VB 做的更多 Excel 而不是用 c# 做的 PowerPoint,但我想你可以尝试类似的东西:

if (selection.GetType == null)

希望这能为您提供进一步调查的线索