Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio - How to disable autoformat/correct while running macro?

When running a macro that changes the selected text, tags are automatically closed and the text formatted. How can I prevent that from happening?

For example, wrapping text in a tag:

DTE.ActiveDocument.Selection.Text = String.Format("<tag>{0}</tag>", DTE.ActiveDocument.Selection.Text)

Ends up with two closing tags:

<tag>Text</tag></tag>

Even stranger, multiple lines fails:

<li>One</li>
<li>Two</li>
<li>Three</li>

An ends up as

<ul>            <li>One</li>
            <li>Two</li>
                        <li>Three</li></li></ul>

How can I prevent that? As can be seen by the last example, the formatting is wrong and there is an extra </li>

like image 486
SamWM Avatar asked May 31 '26 02:05

SamWM


1 Answers

You'll need to insert the text rather than assigning it:

Try
    DTE.UndoContext.Open("InsertSomeCode")
    Dim ts As TextSelection = CType(DTE.ActiveDocument.Selection, TextSelection)
    ts.Insert(String.Format("<tag>{0}</tag>", ts.Text))
Finally
    DTE.UndoContext.Close()
End Try
like image 151
Brian Schmitt Avatar answered Jun 02 '26 17:06

Brian Schmitt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!