我正在做一些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中使用宏瀏覽器的「運行宏」命令運行它。
我審查,沒有發現任何有用的以下文章:
- #1:Visual Studio 2008中宏只能從宏IDE配合工作,而不是宏資源管理
- Recorded macro does not run; Failing on DTE.ExecuteCommand
上午我缺少一些愚蠢的東西
還發現代碼窗口也必須被聚焦。如果你說VS的輸出窗口是焦點,那麼這個宏就不會起作用了......在那裏沒有什麼意外,但是它會拋出異常。 – Bruce 2011-03-24 23:38:01