次の方法で共有


方法 : テキストの検索と置換を自動化する

Visual Studio アドインは、Visual Studio 2013 では使用されなくなりました。 アドインを VSPackage 拡張機能にアップグレードしてください。 アップグレードの詳細については、「FAQ: アドインを VSPackage 拡張に変換する」を参照してください。

Visual Studio では、統合開発環境 (IDE: Integrated Development Environment) で開かれ、システム上のファイルに含まれているドキュメント内のテキストを検索および置換できます。 この操作を行うための主な方法として、Find オブジェクトの FindReplace メソッドおよび Execute メソッドを使用します。 また、TextSelection オブジェクトおよび EditPoint オブジェクトには、FindPattern メソッドも用意されています。 詳細については、「方法 : コード エディターを制御する (Visual Basic)」の FindPattern メソッドを参照してください。

注意

[vsFindOptions] 列挙体の vsFindOptionsMatchInHiddenTex[t] 定数値は、すべてのテキスト (非表示のテキストを含む) を検索するため、FindPattern メソッドに適用されません。

EnvDTE80 名前空間にあるバージョンの Find の名前は Find2 です。 これは Find オブジェクトと同じですが、WaitForFindToComplete という名前の新しいプロパティが用意されています。 このブール型のプロパティが True に設定されている場合、検索操作は、選択したすべてのドキュメントが検索されるまで終了しません。

たとえば、100 個のドキュメントの中からある単語を検索した場合、WaitForFindToComplete プロパティを使用するか、FindDone イベントを処理しないと、不完全な結果が返ってくることがあります。 どちらの方法も有効ですが、すべてのドキュメントを検索してから検索結果を表示するには、WaitForFindToComplete プロパティを設定する方が簡単で時間もかかりません。

注意

実際に画面に表示されるダイアログ ボックスとメニュー コマンドは、アクティブな設定またはエディションによっては、ヘルプの説明と異なる場合があります。ここに記載されている手順は、全般的な開発設定が適用されているものとして記述されています。設定を変更するには、[ツール] メニューの [設定のインポートとエクスポート] をクリックします。詳細については、「Visual Studio での開発設定のカスタマイズ」を参照してください。

使用例

次の例では、Find オートメーション モデルのさまざまなメンバーを参照および使用する方法を示します。 この例では、テキストを含むテキスト ドキュメントを作成してから、さまざまなメソッドを使用してテキストの検索および置換を実行します。 この例を実行するには、単純なアドインの OnConnection メソッドを次のコードと置き換えます。 この例の別のセクションを実行する場合は、該当するコードのコメントを外してください。 このコードを実行する前に、EnvDTE アセンブリ参照の "相互運用性の埋め込み" プロパティが False に設定されていることを確認してください。

Public Sub OnConnection(ByVal application As Object, ByVal _
connectMode As ext_ConnectMode, ByVal addInInst As Object, _
ByRef custom As Array) Implements IDTExtensibility2.OnConnection
    _applicationObject = CType(application, DTE2)
    _addInInstance = CType(addInInst, AddIn)
    searchReplace(_applicationObject)
End Sub

Public Sub searchReplace(ByVal dte As DTE2)
    Dim findWin As Find2
    Dim doc As Document
    Dim textDoc As TextDocument
    Dim textSel As TextSelection
    Dim iCtr As Integer

    ' Create a new text file.
    dte.ItemOperations.NewFile("General\Text File")

    ' Set up references for the text document, Find object, and
    ' TextSelection object.
    doc = dte.ActiveDocument
    textDoc = CType(doc.Object("TextDocument"), TextDocument)
    textSel = textDoc.Selection
    findWin = CType(dte.Find, Find2)
    ' Make sure all docs are searched before displaying results.
    findWin.WaitForFindToComplete = True

    ' Insert ten lines of text.
    For iCtr = 1 To 10
        textDoc.Selection.Text = "This is a test" & vbCr
    Next iCtr
    textDoc.Selection.Text = "This is a different word"

    ' Uses FindReplace to find all occurrences of the word, test, in 
    ' the document.
    MsgBox("Now changing all occurrences of 'test' to 'replacement'.")
    findWin.FindReplace(vsFindAction.vsFindActionReplaceAll, "test", _
      vsFindOptions.vsFindOptionsMatchCase, "replacement", _
      vsFindTarget.vsFindTargetCurrentDocument, , , _
      vsFindResultsLocation.vsFindResultsNone)

    ' Uses Find2.Execute to find the word, different, in the document.
    ' findWin.FindWhat = "different"
    ' findWin.MatchCase = True
    ' findWin.Execute()

    ' Uses Find2.Execute to replace all occurrences of the word, Test, 
    ' with the word, replacement.
    ' findWin.FindWhat = "test"
    ' findWin.ReplaceWith = "replacement"
    ' findWin.Action = vsFindAction.vsFindActionReplaceAll
    ' findWin.Execute()
End Sub
public void OnConnection(object application, ext_ConnectMode 
  connectMode, object addInInst, ref Array custom)
{
    _applicationObject = (DTE2)application;
    _addInInstance = (AddIn)addInInst;
    searchReplace(_applicationObject);
}

public void searchReplace(DTE2 dte)
{
    Find2 findWin;
    Document doc;
    TextDocument textDoc;
    TextSelection textSel;
    int iCtr;

    // Create a new text file.
    dte.ItemOperations.NewFile("General\\Text File"
      ,"New file",Constants.vsViewKindTextView);

    // Set up references for the text document, Find object, and
    // TextSelection object.
    doc = dte.ActiveDocument;
    textDoc = (TextDocument) doc.Object("TextDocument");
    textSel = textDoc.Selection;
    findWin = (Find2) dte.Find;
    // Make sure all docs are searched before displaying results.
    findWin.WaitForFindToComplete = true;

    // Insert ten lines of text.
    for(iCtr=1; iCtr<=10; iCtr++)
    {
        textDoc.Selection.Text = "This is a test"+Environment.NewLine;
    }
    textDoc.Selection.Text = "This is a different word";

    // Uses FindReplace to find all occurrences of the word, test, in 
    // the document.
   System.Windows.Forms.MessageBox.Show(
     "Now changing all occurrences of 'test' to 'replacement'.");
   findWin.FindReplace(vsFindAction.vsFindActionReplaceAll, "test", 
     (int) vsFindOptions.vsFindOptionsFromStart, "replacement", 
     vsFindTarget.vsFindTargetCurrentDocument, "", 
     "",vsFindResultsLocation.vsFindResultsNone);

   // Uses Find2.Execute to find the word, different, in the document.
   // findWin.FindWhat = "different"
   // findWin.MatchCase = True
   // findWin.Execute()

   // Uses Find2.Execute to replace all occurrences of the word, Test, 
   // with the word, replacement.
   // findWin.FindWhat = "test"
   // findWin.ReplaceWith = "replacement"
   // findWin.Action = vsFindAction.vsFindActionReplaceAll
   // findWin.Execute()
}

参照

処理手順

方法 : オートメーション オブジェクト モデルのコード例をコンパイルおよび実行する

方法 : コード エディターを制御する (Visual Basic)

方法 : アドインを作成する

チュートリアル : ウィザードの作成

概念

オートメーション オブジェクト モデルの階層図

その他の技術情報

環境ウィンドウの作成と制御

アドインおよびウィザードの作成

オートメーションと機能拡張のリファレンス