locked
Macro to Find and Replace RRS feed

  • Question

  • Hi All,

    Performing and record macro to find the selected string in open .RESX file and get the key and replace it on selection string.Below is the recorded macro with one modification for find what...

    The problem with the below macro is that when run it does not copy the key from the .RESX file.

    Can you please suggest or refer to achieve this?

     Sub genResx()
            DTE.ActiveDocument.Selection.Copy()
            DTE.Windows.Item("Resources.resx").Activate()
            DTE.ExecuteCommand("Edit.Find")
            DTE.ExecuteCommand("Edit.Paste")
            DTE.Find.MatchCase = True
            DTE.Find.FindWhat = DTE.ActiveDocument.Selection.Copy()
            DTE.Find.Target = vsFindTarget.vsFindTargetCurrentDocument
            DTE.Find.MatchWholeWord = False
            DTE.Find.Backwards = False
            DTE.Find.MatchInHiddenText = False
            DTE.Find.PatternSyntax = vsFindPatternSyntax.vsFindPatternSyntaxLiteral
            DTE.Find.Action = vsFindAction.vsFindActionFind
            If (DTE.Find.Execute() = vsFindResult.vsFindResultNotFound) Then
                Throw New System.Exception("vsFindResultNotFound")
            End If
            DTE.Windows.Item("Program.cs").Activate()
            DTE.ActiveDocument.Selection.Paste()
            DTE.ActiveDocument.Save()
        End Sub

    Thanks & Regards,

    ShailShin

    • Moved by Kristin Xie Wednesday, September 24, 2014 5:19 AM
    Tuesday, September 23, 2014 5:54 AM

Answers

  • Rather than recording command executions ("Edit.Copy", "Edit.Paste", "Edit.Find") you can use the TextDocument, TextSelection and EditPoint objects, which have methods to find / replace, get the selected text, etc.:

            Dim textDocument As TextDocument
            Dim textSelection As TextSelection
            Dim editPoint As EditPoint
            Dim selectedText As String

            textDocument = CType(DTE.ActiveDocument.Object, TextDocument)

            textSelection = textDocument.Selection
            selectedText = textSelection.Text

            editPoint = textDocument.StartPoint.CreateEditPoint()

            editPoint.FindPattern(...) or editPoint.ReplaceText(...) or editPoint.ReplacePattern(...)


    * My new blog about VSX: http://www.visualstudioextensibility.com * Twitter: https://twitter.com/VSExtensibility * MZ-Tools productivity extension for Visual Studio: http://www.mztools.com.

    • Marked as answer by Caillen Monday, September 29, 2014 7:36 AM
    Wednesday, September 24, 2014 9:44 AM

All replies

  • Hi ShaikShin,

    You case related to Visual Studio Integrate, I moved your case to Visual Studio Integrate  forum for better support.

    Best regards,

    Kristin


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, September 24, 2014 5:22 AM
  • Rather than recording command executions ("Edit.Copy", "Edit.Paste", "Edit.Find") you can use the TextDocument, TextSelection and EditPoint objects, which have methods to find / replace, get the selected text, etc.:

            Dim textDocument As TextDocument
            Dim textSelection As TextSelection
            Dim editPoint As EditPoint
            Dim selectedText As String

            textDocument = CType(DTE.ActiveDocument.Object, TextDocument)

            textSelection = textDocument.Selection
            selectedText = textSelection.Text

            editPoint = textDocument.StartPoint.CreateEditPoint()

            editPoint.FindPattern(...) or editPoint.ReplaceText(...) or editPoint.ReplacePattern(...)


    * My new blog about VSX: http://www.visualstudioextensibility.com * Twitter: https://twitter.com/VSExtensibility * MZ-Tools productivity extension for Visual Studio: http://www.mztools.com.

    • Marked as answer by Caillen Monday, September 29, 2014 7:36 AM
    Wednesday, September 24, 2014 9:44 AM