2011-03-23 45 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

到目前爲止,我有這樣的代碼:

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,除了這次我能夠看到它,因爲這個異常停止執行,不像以前那樣。

此代碼:

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

導致此異常:

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)訪問當前選擇的形狀以進行這種操作的正確方法是什麼?

感謝您的任何幫助。

+0

由於某種原因,我再也看不到任何已發佈的答案... – 2011-03-24 03:04:16

回答

0

我遇到過在選擇Visio形狀時遇到的麻煩。我如何解決它是通過創建一個選擇對象,然後使用Select方法。我不確定這是否會對您有所幫助。

另外,您可能會在VisGuy.com處發佈此信息。這就是Visio可編程性專家的地方。

+0

感謝您的回答。我想清楚它是什麼(並且它已經適應了我,以前沒有想到這一點)。 – 2011-04-05 18:29:52

0

我不知道這個問題是否與Selection集合的索引有關。

試試這個:

currShape = myApp.ActiveWindow.Selection.Cast<Shape>().FirstOrDefault() 不要忘記using System.Linq;添加到LINQ庫的引用。

+0

感謝您的回答,問題_was_確實與Selection集合的索引有關。使用[1]工作。 – 2011-04-05 18:33:46

0

事實證明,我需要訪問[1]而不是[0],例如,

currShape = myApp.ActiveWindow.Selection[1]; 

因爲Visio啓動從1編號對象即使在Visual Studio中觀看上myApp.ActiveWindow.Selection曾在[0],我只有一個對象用來訪問它[1]。

相關問題