开发者

Having trouble writing a simple VS2010 macro

I'm trying to write a simple visual studio 2010 macro to search the solution for a string (gotten from the clipboard)

what I have so far:

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 = vsFindTarge开发者_开发百科t.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

unfortunately, it doens't work. When I try using it, I get a 'Value does not fall in expected range' error on the line 'DTE.Find.FindWhat = My.Computer.Clipboard.GetText()'. This is my first visual studio macro ever, so I'm kind of lost.


I tested your code and the problem is that GetText() is returning an empty string. When you set Find.FindWhat with an empty string it throws the error. For a test, try explicitly setting Find.FindWhat to a string literal like "hello" and see if the code still crashes (in my test it did not).

If not, then the question is why GetTest() returns an empty string. After some poking around, I found another thread which discusses this same thing:

Clipboard.GetText returns null (empty string)

You might want to check that out (the solution is in C#). For VB code, I found another thread that you might want to try:

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

Looks like an annoying bug to me. Good luck!


Your GetText() is failing because macros are not run in an STA thread. That sounds weird but it is like this. Therefore you have to wrap the GetText() so it is called inside of a STA thread. Here is some code I use currently:

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

I think you have to do the same if you want to put something into the clipboard.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