首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >System.Runtime.InteropServices.COMException:“此操作的源对象不正确”。在检索连接的形状时标识

System.Runtime.InteropServices.COMException:“此操作的源对象不正确”。在检索连接的形状时标识
EN

Stack Overflow用户
提问于 2019-03-19 16:11:13
回答 1查看 227关注 0票数 0

我正在尝试获取页面中每个形状的连接形状。但是我发现了一个奇怪的COM异常--“不适合这个动作的源对象”

下面是我的代码:

代码语言:javascript
复制
using Microsoft.Office.Interop.Visio;

class Program
{
  static void Main(string[] args)
  {
    Application visApp = new Application();
    Document visDoc = visApp.Documents.Open(filePath);

    // Get the first page in the sample drawing.
    page = visDoc.Pages[1];

    foreach (Microsoft.Office.Interop.Visio.Shape shp in page.Shapes)
    {               
        GetConnected(shp, Microsoft.Office.Interop.Visio.VisConnectedShapesFlags.visConnectedShapesOutgoingNodes, "", null, true);
    }
 }


    private static void GetConnected(Microsoft.Office.Interop.Visio.Shape shp, Microsoft.Office.Interop.Visio.VisConnectedShapesFlags whichConnectedShapes, string category, Microsoft.Office.Interop.Visio.Selection selection, bool addToSelection)
     {
        Array aryTargetIDs;

        //getting an exception here during the second iteration of for loop of Main method
        aryTargetIDs = shp.ConnectedShapes(whichConnectedShapes, category); 
     }
  }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-03-19 17:54:00

对于1D形状(即连接器),ConnectedShapes方法会抛出此异常。因此,您只需在调用helper方法之前或在其中包含此检查,如下所示:

代码语言:javascript
复制
using Visio = Microsoft.Office.Interop.Visio

private static void GetConnected(
            Visio.Shape shp, 
            Visio.VisConnectedShapesFlags whichConnectedShapes, 
            string category, 
            Visio.Selection selection, 
            bool addToSelection)
{
    if (shp is null)
    {
        throw new ArgumentNullException();
    }
    if (shp.OneD == 0)
    {   
        Array aryTargetIDs = shp.ConnectedShapes(whichConnectedShapes, category);
        Console.WriteLine($"{shp.Master.Name} ({shp.NameID}) - {String.Join(", ", aryTargetIDs.Cast<object>().ToArray())}");
    }
}

所以给出一组像这样的形状:

上述代码的控制台输出如下所示:

代码语言:javascript
复制
Start/End (Sheet.1) - 2
Decision (Sheet.2) - 4, 6
Subprocess (Sheet.4) - 
Document (Sheet.6) -
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55236187

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档