Helixoft Blog

Peter Macej - lead developer of VSdocman - talks about Visual Studio tips and automation

Multiline Search and Replace in Visual Studio

UPDATE 3. March 2014: I have converted the macro to a VS extension which works in VS 2013-2005.

I was cleaning my older VB .NET project some time ago. I just wanted to "refactor" some code to look more readable. All I needed was several replaces of multiline text. VS 2002, 2003 nor 2005 is unable to perform multiline search/replace. So I wrote a macro that can do it. Multi-line search and replace dialog

As you know, you can use regular expressions (regex in next text) in standard Find/Search. See Regular Expressions in MSDN for more info. You can define newline by \n sequence in regex . So theoretically, all you need to do is to replace newline by \n in Find what and Replace with boxes and select Regular Expressions in Use box. But that means that you cannot just simply copy and paste the text to this dialog.

There's another problem. Regex uses many characters which have special meaning, for example * + [ ] . ^ $ : < > etc. You can find complete definition in MSDN. That means that your original text cannot contain these characters. Or you must escape them all with \ character.

And that's exactly what my macro does:

  1. It allows you to enter multiline search/replace text.
  2. Then it converts it to regex syntax.
  3. Opens standard VS Find/Replace window and automatically fills Find and Replace boxes with regex text. It also selects Regular Expressions mode.

    Standard VS Replace dialog with prefilled values

