C# 访问嵌套连接器形状的BeginConnectedShape和EndConnectedShape时发生异常(灾难性故障)
我正在创建一个PowerPoint加载项,它应该从外部文件读取信息,并将这些信息集成到当前幻灯片中,其中包含形状,主要是文本框形状和连接文本框的连接器形状。每个连接器位于一个组中,包含连接器形状和一个文本框,用作连接器的标签。下图描述了此场景 我手头的任务涉及访问上述组中包含的连接器形状的属性,即BeginConnectedShape和EndConnectedShape。下面是一些代码,应该可以做到这一点:C# 访问嵌套连接器形状的BeginConnectedShape和EndConnectedShape时发生异常(灾难性故障),c#,powerpoint,office-interop,office-addins,C#,Powerpoint,Office Interop,Office Addins,我正在创建一个PowerPoint加载项,它应该从外部文件读取信息,并将这些信息集成到当前幻灯片中,其中包含形状,主要是文本框形状和连接文本框的连接器形状。每个连接器位于一个组中,包含连接器形状和一个文本框,用作连接器的标签。下图描述了此场景 我手头的任务涉及访问上述组中包含的连接器形状的属性,即BeginConnectedShape和EndConnectedShape。下面是一些代码,应该可以做到这一点: Shape groupShape = Globals.ThisAddIn.Applic
Shape groupShape = Globals.ThisAddIn.Application.ActiveWindow.Selection.ShapeRange[1];
//check if groupshape actually is a group
if (groupShape.Type == Microsoft.Office.Core.MsoShapeType.msoGroup) {
//iterate over all elements within groupShape
foreach (Shape childShape in groupShape.GroupItems)
{
if (childShape.Connector == Microsoft.Office.Core.MsoTriState.msoTrue)
{
//if the shape is a connector, retrieve source and target of the connector
Shape source = childShape.ConnectorFormat.BeginConnectedShape;
Shape target = childShape.ConnectorFormat.EndConnectedShape;
//Do something with source and target.
}
}
}
在选择包含正确连接的连接件形状(即两端连接到其他形状的定位点)的组时运行此代码会产生以下异常
System.Runtime.InteropServices.COMException wurde nicht von Benutzercode behandelt.
HResult=-2147418113
Message=Catastrophic failure (Exception from HRESULT: 0x8000FFFF (E_UNEXPECTED))
Source=HandoverAddin
ErrorCode=-2147418113
StackTrace:
at Microsoft.Office.Interop.PowerPoint.ConnectorFormat.get_BeginConnectedShape()
at MyAddin.Ribbon.button1_Click(Object sender, RibbonControlEventArgs e) in Ribbon.cs:line 56
at Microsoft.Office.Tools.Ribbon.RibbonPropertyStorage.ControlActionRaise(IRibbonControl control)
at Microsoft.Office.Tools.Ribbon.RibbonPropertyStorage.ButtonClickCallback(RibbonComponentImpl component, Object[] args)
at Microsoft.Office.Tools.Ribbon.RibbonManagerImpl.Invoke(RibbonComponentCallback callback, Object[] args)
at Microsoft.Office.Tools.Ribbon.RibbonMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at Microsoft.Office.Tools.Ribbon.RibbonManagerImpl.System.Reflection.IReflect.InvokeMember(String name, BindingFlags invokeAttr, Binder binder, Object target, Object[] args, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParameters)
InnerException:
执行childShape.ConnectorFormat.BeginConnectedShape
时引发此异常childShape.ConnectorFormat.EndConnectedShape
也会引发此异常。ConnectorFormat
中的其他操作工作正常(即begindconnect
成功断开连接器的起始连接)
shape.ConnectorFormat.BeginConnectedShape
如果连接器不包含在组中,则可以正常工作。在我的情况下,连接器和相关标签必须分组在一起
我还观察到,在断开启动的连接器上访问BeginConnectedShape
会引发未经授权的访问异常
,无论该连接器是否包含在组中。这似乎是意料之中的行为
上面列出的异常似乎是某种内部异常,在正常情况下不应该出现这种异常。因此,我检查了所有可用的更新,但没有找到Office 2010和Visual Studio 2010 Professional的更新
在此问题上的任何帮助、可能的修复或解决方法都将不胜感激
编辑:使用VBA实现相同的功能也会导致代码为8000FFFF的错误(如上所述)。以下是一个粗略的VBA版本,可能会有所帮助: 我认为它基本上与您的相同,只是增加了一个测试: 如果是osh.连接器 否则,它会引发错误,因为它正在查看组中的每个形状,但只有连接器具有.ConnectorFormat及其关联属性
Dim groupshape As Shape
Dim oSh As Shape
Dim oBgnShape As Shape
Dim oEndShape As Shape
Set groupshape = ActiveWindow.Selection.ShapeRange(1)
If groupshape.Type = msoGroup Then
For Each oSh In groupshape.GroupItems
**If oSh.Connector Then**
Set oBgnShape = oSh.ConnectorFormat.BeginConnectedShape
Debug.Print oBgnShape.Left
Set oEndShape = oSh.ConnectorFormat.EndConnectedShape
End If
Next
End If
该错误消息仅评估错误报告的质量。您应该检查BeginConnect和EndConnected属性,以确保它们实际上是连接的。在本例中,BeginConnect和EndConnected都返回true。我的测试表明这是PowerPoint中的一个真正错误。我也这样做了,尽管用c#编写时看起来有点不同。啊,我现在明白了。很抱歉这里不是一个C#家伙。你能发布你的VBA版本吗?