【发布时间】:2009-12-04 00:26:13
【问题描述】:
我正在做一些 javascript 开发,并找到了一个很酷的宏来对我的代码进行区域划分(“在 Visual Studio 中使用 #region 指令和 JavaScript 文件”)。我在我的 32 位盒子上使用了这个,它第一次工作。 (Visual Studio 2008 SP1, Win7)
为了便于参考,宏是:
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 宏 IDE 单步执行它,它在 DTE.ExecuteCommand("Edit.StopOutlining") 行第一次失败并出现 COM 错误(错误 HRESULT E_FAIL 已从调用返回COM 组件)。
如果我尝试第二次运行它,我可以从宏编辑器运行它而没有问题,但不能从 Visual Studio 中使用宏资源管理器“运行宏”命令运行它。
我查看了以下文章,但没有发现任何有用的信息:
- Stackoverflow:Visual Studio 2008 宏仅适用于宏 IDE,不适用于宏资源管理器
- Recorded macro does not run; Failing on DTE.ExecuteCommand
我错过了什么愚蠢的东西吗?
【问题讨论】:
-
还发现代码窗口也必须聚焦。比如说,如果你让 VS 输出窗口处于焦点位置,那么宏将不起作用……这并不奇怪,但它会引发异常。
标签: visual-studio visual-studio-2008 64-bit macros