将visio vba方法转换为c#

时间:2016-01-20 01:42:09

标签: c# vba visio visio-vba

您好我想将此vba方法转换为C#。我正在尝试获取选择的ID并将其打印出来。特别是我无法将GetIDs()方法(visio vba中的内置方法)转换为C#。

Public Sub getCapabilityRectIDs()

    Dim vsoSelection1 As Visio.Selection
    Dim selectionIDs() As Long


    Set vsoSelection1 = Application.ActiveWindow.Page.CreateSelection(visSelTypeByLayer, visSelModeSkipSuper, "Capability")
    Application.ActiveWindow.Selection = vsoSelection1
    Call vsoSelection1.GetIDs(selectionIDs)
    For i = 0 To UBound(selectionIDs)
        Debug.Print selectionIDs(i)
    Next i
End Sub

这是我到目前为止在C#

中所拥有的
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using Visio = Microsoft.Office.Interop.Visio;


class Program
    {
        static void Main(string[] args)
        {
            //create the object that will do the drawing
             VisioDrawer Drawer = new VisioDrawer();
             Drawer.selectShpLayer("Capability");

        }
    }

    class VisioDrawer
    {
        public Visio.Application VisApp;
        public static Visio.Page acPage;
        public Visio.Selection LayerSelection;
        public VisioDrawer()
        {   

            //create the application
            VisApp = new Visio.Application();
            VisApp.Documents.Open(@"............. - abc.vsdm");
            ActiveDoc = VisApp.ActiveDocument;
            acPage = VisApp.ActivePage;
        }



         public void selectShpLayer (string layerName){
            Int i = 0;
            long[] lngRowIDs;
            //this selects the shapes of the selected layer
            LayerSelection = acPage.CreateSelection(Microsoft.Office.Interop.Visio.VisSelectionTypes.visSelTypeByLayer, Microsoft.Office.Interop.Visio.VisSelectMode.visSelModeOnlySuper,layerName);
            LayerSelection.GetIDs(lngRowIDs);
            for (i = 0; i < lngRowIDs.Length; i++)
            {
                Debug.Write(lngRowIDs[i]);
            }
        }
    }

提前致谢!

1 个答案:

答案 0 :(得分:0)

我创建了一个visio doc并绘制了3个简单的形状。然后我创建了一个名为&#34; cool&#34;并添加了这3个形状。

我对形状选择不够了解,你有visSelModeOnlySuper可能对你有用,但对我创建的情况不起作用。默认值为visSelModeSkipSuper(对我有用)

以下是介绍如何使用createSelection的API页面: https://msdn.microsoft.com/en-us/library/office/ff765565.aspx

以下是我的示例代码:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using Visio = Microsoft.Office.Interop.Visio;


class Program
{
    static void Main(string[] args)
    {
        //create the object that will do the drawing
        VisioDrawer Drawer = new VisioDrawer();
        Drawer.selectShpLayer("cool");

    }
}

class VisioDrawer
{
    public Visio.Application VisApp;
    public static Visio.Page acPage;
    public Visio.Selection LayerSelection;
    public VisioDrawer()
    {

        //create the application
        VisApp = new Visio.Application();
        VisApp.Documents.Open(@"c:\temp\trial.vsdm");
        acPage = VisApp.ActivePage;
    }



    public void selectShpLayer(string layerName)
    {
        int i = 0;
        int[] lngRowIDs;
        Array lngRowIDArray;
        //this selects the shapes of the selected layer
        LayerSelection = acPage.CreateSelection(Microsoft.Office.Interop.Visio.VisSelectionTypes.visSelTypeByLayer, Microsoft.Office.Interop.Visio.VisSelectMode.visSelModeSkipSuper, layerName);
        LayerSelection.GetIDs(out lngRowIDArray);
        lngRowIDs = (int[])lngRowIDArray;
        for (i = 0; i < lngRowIDs.Length; i++)
        {
            Debug.Write("Object ID: " + lngRowIDs[i].ToString() + "\n");
        }
    }
}

它产生了这个输出:

Object ID: 1
Object ID: 2
Object ID: 3

如果将调试行更改为:

Debug.Write("Object ID: " + lngRowIDs[i].ToString() + " -- " + acPage.Shapes.ItemFromID[i].Name + "\n");

输出变得更有用:

Object ID: 1 -- ThePage
Object ID: 2 -- Circle
Object ID: 3 -- Rectangle