0

我正在尝试编写一个简单的 Visual Studio 2010 宏来搜索字符串的解决方案(从剪贴板中获取)

到目前为止我所拥有的:

Option Strict Off
Option Explicit Off
Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports EnvDTE90a
Imports EnvDTE100
Imports System.Diagnostics

Public Module RecordingModule

    Sub TemporaryMacro()
        DTE.ExecuteCommand("Edit.FindinFiles")
        DTE.Find.FindWhat = My.Computer.Clipboard.GetText()
        DTE.Find.Target = vsFindTarget.vsFindTargetFiles
        DTE.Find.MatchCase = True
        DTE.Find.MatchWholeWord = False
        DTE.Find.MatchInHiddenText = True
        DTE.Find.PatternSyntax = vsFindPatternSyntax.vsFindPatternSyntaxLiteral
        DTE.Find.SearchPath = "Entire Solution"
        DTE.Find.SearchSubfolders = True
        DTE.Find.FilesOfType = ""
        DTE.Find.ResultsLocation = vsFindResultsLocation.vsFindResults2
        DTE.Find.Action = vsFindAction.vsFindActionFindAll
        If (DTE.Find.Execute() = vsFindResult.vsFindResultNotFound) Then
            Throw New System.Exception("vsFindResultNotFound")
        End If
        DTE.Windows.Item(Constants.vsWindowKindFindReplace).Close()
    End Sub
End Module

不幸的是,它不起作用。当我尝试使用它时,在“DTE.Find.FindWhat = My.Computer.Clipboard.GetText()”行上出现“值不在预期范围内”错误。这是我的第一个视觉工作室宏,所以我有点迷路了。

4

2 回答 2

0

我测试了你的代码,问题是GetText()返回一个空字符串。当您Find.FindWhat使用空字符串设置时,它会引发错误。对于测试,尝试显式设置Find.FindWhat为“hello”之类的字符串文字,看看代码是否仍然崩溃(在我的测试中没有)。

如果不是,那么问题是为什么GetTest()返回一个空字符串。经过一番摸索,我找到了另一个讨论同一件事的线程:

Clipboard.GetText 返回 null(空字符串)

您可能想检查一下(解决方案在 C# 中)。对于 VB 代码,我找到了另一个您可能想尝试的线程:

http://www.dotnetmonster.com/Uwe/Forum.aspx/vs-net-general/10874/Clipboard-GetText-no-longer-working

对我来说似乎是一个烦人的错误。祝你好运!

于 2011-08-26T20:53:17.160 回答
0

GetText()失败了,因为宏未在 STA 线程中运行。这听起来很奇怪,但就是这样。因此,您必须包装GetText()它,以便在 STA 线程内部调用它。这是我目前使用的一些代码:

Private clipString As String = String.Empty

Function GetClipboardText() As String
    clipString = ""
    Dim data = Clipboard.GetDataObject()
    If Not data Is Nothing Then
        clipString = data.GetData(System.Windows.Forms.DataFormats.StringFormat)
    End If
    ' myString = DataObj.GetText(1)
    ' MsgBox(myString)

    ' clipString = _
    ' Clipboard.GetDataObject() _
    ' .GetData(System.Windows.Forms.DataFormats.StringFormat)
End Function

Private Sub StoreClipBoardText(ByVal s As String)
    clipString = Clipboard.GetDataObject().GetData(System.Windows.Forms.DataFormats.StringFormat)
End Sub

我认为如果您想将某些内容放入剪贴板,您也必须这样做。

于 2012-09-11T11:21:45.513 回答