2009-12-04 34 views
0

我正在做一些javascript開發,並發現一個很酷的宏來區域我的代碼(「在Visual Studio中使用#region指令與JavaScript文件」)。我在我的32位盒子上使用了它,並且它第一次工作。 (Visual Studio 2008中SP1,Win7的)64位的Visual Studio宏失敗,COM錯誤

爲了方便的參考宏是:

Option Strict Off 
Option Explicit Off 

Imports System 
Imports EnvDTE 
Imports EnvDTE80 
Imports System.Diagnostics 
Imports System.Collections 

Public Module JsMacros 

    Sub OutlineRegions() 
     Dim selection As EnvDTE.TextSelection = DTE.ActiveDocument.Selection 

     Const REGION_START As String = "//#region" 
     Const REGION_END As String = "//#endregion" 

     DTE.ExecuteCommand("Edit.StopOutlining") 

     selection.SelectAll() 
     Dim text As String = selection.Text 
     selection.StartOfDocument(True) 

     Dim startIndex As Integer 
     Dim endIndex As Integer 
     Dim lastIndex As Integer = 0 
     Dim startRegions As Stack = New Stack() 

     Do 
      startIndex = text.IndexOf(REGION_START, lastIndex) 
      endIndex = text.IndexOf(REGION_END, lastIndex) 

      If startIndex = -1 AndAlso endIndex = -1 Then 
       Exit Do 
      End If 

      If startIndex <> -1 AndAlso startIndex < endIndex Then 
       startRegions.Push(startIndex) 
       lastIndex = startIndex + 1 
      Else 
       ' Outline region ... 
       selection.MoveToLineAndOffset(CalcLineNumber(text, CInt(startRegions.Pop())), text.Length) 
       selection.MoveToLineAndOffset(CalcLineNumber(text, endIndex) + 1, 1, True) 
       selection.OutlineSection() 

       lastIndex = endIndex + 1 
      End If 
     Loop 

     selection.StartOfDocument() 
    End Sub 

    Private Function CalcLineNumber(ByVal text As String, ByVal index As Integer) 
     Dim lineNumber As Integer = 1 
     Dim i As Integer = 0 

     While i < index 
      If text.Chars(i) = vbCr Then 
       lineNumber += 1 
       i += 1 
      End If 

      i += 1 
     End While 

     Return lineNumber 
    End Function 

End Module 

我然後試圖使用相同的宏在兩個單獨的64臺機器(Win7的X64),比相同的其它64位操作系統版本,它無法正常工作。使用Visual Studio宏集成開發環境進行單步執行時,它首次出現DTE.ExecuteCommand(「Edit.StopOutlining」)行出現COM錯誤(錯誤HRESULT E_FAIL已從調用COM組件返回)。

如果我第二次嘗試運行它,我可以在沒有問題的情況下從宏編輯器運行它,但不能在Visual Studio中使用宏瀏覽器的「運行宏」命令運行它。

我審查,沒有發現任何有用的以下文章:

上午我缺少一些愚蠢的東西

+0

還發現代碼窗口也必須被聚焦。如果你說VS的輸出窗口是焦點,那麼這個宏就不會起作用了......在那裏沒有什麼意外,但是它會拋出異常。 – Bruce 2011-03-24 23:38:01

回答

0

只是包裝線

DTE.ExecuteCommand("Edit.StopOutlining") 

在try catch塊和靜噪例外 - 我有一個劇本,正是這樣做的,但一個我有沒有工作 - 努力你和改變上述行後

Try 

    DTE.ExecuteCommand("Edit.StopOutlining") 

Catch ex As Exception 

End Try 

和製作方法公開,我後來去的工具,期權,鍵盤和指定的快捷鍵組合Ctrl鍵男,按Ctrl O到宏觀MyMacros.OutLineRegions和它的作品就像是夢想成真:)

使用Windows 7 64位,Visual Studio 2010 .. YMMV

0

我有類似的問題,並通過查看_applicationObject中的每個命令發現一個命令存在兩次(在我的情況下它是Debug.SaveDumpAs) 一個可用,而不是其他。 「不幸」夠了,我執行命令時總是找錯了。

你必須檢索你想要的命令的GUID和ID,當然,你想要一個可用的命令。 這是C#中的一個例子

 string guid =""; 
     int ID = 0; 
     string NAMEOFCOMMAND = "Edit.StopOutLining"; 

     try 
     { 
      foreach (Command command in _applicationObject.Commands) 
      {      
       if (command.Name == NAMEOFCOMMAND) 
       { 
        if(command.IsAvailable) 
        { 
         guid = command.Guid; 
         ID = command.ID; 
        } 
       } 
      } 
      //The ExecuteCommand method cannot be used to have the right guid and id. I believe though that there is a format you can use to use it anyway. In doubt: use the Raise command which is most likely the same with additional parameters which you can set to null if you don't need them 
      ((DTE)_applicationObject).Commands.Raise(guid, ID, null, null); 

乾杯! :)

相關問題