首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从当前所选形状读取PinX、PinY ShapeSheet单元格值: Visio、C# COM互操作

从当前所选形状读取PinX、PinY ShapeSheet单元格值: Visio、C# COM互操作
EN

Stack Overflow用户
提问于 2011-03-24 00:52:17
回答 3查看 3.5K关注 0票数 0

我正在尝试在键盘序列上为Visio中当前选定的形状设置PinX和PinY的值,例如Ctrl+G。此操作的目的是根据当前选定形状的图钉坐标以编程方式将形状拖放到Visio绘图上。我使用C#和Microsoft.Office.Interop.Visio应用编程接口来完成这项工作。我使用的是.NET 4.0 (mscorlib.dll是4.0.30319.1版)。

到目前为止,我有以下代码:

代码语言:javascript
复制
Application myApp; // the reference to the Visio Application instance, which is passed into this class via constructor
Shape currShape; // a global variable for this class

//... down to the method in question
void app_KeyUp(int KeyCode, int KeyButtonState, ref bool CancelDefault)
{
    currShape = myApp.ActiveWindow.Selection[0];
    String xCoord = currShape.get_Cells("PinX").Formula;
    String yCoord = currShape.get_Cells("PinY").Formula;

    //handle keyboard events here
    //...
}

这段代码产生了一个COMException;经过研究后发现,即使myApp.ActiveWindow.Selection有一个元素0,我也不能真正将该元素存储到currShape中。我不知道这是为什么。奇怪的是,COMException不会导致程序停止。当尝试向currShape赋值时,程序退出该方法,但执行仍在继续。

我尝试用另一种方法获取当前的形状;这引发了相同的COMException,除了这一次我能够看到它,因为这个异常停止了执行,这与前一个不同。

这段代码:

代码语言:javascript
复制
public void test()
        {
            currShape = myApp.ActiveWindow.Selection[0];
            String x = currShape.Shapes[1].get_Cells("PinX").Formula;
            currShape.Shapes[1].get_Cells("PinX").FormulaForce = "5";
        }

导致此异常的原因:

代码语言:javascript
复制
System.Runtime.InteropServices.COMException was unhandled
  Message="\n\nInvalid selection identifier."
  Source="Microsoft Visio"
  ErrorCode=-2032465753
  StackTrace:
       at Microsoft.Office.Interop.Visio.SelectionClass.get_Item(Int32 Index)
       at WindowsFormsApplication4.Handler.test() in C:\Users\pvs5x\Documents\Visual Studio 2008\Projects\ACCESS(1)\Handler.cs:line 91
       at WindowsFormsApplication4.Form3.changeColorToolStripMenuItem_Click(Object sender, EventArgs e) in C:\Users\pvs5x\Documents\Visual Studio 2008\Projects\ACCESS(1)\OpenSafetyCase.cs:line 355
       at System.Windows.Forms.ToolStripItem.RaiseEvent(Object key, EventArgs e)
       at System.Windows.Forms.ToolStripMenuItem.OnClick(EventArgs e)
       at System.Windows.Forms.ToolStripItem.HandleClick(EventArgs e)
       at System.Windows.Forms.ToolStripItem.HandleMouseUp(MouseEventArgs e)
       at System.Windows.Forms.ToolStripItem.FireEventInteractive(EventArgs e, ToolStripItemEventType met)
       at System.Windows.Forms.ToolStripItem.FireEvent(EventArgs e, ToolStripItemEventType met)
       at System.Windows.Forms.ToolStrip.OnMouseUp(MouseEventArgs mea)
       at System.Windows.Forms.ToolStripDropDown.OnMouseUp(MouseEventArgs mea)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
       at System.Windows.Forms.ToolStrip.WndProc(Message& m)
       at System.Windows.Forms.ToolStripDropDown.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(Form mainForm)
       at WindowsFormsApplication4.Program.Main() in C:\Users\pvs5x\Documents\Visual Studio 2008\Projects\ACCESS(1)\Program.cs:line 20
       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

我不知道“无效的选择标识符”是什么意思,谷歌只给出this主题,这是针对Visual Basic的,而不是C#的。

我的两个问题是:

(1)这里出了什么问题?

(2)对于这种操作,访问当前所选形状的正确方式是什么?

谢谢你的帮助。

EN

回答 3

Stack Overflow用户

发布于 2011-03-30 02:27:53

我以前在Visio中选择形状时遇到过问题。我解决这个问题的方法是创建一个Selection对象,然后使用Select方法。我不确定这对你有没有帮助。

另外,你可以在VisGuy.com上交叉发布这篇文章。这就是Visio可编程性专家所在的地方。

票数 0
EN

Stack Overflow用户

发布于 2011-04-03 17:13:56

我想知道这个问题是否与精选集合的索引有关。

试一试:

currShape = myApp.ActiveWindow.Selection.Cast<Shape>().FirstOrDefault()不要忘记通过using System.Linq;添加对Linq库的引用。

票数 0
EN

Stack Overflow用户

发布于 2011-04-06 02:32:48

结果是我需要访问1,而不是,例如

代码语言:javascript
复制
currShape = myApp.ActiveWindow.Selection[1];

因为Visio从1开始对对象进行编号。即使myApp.ActiveWindow.Selection上的Visual Studio中的手表只有一个对象,我也必须使用1来访问它。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5408814

复制
相关文章

相似问题

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