2011-03-23 9 views
0

Visio에서 현재 선택한 셰이프의 PinX 및 PinY 값을 키보드 시퀀스에서 설정하려고합니다. [Ctrl] + [G]. 이 작업의 목적은 프로그래밍 방식으로 현재 선택된 도형의 핀 좌표를 기반으로 Visio 드로잉에 도형을 놓는 것입니다. 나는 이것을하기 위해 C#과 Microsoft.Office.Interop.Visio API를 사용하고있다. .NET 4.0 (mscorlib.dll은 버전 4.0.30319.1)을 사용하고 있습니다.현재 선택한 셰이프의 PinX, PinY ShapeSheet 셀 값을 읽습니다. Visio, C# COM Interop

은 지금까지 나는이 코드를 가지고 :이 코드는 COMException이 발생

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 
    //... 
} 

을; 조사한 결과, myApp.ActiveWindow.Selection에 요소 [0] (단 하나의 모양 만 선택된 경우 유일한 요소)가 있어도 실제로 해당 요소를 currShape에 저장할 수는 없습니다. 왜 그런지 모르겠습니다. 이상하게도 COMException은 프로그램을 중지시키지 않습니다. 프로그램은 currShape에 할당하려 할 때 메소드에서 빠져 나오지만 실행은 계속됩니다.

다른 방법으로 현재 모양을 얻으려고했습니다. 이것은 이전과는 달리,이 예외가 실행을 중단했기 때문에 이번에는 그것을 볼 수 있었던 것을 제외하고는 동일한 COMException을 발생 시켰습니다.

이 코드 : 나는 "잘못된 선택 식별자가"무엇을 의미하는지 단서가없고, 인터넷 검색은 Visual Basic 및입니다 this 주제를 산출

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: 

:

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"; 
     } 

이 예외를 발생 C#이 아닙니다.

두 가지 질문은 다음과 같습니다.
(1) 여기서 무엇이 잘못됩니까?
(2) 이러한 종류의 조작을 위해 현재 선택된 모양에 액세스하는 올바른 방법은 무엇입니까?

도움 주셔서 감사합니다.

+0

어떤 이유로 든 더 게시 된 답변을 볼 수 없습니다. –

답변

0

저는 과거에는 Visio에서 모양을 선택하는 데 어려움을 겪었습니다. 어떻게 해결했는지는 Selection 객체를 만든 다음 Select 메서드를 사용하여 해결했습니다. 그게 너에게 도움이 될지 모르겠다.

VisGuy.com에서 교차 게시 할 수도 있습니다. 바로 Visio 프로그래밍 전문가가 있습니다.

+0

답변 해 주셔서 감사합니다. 나는 그것이 무엇인지를 알아 냈다. (그리고 전에는 이것을 알아 내지 못했던 것이었다.) –

0

문제가 Selection 컬렉션의 인덱스와 관련이 있는지 궁금합니다.

currShape = myApp.ActiveWindow.Selection.Cast<Shape>().FirstOrDefault() 망가 using System.Linq;으로의 LINQ 라이브러리에 대한 참조를 추가하는 것을 잊지 :

이 사용 해보세요.

+0

답변을 주셔서 감사합니다. 문제는 실제로 Selection 컬렉션의 인덱스와 관련이 있습니다. [1]을 (를) 사용하여 효과적입니다. –

0

예를 들어 [0] 대신 [1]에 액세스해야한다는 것이 드러났습니다.

currShape = myApp.ActiveWindow.Selection[1]; 

Visio에서 Visual Studio에서 시계가 myApp.ActiveWindow.Selection에 사용하여 액세스 할 수 [0], 내가 가진에 하나 개의 객체를했다하더라도 1에서 개체를 매기기 시작하기 때문에 [1].