Freigeben über


Visual Studio Macro for Replacing Text in a File

Recently, I used Microsoft Visual Studio 2005 to edit a set of HTML files. As I began to search and replace the same text over and over in these files, I realized that I could do it faster with a macro. To be honest, I had only used Visual Studio macros once or twice before, so I wasn't certain about the specific code that I needed to write to make it happen. Using my prior knowledge of the Microsoft .NET base class library's System.IO.File class and the Microsoft Visual Basic for Applications library's String class, I was able to easily figure this out:

Sub ReplaceHTML()

        Dim doc As TextDocument
doc = DTE.ActiveDocument.Object

        With doc

            .ReplacePattern("hiddenScrollOffset"">", _
"hiddenScrollOffset""/>")
.ReplacePattern("VsPackage", "VSPackage", _
EnvDTE.vsFindOptions.vsFindOptionsMatchCase)
.ReplacePattern("Walkthru:", "Walkthrough: ")
.ReplacePattern("i.e.", "for example")
.ReplacePattern("etc.", "and so on")

        End With

End Sub

-- Paul

------------------------------------
This posting is provided "AS IS" with no warranties, and confers no rights.

Comments

  • Anonymous
    March 07, 2013
    good job :)