You can then search and replace as usually. To create macro:

  • Open Macro Explorer. Go to Tools - Macros - Macro Explorer
  • Navigate to Macros - MyMacros. Here you must create your own class by right-click and New Module.... Name it MultilineSearch.
  • Double click your MultilineSearch module to open Macros IDE.
  • Delete its all contents and copy this code:
    Imports System
    Imports EnvDTE
    Imports EnvDTE80
    Imports System.Diagnostics
    
    Public Module MultilineSearch
        Sub MultilineSearchReplace()
            Dim winptr As New WinWrapper(DTE)
            Dim sf As New MultilineSearchForm
            sf.ShowDialog(winptr)
            If sf.result <> FindReplaceKind.none Then
                ' temporarily disable Tools - Options -
                ' Environment - Documents - Initialize Find text from editor
                Dim oldFindInit As Boolean
                Try
                    'vs 2002/2003
                    Dim props As EnvDTE.Properties
                    props = DTE.Properties("Environment", "Documents")
                    Dim prop As EnvDTE.Property = props.Item("FindReplaceInitializeFromEditor")
                    oldFindInit = CBool(prop.Value)
                    prop.Value = False
                Catch ex As System.Exception
                    Try
                        'vs 2005/2008
                        Dim props As EnvDTE.Properties
                        props = DTE.Properties("Environment", "FindAndReplace")
                        Dim prop As EnvDTE.Property = props.Item("InitializeFromEditor")
                        oldFindInit = CBool(prop.Value)
                        prop.Value = False
                    Catch ex2 As Exception
                    End Try
                End Try
                DTE.Find.PatternSyntax = vsFindPatternSyntax.vsFindPatternSyntaxRegExpr
                DTE.Find.FindWhat = sf.findText
                If sf.replaceText.Length = 0 Then
                    'If Replace text is empty, the Find dialog automatically
                    'uses the last non-empty value from history. To prevent this,
                    'we must pass some non-empty value which produces empty text.
                    'The ninth regex tagged expression seems good for this as it is very
                    'unlikely that user will add 9 tagged expressions in Find field in
                    'original VS Find dialog after it is pre-filled with this macro.
                    DTE.Find.ReplaceWith = "\9"
                End If
                DTE.Find.ReplaceWith = sf.replaceText
                Select Case sf.result
                    Case FindReplaceKind.find
                        DTE.ExecuteCommand("Edit.Find")
                    Case FindReplaceKind.findInFiles
                        DTE.ExecuteCommand("Edit.FindinFiles")
                    Case FindReplaceKind.replace
                        DTE.ExecuteCommand("Edit.Replace")
                    Case FindReplaceKind.replaceInFiles
                        DTE.ExecuteCommand("Edit.ReplaceinFiles")
                    Case Else
                End Select
                ' restore Tools - Options -
                ' Environment - Documents - Initialize Find text from editor
                Try
                    'vs 2002/2003
                    Dim props As EnvDTE.Properties
                    props = DTE.Properties("Environment", "Documents")
                    Dim prop As EnvDTE.Property = props.Item("FindReplaceInitializeFromEditor")
                    prop.Value = oldFindInit
                Catch ex As System.Exception
                    Try
                        'vs 2005/2008
                        Dim props As EnvDTE.Properties
                        props = DTE.Properties("Environment", "FindAndReplace")
                        Dim prop As EnvDTE.Property = props.Item("InitializeFromEditor")
                        prop.Value = oldFindInit
                    Catch ex2 As Exception
                    End Try
                End Try
            End If
        End Sub
    End Module
    
    '''<summary>Types of find/replace operations.</summary>
    Public Enum FindReplaceKind
        '''<summary>Find</summary>
        find
        '''<summary>Find In Files</summary>
        findInFiles
        '''<summary>Replace</summary>
        replace
        '''<summary>Replace in Files</summary>
        replaceInFiles
        '''<summary>None. Cancel was pressed.</summary>
        none
    End Enum
    
    Public Class MultilineSearchForm
        Inherits System.Windows.Forms.Form
    #Region " Windows Form Designer generated code "
        Public Sub New()
            MyBase.New()
            'This call is required by the Windows Form Designer.
            InitializeComponent()
            'Add any initialization after the InitializeComponent() call
        End Sub
        'Form overrides dispose to clean up the component list.
        Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
            If disposing Then
                If Not (components Is Nothing) Then
                    components.Dispose()
                End If
            End If
            MyBase.Dispose(disposing)
        End Sub
        'Required by the Windows Form Designer
        Private components As System.ComponentModel.IContainer
        'NOTE: The following procedure is required by the Windows Form Designer
        'It can be modified using the Windows Form Designer. 
        'Do not modify it using the code editor.
        Friend WithEvents FindBox As System.Windows.Forms.TextBox
        Friend WithEvents Label1 As System.Windows.Forms.Label
        Friend WithEvents Label2 As System.Windows.Forms.Label
        Friend WithEvents ReplaceBox As System.Windows.Forms.TextBox
        Friend WithEvents FindBtn As System.Windows.Forms.Button
        Friend WithEvents FindInFilesBtn As System.Windows.Forms.Button
        Friend WithEvents ReplaceBtn As System.Windows.Forms.Button
        Friend WithEvents ReplaceInFilesBtn As System.Windows.Forms.Button
        Friend WithEvents CancelBtn As System.Windows.Forms.Button
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
            Me.FindBox = New System.Windows.Forms.TextBox
            Me.Label1 = New System.Windows.Forms.Label
            Me.Label2 = New System.Windows.Forms.Label
            Me.ReplaceBox = New System.Windows.Forms.TextBox
            Me.FindBtn = New System.Windows.Forms.Button
            Me.FindInFilesBtn = New System.Windows.Forms.Button
            Me.ReplaceBtn = New System.Windows.Forms.Button
            Me.ReplaceInFilesBtn = New System.Windows.Forms.Button
            Me.CancelBtn = New System.Windows.Forms.Button
            Me.SuspendLayout()
            '
            'FindBox
            '
            Me.FindBox.Location = New System.Drawing.Point(16, 24)
            Me.FindBox.Multiline = True
            Me.FindBox.Name = "FindBox"
            Me.FindBox.ScrollBars = System.Windows.Forms.ScrollBars.Both
            Me.FindBox.Size = New System.Drawing.Size(400, 80)
            Me.FindBox.TabIndex = 0
            Me.FindBox.Text = ""
            '
            'Label1
            '
            Me.Label1.Location = New System.Drawing.Point(16, 8)
            Me.Label1.Name = "Label1"
            Me.Label1.Size = New System.Drawing.Size(160, 16)
            Me.Label1.TabIndex = 2
            Me.Label1.Text = "Find what:"
            '
            'Label2
            '
            Me.Label2.Location = New System.Drawing.Point(16, 112)
            Me.Label2.Name = "Label2"
            Me.Label2.Size = New System.Drawing.Size(160, 16)
            Me.Label2.TabIndex = 4
            Me.Label2.Text = "Replace with:"
            '
            'ReplaceBox
            '
            Me.ReplaceBox.Location = New System.Drawing.Point(16, 128)
            Me.ReplaceBox.Multiline = True
            Me.ReplaceBox.Name = "ReplaceBox"
            Me.ReplaceBox.ScrollBars = System.Windows.Forms.ScrollBars.Both
            Me.ReplaceBox.Size = New System.Drawing.Size(400, 80)
            Me.ReplaceBox.TabIndex = 3
            Me.ReplaceBox.Text = ""
            '
            'FindBtn
            '
            Me.FindBtn.Location = New System.Drawing.Point(16, 232)
            Me.FindBtn.Name = "FindBtn"
            Me.FindBtn.Size = New System.Drawing.Size(80, 24)
            Me.FindBtn.TabIndex = 5
            Me.FindBtn.Text = "Find>>"
            '
            'FindInFilesBtn
            '
            Me.FindInFilesBtn.Location = New System.Drawing.Point(104, 232)
            Me.FindInFilesBtn.Name = "FindInFilesBtn"
            Me.FindInFilesBtn.Size = New System.Drawing.Size(96, 24)
            Me.FindInFilesBtn.TabIndex = 6
            Me.FindInFilesBtn.Text = "Find in Files>>"
            '
            'ReplaceBtn
            '
            Me.ReplaceBtn.Location = New System.Drawing.Point(216, 232)
            Me.ReplaceBtn.Name = "ReplaceBtn"
            Me.ReplaceBtn.Size = New System.Drawing.Size(80, 24)
            Me.ReplaceBtn.TabIndex = 7
            Me.ReplaceBtn.Text = "Replace>>"
            '
            'ReplaceInFilesBtn
            '
            Me.ReplaceInFilesBtn.Location = New System.Drawing.Point(304, 232)
            Me.ReplaceInFilesBtn.Name = "ReplaceInFilesBtn"
            Me.ReplaceInFilesBtn.Size = New System.Drawing.Size(112, 24)
            Me.ReplaceInFilesBtn.TabIndex = 8
            Me.ReplaceInFilesBtn.Text = "Replace in Files>>"
            '
            'CancelBtn
            '
            Me.CancelBtn.DialogResult = System.Windows.Forms.DialogResult.Cancel
            Me.CancelBtn.Location = New System.Drawing.Point(168, 272)
            Me.CancelBtn.Name = "CancelBtn"
            Me.CancelBtn.Size = New System.Drawing.Size(80, 24)
            Me.CancelBtn.TabIndex = 9
            Me.CancelBtn.Text = "Cancel"
            '
            'MultilineSearchForm
            '
            Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
            Me.CancelButton = Me.CancelBtn
            Me.ClientSize = New System.Drawing.Size(432, 310)
            Me.Controls.Add(Me.CancelBtn)
            Me.Controls.Add(Me.ReplaceInFilesBtn)
            Me.Controls.Add(Me.ReplaceBtn)
            Me.Controls.Add(Me.FindInFilesBtn)
            Me.Controls.Add(Me.FindBtn)
            Me.Controls.Add(Me.Label2)
            Me.Controls.Add(Me.ReplaceBox)
            Me.Controls.Add(Me.Label1)
            Me.Controls.Add(Me.FindBox)
            Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.SizableToolWindow
            Me.Name = "MultilineSearchForm"
            Me.Text = "Multiline Search and Replace"
    
            Me.ResumeLayout(False)
        End Sub
    #End Region
    
    #Region "Properties"
        Private m_result As FindReplaceKind = FindReplaceKind.none
        '''<summary>Gets result button from this dialog.</summary>
        '''<value>The value specifying which button was pressed.</value>
        Public ReadOnly Property result() As FindReplaceKind
            Get
                Return m_result
            End Get
        End Property
        Private m_findText As String
        '''<summary>Gets escaped multiline text to be searched.</summary>
        '''<value></value>
        Public ReadOnly Property findText() As String
            Get
                Return m_findText
            End Get
        End Property
    
        Private m_replaceText As String
        '''<summary>Gets escaped multiline replace text.</summary>
        '''<value></value>
        Public ReadOnly Property replaceText() As String
            Get
                Return m_replaceText
            End Get
        End Property
    #End Region
    
        '''<summary>Transforms the text to regular expression syntax.</summary>
        '''<param name="original">Original text.</param>
        '''<returns>Text with escaped regex characters.</returns>
        Private Function escapeRegEx(ByVal original As String) As String
            Dim specialChars() As Char = "\.*+^$><[]|{}:@#()~".ToCharArray
            Dim c As Char
            For Each c In specialChars
                original = original.Replace(c.ToString, "\" & c.ToString)
            Next
            original = original.Replace(vbCrLf, "\n")
            Return original
        End Function
    
        '''<summary>Transforms the text to regular expression syntax in Replace field.</summary>
        '''<param name="original">Original text.</param>
        '''<returns>Text with some escaped regex characters.</returns>
        Private Function escapeReplaceRegEx(ByVal original As String) As String
            Dim specialChars() As Char = "\".ToCharArray
            Dim c As Char
            For Each c In specialChars
                original = original.Replace(c.ToString, "\" & c.ToString)
            Next
            original = original.Replace(vbCrLf, "\n")
            Return original
        End Function
    
        Private Sub MultilineSearchForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Try
                Me.Activate()
            Catch ex As System.Exception
            End Try
        End Sub
    
        Private Sub CancelBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CancelBtn.Click
            Try
                m_result = FindReplaceKind.none
                Me.Close()
            Catch ex As System.Exception
            End Try
        End Sub
        Private Sub FindBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FindBtn.Click
            Try
                m_findText = escapeRegEx(Me.FindBox.Text)
                m_replaceText = escapeReplaceRegEx(Me.ReplaceBox.Text)
                m_result = FindReplaceKind.find
                Me.Close()
            Catch ex As System.Exception
            End Try
        End Sub
        Private Sub FindInFilesBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FindInFilesBtn.Click
            Try
                m_findText = escapeRegEx(Me.FindBox.Text)
                m_replaceText = escapeReplaceRegEx(Me.ReplaceBox.Text)
                m_result = FindReplaceKind.findInFiles
                Me.Close()
            Catch ex As System.Exception
            End Try
        End Sub
        Private Sub ReplaceBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ReplaceBtn.Click
            Try
                m_findText = escapeRegEx(Me.FindBox.Text)
                m_replaceText = escapeReplaceRegEx(Me.ReplaceBox.Text)
                m_result = FindReplaceKind.replace
                Me.Close()
            Catch ex As System.Exception
            End Try
        End Sub
        Private Sub ReplaceInFilesBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ReplaceInFilesBtn.Click
            Try
                m_findText = escapeRegEx(Me.FindBox.Text)
                m_replaceText = escapeReplaceRegEx(Me.ReplaceBox.Text)
                m_result = FindReplaceKind.replaceInFiles
                Me.Close()
            Catch ex As System.Exception
            End Try
        End Sub
    End Class
    
    ''' <summary>
    ''' Helper class for passing parent window argument to ShowDialog method.
    ''' </summary>
    ''' <remarks>See http://support.microsoft.com/kb/312877 for details.</remarks>
    Friend Class WinWrapper
        Implements System.Windows.Forms.IWin32Window
    
        Private vsinstance As DTE
    
        Sub New(ByVal dte As DTE)
            vsinstance = dte
        End Sub
    
        Overridable ReadOnly Property Handle() As System.IntPtr Implements System.Windows.Forms.IWin32Window.Handle
            Get
                Dim iptr As New System.IntPtr(vsinstance.MainWindow.HWnd)
                Return iptr
            End Get
        End Property
    End Class
  • Add System.Drawing.dll to References under MyMacros in Macros IDE.
  • Close Macros IDE.

Now you can run MultilineSearchReplace macro. You can of course create a toolbar or menu button or assign keyboard shortcut to it.

There is one interesting thing on this macro. It creates its own form. There is no Form designer in Macros IDE so I thought it was not possible to create my own form in macro. Fortunately, I was wrong. Form is a class as any other, it just must inherit from System.Windows.Forms.Form. I created my form in normal VB .NET project using Form designer and then copied the code into macro IDE. It couldn't be easier.

UPDATE 15. October 2008: I have updated the macro code. It fixes two bugs:

  1. In VS 2005 and 2008, when using Find/Replace in Files, the "Find what" field didn't contain value entered by user but text from cursor position in editor.
  2. When Replace field contained some special characters, they were incorrectly escaped with \.

UPDATE 23. June 2009: I have updated the macro code. It fixes two issues:

  1. When Replace with text was empty (you wanted to delete the text), the last non-empty text from replace history was automatically filled. This is behavior of standard VS Find/Replace dialog. To prevent this, we need to pass non-empty value which produces empty text. The "\9" seems OK for this. This is regex tagged expression number nine which is always empty string. You would need to manually add nine tagged expressions (text enclosed with {}) in Find what field in original VS Find/Replace dialog to \9 produce any text.
  2. When macro's modal window was open and you switched to another application and then back to VS, the macro window was not visible and you had to activate it from task bar.

Comments

# Peter Macej 2006-03-23 21:16
I just tried to create thid macro in new installation of VS and there were some errors when I tried to run it. I needed to add System.Drawing. dll to References under MyMacros in Macros IDE.
# Magnon Damant 2006-04-05 04:02
I'd REALLY appreachiate a stand alone program of this...
Do you have one available? If you did I'd really
appreachiate it, emailed to me.

Thank you for this tip, I could maybe try to translate the code
into Python to use as a script (don't have VS...)

-Magnon Damant
# Peter Macej 2006-04-05 09:23
No, I don't have standalone program for this. Quick Google search revealed some free programs that you may try:
http://www.nodesoft.com/SearchAndReplace/
http://www.pricelesswarehome.org/2004/PL2004FILEUTILITIES.php#0379-PW
# Avi 2006-05-04 13:49
After a while , the lookin is full with all the last locations .It all adds up in the registry. Is there a way to clear it/ prevent from adding ?
# Peter Macej 2006-05-04 15:42
I don't think it is possible to clear or prevent from adding the "Look In" history of "Find in Files" dialog from macro or add-in. You will probably need to use VSIP package. You can only manually manipulate the registry. But the changes take effect only after VS restart. You can use DTE.RegistryRoo t property to find the correct registry key.
# Avi 2006-05-07 13:33
Peter - sorry for bugging but your post is so useful...
Is there a way to get the results back as a collection or something like that ? I would like to use the results in some way
# Peter Macej 2006-05-07 19:31
You can get the results easily. I wrote an article at http://www.helixoft.com/blog/archives/23 how to do it.
# _ 2006-06-07 00:11
Does this work in VS 2003? I couldn't get it to work on VS 2003.
# Peter Macej 2006-06-07 09:32
Yes, it works in VS 2003. The screenshots are from VS 2003. What kind of error did you encounter? Did you add System.Drawing. dll to References under MyMacros in Macros IDE?
# Sadiq 2007-09-21 09:54
Your macro worked for me but I had to remove the escape character "\" wherever there was ")" or "(" in the 'replace' line. For example, for "dispose\(\)" in the replace line I had to use "dispose()".

Thanks for your multiline searching macro. It helped me.
# Torben 2007-10-13 15:32
Hej Powershell guy.

In gui-help replace it can be defined as - a specified number of times.
But how should i do, if i only will change the first space and not the other space
in the following example ???

${C:\powerstart.txt} -replace ' ',';' > C:\powerstart1.txt

I need a real reference manual !!
--------------------------------------------------------------------

Replace

Definition: Returns a string in which a specified substring has been replaced
with another substring a specified number of times.

$a = "bxnxnx"
$a = $a -replace("x","a")

Regards
Torben BrĂ¸nsholm
Denmark
# Ramone Eduardo 2008-05-07 04:02
Thank you!
# Giuseppe 2008-05-19 16:56
This has solved my problem in a nanosecond, thanks a lot!!!
# Andrei 2008-07-29 12:00
Man,
It works just fine! Saved a lot of time for me. Thanks pal!
# Shaun 2008-08-23 09:44
hehe,

i just requested this functionality as a feature in visual studio and the microsoft connect team pointed me here.

cheers for the macro. very useful
# Nathan Phillips 2008-10-15 13:53
The code above may be good for VS2005, but the two bits where it looks up environment settings will need to be replaced with something like the following in VS2008:
Dim props As EnvDTE.Properti es = DTE.Properties( "Environment", "FindAndReplace")
Dim prop As EnvDTE.Property = props.Item("InitializeFromEditor")
oldFindInit = CBool(prop.Valu e)
# Peter Macej 2008-10-15 14:43
Nathan, thank you for finding this problem. It even didn't work in VS 2005, only in VS 2002/2003. I haven't found it because it worked in VS 2005 if Find/Replace was used. The problem occured only if Find in Files/Replace in files was used.

I have updated the code in my post. It fixes this problem and also bug when replacing with special characters mentioned in Sadiq's comment.
# Charles Rex 2008-10-25 20:04
Hello,

Be careful, this macro is unchecking behind the scenes, without asking permission, a Visual Studio 2005 environment checkbox:

There is a "Automatically populate Find What with text from the editor" checkbox under
Tools-> Options -> Environment -> Find and Replace

which is automatically reset ??

See also this article:

http://blogs.msdn.com/saraford/archive/2007/10/22/did-you-know-ctrl-f-does-a-quick-find-in-the-current-document.aspx
# Peter Macej 2008-10-25 20:13
To Charles Rex: Exactly. This macro turns off this setting and then immediatelly restores its value back. The setting must be temporarily reset, otherwise the Find dialog ignores our text and uses selected text from editor. The update mentioned in the post addresses exactly this issue.
# Charles Rex 2008-10-25 20:30
Small correction

Replace

Catch ex2 As Exception

with

Catch ex2 As System.Exceptio n
# Peter Macej 2008-10-26 16:05
> Catch ex2 As System.Exceptio n

Good point. I have updated the code. In fact, I have
Imports System
at the beginning of each file so this was not necessary on my computer.
# Tewr 2008-11-17 10:51
Just awesome, works like a dream, thank you for this. I do this all the time, and I wrote an external program to escape my searches and replaces, really annoying, this macro saves a lot of time. Great install instructions, too.

A suggestion to perfection would be to give the user the ability to select text during the macro execution, which is not possible, at least not in VS2008. Don't know if it's possible.

Thanks again //Tor
# mathias 2009-01-16 17:28
Thanks a lot. You should be a Sir ;-)
# Carlos 2009-03-13 21:53
Thank you for making this available. It saved me a ton of time yesterday and certainly in the future as well!
# Magsy 2009-03-25 19:38
You is awesome!
# SM 2009-04-29 01:24
Thanks. This was very useful.
# Tad 2009-05-11 03:27
Awesome... thanks.
# AndrewW 2009-06-13 11:23
Fantastic, saved me loads of time. Thanks.
Just one thing though ... I was deleting lines with your macro, so left the 'Replace with' textbox blank. The 'Find and Replace' dialog though put a " character in the 'Replace with' textbox.
Cheers.
# Peter Macej 2009-06-23 10:52
Andrew, thank you for reporting this problem. I have fixed it and updated the article. See UPDATE note at the end of article.
# Enrique Duarte 2009-09-04 17:15
I have been looking for this kind of functionality for months. Your macro solves many of my problems. Thanks.
Can I use your macro combined with wildcards?
I would like to replace similar, non consecutive, lines of code in one shot, for instance, replace the lines
'if traceActive then traceEntry(xxx)
'if myFunctions.tra ceActive then traceEntry(ggggg)
'if traceActive then traceExit()
with
#if debug then
traceEntry(xxx)
#end if
#if debug then
myFunctions.traceEntry(ggggg)
#end if
#if debug then
traceExit()
#end if
# Tobias Manthey 2009-09-22 19:23
Thanks a lot for sharing! Worked perfectly!
# Walker 2009-10-02 04:29
I have found another tool for this task. It is Freeware and you can replace, delete and insert multi-line text passages in lots of files at once. Here you can find it:

http://www.sttmedia.com/textconverter

Greets, Walker
# xemulja 2009-10-09 09:08
Thanks a lot!
# heyyou 2009-10-14 22:30
that text converter is a crap and it's not really free! don't advertise here ok?!
# heyyou 2009-10-14 22:32
The VS macro is marvalous! It saves me tons of minutes. Thanks a lot!
# Dan Mattsson 2009-10-28 12:12
Sweet!
Works like a charm and saves me a ton of work.
# Mark Kamoski 2010-01-05 21:56
Helixoft -- Thank you for the excellent code. Here are some simple suggestions to easily make it better. (1). For the textboxes, use a monospaced font, preferably one with a slashed-zero like Consolas. (2) Make the form stretchy, so the textboxes are pinned to left and right window, so the textboxes resize when the window resizes, etc. Regardless, the code is great as-is. I really appreciate the help. Thank you. -- Mark Kamoski
# dano 2010-02-18 20:08
Nothing happens when I run the macro - do I have to create my own form in order for this to work?
# Alex Cohn 2010-02-24 15:16
Thanks a lot!
# Doug 2010-05-20 20:35
Brillant! thanks for this! I love the fact that it integrates into the reg expression function of the existing find and replace.
# Nathan 2010-06-22 19:56
Excellent! Works great in VS2010! Just make sure to follow ALL the directions, I didn't add the Drawing reference at first.
One idea is to make it non-modal, so I can copy text from the file to paste into the search box.
# toplum 2010-08-16 12:07
The VS macro is marvalous! It saves me tons of minutes. Thanks a lot!
# Fontaine 2010-09-09 22:48
I have followed the directions very carefully even to make sure I add the System.Drawing. dll.
When I try and run the Macros nothing happens not even an error message. This will be my first Macro but I don't believe the error is from careless mistake. I have tried it in both 2005 and 2008 and the same occurs when I try to run it. If you have any suggestions or thoughts they would be greatly appreciated. Thank You
# Parrotlover77 2010-11-29 00:01
I'm having the same problem as Fontaine. I run it and nothing happens. No error, no dialog. Nothing. If I run it from the macro IDE, it works fine. So it's not something I can even debug! What the hell? What am I doing wrong? I followed the instructions exactly.
# Parrotlover77 2010-11-29 00:16
Well. I deleted the macro file entirely and started over with a new file and this time it worked. Weird.
# 2011 modası 2011-05-09 11:38
great man! Works like a charm and saves me a ton of work. thanks
# Lena 2011-06-01 09:52
To Fontaine and Parrotlover77:
I had the same problem but ovecame it. Are you sure you callsed module MultilineSearch ? Code declares module with this name, so if you use another name when create a new module, it doesn't work.
# Tyler 2011-10-12 02:05
Thank you! Exactly what I was looking for.
# Perry 2011-10-14 21:07
You should anchor the FindBox and the ReplaceBox to the right, or it is kind of purposeless having the dialog be resizable. Better yet, do that and additionally handle resizing the form and split the added vertical text between both of those.
# JB Brossard 2012-01-17 14:43
Thanks very much for this very useful macro!
# viviana 2012-01-19 15:46
AWWWWWWWWWWWSOO OOOOOMEEEEE!!!!
# Steve 2012-02-09 22:59
Thank you so much! Your work really saved me tons of time! Absolutely awesome solution. Can't help wondering why isn't this built into VS... and no, I'm not interested in writing regex myself, thank you so much... :)
# Yuval 2012-06-26 12:37
a few tweaks.

1. insert selected text in the search box:
add this code in the MultilineSearch Form constructor, after this comment:
'Add any initialization after the InitializeCompo nent() call
Dim objDocument As EnvDTE.Document
Dim objTextDocument As EnvDTE.TextDocument
Dim objTextSelectio n As EnvDTE.TextSelection
Try
'Get the active document
objDocument = DTE.ActiveDocument
'Get the text document
objTextDocument = CType(objDocume nt.Object, EnvDTE.TextDocument)
'Get the text selection object
objTextSelectio n = objTextDocument.Selection
'Set the text
Me.FindBox.Text = objTextSelectio n.Text
Catch ex As System.Exception
End Try

2. now it would be very useful to make the macro available in the context menu
http://blogs.msdn.com/b/vseditor/archive/2004/12/14/301111.aspx
you'll be able to highlight a piece of code, right click, execute the macro and the text you highlighted is already in the search box

3. the resizing doesn't work properly. add these lines before each comment:
'
'FindBox
'
Me.FindBox.Anchor = CType((((System .Windows.Forms. AnchorStyles.To p Or System.Windows. Forms.AnchorSty les.Bottom) _
Or System.Windows. Forms.AnchorSty les.Left) _
Or System.Windows. Forms.AnchorSty les.Right), System.Windows. Forms.AnchorSty les)
'
'Label2
'
Me.Label2.Anchor = CType((System.W indows.Forms.An chorStyles.Bott om Or System.Windows. Forms.AnchorSty les.Left), System.Windows. Forms.AnchorSty les)
'
'ReplaceBox
'
Me.ReplaceBox.Anchor = CType(((System. Windows.Forms.A nchorStyles.Bot tom Or System.Windows. Forms.AnchorSty les.Left) _
Or System.Windows. Forms.AnchorSty les.Right), System.Windows. Forms.AnchorSty les)
'
'FindBtn
'
Me.FindBtn.Anchor = CType((System.W indows.Forms.An chorStyles.Bott om Or System.Windows. Forms.AnchorSty les.Left), System.Windows. Forms.AnchorSty les)
'
'FindInFilesBtn
'
Me.FindInFilesBtn.Anchor = CType((System.W indows.Forms.An chorStyles.Bott om Or System.Windows. Forms.AnchorSty les.Left), System.Windows. Forms.AnchorSty les)
'
'ReplaceBtn
'
Me.ReplaceBtn.Anchor = CType((System.W indows.Forms.An chorStyles.Bott om Or System.Windows. Forms.AnchorSty les.Left), System.Windows. Forms.AnchorSty les)
'
'ReplaceInFilesBtn
'
Me.ReplaceInFilesBtn.Anchor = CType((System.W indows.Forms.An chorStyles.Bott om Or System.Windows. Forms.AnchorSty les.Left), System.Windows. Forms.AnchorSty les)
'
'CancelBtn
'
Me.CancelBtn.Anchor = CType((System.W indows.Forms.An chorStyles.Bott om Or System.Windows. Forms.AnchorSty les.Left), System.Windows. Forms.AnchorSty les)
# Tyson 2012-07-02 23:11
You are a life saver! Thank you!
# Stiv Ostenberg 2014-01-23 16:57
This is a macro, and does not work under VS 2012 and later which require "AddIns". Tried to paste this code into an AddIn but even after I resolved the squiggly line object issues by replacing DTE with a new DTE object, the AddIn failed to load. Sigh.

Comments are now closed for this entry

 

Start generating your .NET documentation now!
DOWNLOAD
Free, fully functional trial