Click here to Skip to main content
13,147,050 members (24,255 online)
Click here to Skip to main content
Add your own
alternative version


2 bookmarked
Posted 3 Sep 2012

Visual Studio Extensibility

, 3 Sep 2012
Rate this:
Please Sign up or sign in to vote.
Just wrote a quick macro for Visual Studio that would replace with >, etc.

Just wrote a quick macro for Visual Studio that would replace < with < and > with >, etc. I am probably the 1000th person to do that. Yet, it proved to be surprisingly hard.

  • DTE.ActiveDocument.Selection is an Object. No methods, no nothing. Need to cast to TextSelection to do anything useful with it.
  • TextSelection.ReplacePattern() happily replaces one pattern, then loses selection. Attempts to artificially restore selection failed, as TopPoint and BottomPoint properties are read only. MoveToPoint() did not do the trick either. I am still not sure how to tell the IDE to select the text from here to there.
  • If you attempt to do something like Selection.Text = Selection.Text.Replace((...) it works fine for one line, but for multiline selection it inserts lots of spaces and generally looks very weird. It also takes a lot of time and occasionally throws exceptions. A non starter.
  • Tried to use DTE.Find object, could not find a way to close programmatically the damn Find window. Fortunately DTE.Find.FindReplace() works without opening the window.

Oh yeah, if you try Googling for it – forget about it. You are drowning in crap. Bing is slightly better, but still no help there.

Final solution:

Sub EscapeXmlChars()
    Replace("&", "&")
    Replace("<", "<")
    Replace(">", ">")
End Sub

Sub UnescapeXmlChars()
    Replace("<", "<")
    Replace(">", ">")
    Replace("&", "&")
End Sub

Private Sub Replace(ByVal find As String, ByVal replace As String)
    DTE.Find.FindReplace( _
    vsFindAction.vsFindActionReplaceAll, _
    find, _
    vsFindOptions.vsFindOptionsMatchInHiddenText, _
    replace, _
End Sub


This article, along with any associated source code and files, is licensed under The Apache License, Version 2.0


About the Author

Ivan Krivyakov
Technical Lead Thomson Reuters
United States United States
Ivan is a hands-on software architect/technical lead working for Thomson Reuters in the New York City area. At present I am mostly building complex multi-threaded WPF application for the financial sector, but I am also interested in cloud computing, web development, mobile development, etc.

Please visit my web site:

You may also be interested in...

Comments and Discussions

Questionшутка дня! Pin
Thornik5-Sep-12 0:01
memberThornik5-Sep-12 0:01 
AnswerRe: шутка дня! Pin
Ivan Krivyakov6-Sep-12 10:40
memberIvan Krivyakov6-Sep-12 10:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.170915.1 | Last Updated 3 Sep 2012
Article Copyright 2012 by Ivan Krivyakov
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid