代码之家  ›  专栏  ›  技术社区  ›  Ryan Lundy

从Visual Studio宏插入代码段

  •  0
  • Ryan Lundy  · 技术社区  · 16 年前

    在这种情况下,我希望运行一个Visual Studio宏,该宏让用户在输入框中键入一些内容,然后插入一个片段,并在其中的某个位置包含该文本。不幸的是,我不知道如何从宏代码中插入代码片段。看起来像是

    DTE.ExecuteCommand("Edit.InvokeSnippetFromShortcut")
    

    DTE.ExecuteCommand("Edit.InvokeSnippetFromShortcut", "theSnippetName")
    

    但这些不管用。有什么想法吗?

    2 回复  |  直到 14 年前
        1
  •  1
  •   Matt    14 年前

    可以使用以下代码插入代码段。

    DTE.ActiveDocument.Selection.Text = "snippetshortcut"
    DTE.ExecuteCommand("Edit.InsertTab")
    

    如果您使用的是VS 2010,那么您需要两次调用dte.executeCommand(“edit.inserttab”)。

        2
  •  0
  •   Yaroslav Yakovlev    16 年前

    您需要决定应该在哪里选择此代码。要在当前文本选择中插入文本,需要使用以下代码:

    Dim textSelection As EnvDTE.TextSelection
    textSelection = DTE.ActiveWindow.Selection          
    textSelection.Insert(MyTextVarHere)