次の方法で共有


チュートリアル : アプリケーション レベルのプロジェクトでのサービスからのデータへのバインド

更新 : 2008 年 7 月

対象

このトピックの情報は、指定された Visual Studio Tools for Office プロジェクトおよび Microsoft Office のバージョンにのみ適用されます。

プロジェクトの種類

  • アプリケーション レベルのプロジェクト

Microsoft Office のバージョン

  • Word 2007

詳細については、「アプリケーションおよびプロジェクトの種類別の使用可能な機能」を参照してください。

Visual Studio 2008 Service Pack 1 (SP1) 以降、アプリケーション レベルのプロジェクトでは、ホスト コントロールにデータをバインドできるようになりました。このチュートリアルでは、Microsoft Office Word 文書へのコントロールの追加、MSDN Content Service から取得したデータへのコントロールのバインド、実行時におけるイベントへの応答を行う方法について説明します。

このチュートリアルでは、次の作業について説明します。

Cc668204.alert_note(ja-jp,VS.90).gifメモ :

お使いのマシンで、Visual Studio ユーザー インターフェイスの一部の要素の名前や場所が、次の手順とは異なる場合があります。これらの要素は、使用している Visual Studio のエディションや独自の設定によって決まります。詳細については、「Visual Studio の設定」を参照してください。

前提条件

このチュートリアルを実行するには、次のコンポーネントが必要です。

  • Visual Studio Tools for Office (Visual Studio 2008 Professional および Visual Studio Team System のオプションの要素)

    ここに挙げた Visual Studio のバージョンでは、Visual Studio Tools for Office が既定でインストールされます。インストールされているかどうかを確認する方法については、「Visual Studio Tools for Office のインストール」を参照してください。

  • Word 2007

新規プロジェクトの作成

まず、Word アドイン プロジェクトを作成します。

新しいプロジェクトを作成するには

  • Visual Basic または C# を使用して、MSDN Content Service という名前の Word 2007 アドイン プロジェクトを作成します。

    詳細については、「方法 : Visual Studio Tools for Office プロジェクトを作成する」を参照してください。

    Visual Studio は ThisAddIn.vb ファイルまたは ThisAddIn.cs ファイルを開き、ソリューション エクスプローラにプロジェクトを追加します。

Web サービスの追加

このチュートリアルでは、MSDN Content Service と呼ばれる Web サービスを使用します。この Web サービスは、指定された MSDN の記事の情報を XML 文字列またはプレーンテキストの形式で返します。後の手順で、コンテンツ コントロールに返された情報を表示する方法を説明します。

MSDN Content Service をプロジェクトに追加するには

  1. [データ] メニューの [新しいデータ ソースの追加] をクリックします。

  2. データ ソース構成ウィザードで、[サービス] をクリックし、[次へ] をクリックします。

  3. "アドレス" フィールドに、次の URL を入力します。

    http://services.msdn.microsoft.com/ContentServices/ContentService.asmx

  4. [移動] をクリックします。

  5. "名前空間" フィールドに「ContentService」と入力し、[OK] をクリックします。

  6. [データ ソース構成ウィザード] ダイアログ ボックスで、[完了] をクリックします。

実行時におけるコンテンツ コントロールの追加とデータへのバインド

アプリケーション レベルのプロジェクトでは、実行時にコントロールを追加してバインドできます。このチュートリアルでは、ユーザーがコントロール内でクリックすると Web サービスからデータを取得するように、コンテンツ コントロールを構成します。

コンテンツ コントロールを追加してデータにバインドするには

  1. ThisAddIn クラスで、MSDN Content Service、コンテンツ コントロール、およびデータ バインディングに使用する変数を宣言します。

    Private request As ContentService.getContentRequest
    Private proxy As ContentService.ContentServicePortTypeClient
    Private document As ContentService.requestedDocument()
    Private response As ContentService.getContentResponse
    Private appId As ContentService.appId
    Private WithEvents richTextContentControl _
        As Microsoft.Office.Tools.Word.RichTextContentControl
    Private components As System.ComponentModel.Container
    Private primaryDocumentsBindingSource As  _
        System.Windows.Forms.BindingSource
    
    private ContentService.getContentRequest request;
    private ContentService.ContentServicePortTypeClient proxy;
    private ContentService.requestedDocument[] document;
    private ContentService.getContentResponse response;
    private ContentService.appId appId;
    private Microsoft.Office.Tools.Word.RichTextContentControl
        richTextContentControl;
    private System.ComponentModel.Container components;
    private System.Windows.Forms.BindingSource
        primaryDocumentsBindingSource;
    
  2. ThisAddIn クラスに次のメソッドを追加します。このメソッドにより、アクティブな文書の先頭にコンテンツ コントロールが作成されます。

    Private Sub AddRichTextControlAtRange()
    
        Dim currentDocument As Word.Document = Me.Application.ActiveDocument
        currentDocument.Paragraphs(1).Range.InsertParagraphBefore()
    
        Dim extendedDocument As Document = currentDocument.GetVstoObject()
        richTextContentControl = _
            extendedDocument.Controls.AddRichTextContentControl _
            (currentDocument.Paragraphs(1).Range, "richTextControl2")
        richTextContentControl.PlaceholderText = _
            "Click here to download MSDN Library information about content controls."
    End Sub
    
    private void AddRichTextControlAtRange()
    {
        Word.Document currentDocument = this.Application.ActiveDocument;
        currentDocument.Paragraphs[1].Range.InsertParagraphBefore();
    
        Document extendedDocument = currentDocument.GetVstoObject();
        richTextContentControl = extendedDocument.Controls.AddRichTextContentControl(
            currentDocument.Paragraphs[1].Range, "richTextContentControl");
        richTextContentControl.PlaceholderText =
            "Click here to download MSDN Library information about content controls.";
    }
    
  3. ThisAddIn クラスに次のメソッドを追加します。このメソッドにより、Web サービスへの要求を作成して送信するのに必要なオブジェクトが初期化されます。

    Private Sub InitializeServiceObjects()
        request = New ContentService.getContentRequest()
        proxy = New ContentService.ContentServicePortTypeClient()
        document = New ContentService.requestedDocument(0) {}
        response = New ContentService.getContentResponse()
        appId = New ContentService.appId()
        components = New System.ComponentModel.Container()
        primaryDocumentsBindingSource = _
            New System.Windows.Forms.BindingSource(components)
    End Sub
    
    private void InitializeServiceObjects()
    {
        request = new ContentService.getContentRequest();
        proxy = new ContentService.ContentServicePortTypeClient();
        document = new ContentService.requestedDocument[1];
        response = new ContentService.getContentResponse();
        appId = new ContentService.appId();
        components = new System.ComponentModel.Container();
        primaryDocumentsBindingSource =
            new System.Windows.Forms.BindingSource(this.components);
    
    }
    
  4. ユーザーがコンテンツ コントロール内をクリックするとコンテンツ コントロールに関する MSDN ライブラリの文書を取得し、そのデータをコンテンツ コントロールにバインドするためのイベント ハンドラを作成します。

    Private Sub richTextContentControl_Entering _
        (ByVal sender As Object, ByVal e As ContentControlEnteringEventArgs) _
        Handles richTextContentControl.Entering
    
        document(0) = New ContentService.requestedDocument()
        With document(0)
            .type = ContentService.documentTypes.primary
            .selector = "Mtps.Xhtml"
        End With
    
        With request
            .contentIdentifier = "ed59e522-dd6e-4c82-8d49-f5dbcfcc950d"
            .locale = "en-us"
            .version = "VS.90"
            .requestedDocuments = document
        End With
    
        response = proxy.GetContent(appId, request)
    
        primaryDocumentsBindingSource.DataSource = _
            response.primaryDocuments(0).Any.InnerText
        richTextContentControl.DataBindings.Add( _
            "Text", _
            primaryDocumentsBindingSource.DataSource, _
            "", _
            True, _
            System.Windows.Forms.DataSourceUpdateMode.OnValidation)
    End Sub
    
    void richTextContentControl_Entering(object sender, ContentControlEnteringEventArgs e)
    {
        document[0] = new ContentService.requestedDocument();
        document[0].type = ContentService.documentTypes.primary;
        document[0].selector = "Mtps.Xhtml";
    
        request.contentIdentifier = "ed59e522-dd6e-4c82-8d49-f5dbcfcc950d";
        request.locale = "en-us";
        request.version = "VS.90";
        request.requestedDocuments = document;
    
        response = proxy.GetContent(appId, request);
    
        primaryDocumentsBindingSource.DataSource =
            response.primaryDocuments[0].Any.InnerText;
        richTextContentControl.DataBindings.Add(
            "Text",
            primaryDocumentsBindingSource.DataSource,
            "",
            true,
            System.Windows.Forms.DataSourceUpdateMode.OnValidation);
    }
    
  5. AddRichTextControlAtRange メソッドと InitializeServiceObjects メソッドを、ThisAddIn_Startup メソッドから呼び出します。C# を使用してプログラミングを行っている場合は、イベント ハンドラを追加します。

    Private Sub ThisAddIn_Startup(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Startup
        AddRichTextControlAtRange()
        InitializeServiceObjects()
    End Sub
    
    private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
        AddRichTextControlAtRange();
        InitializeServiceObjects();
        this.richTextContentControl.Entering +=
            new EventHandler<ContentControlEnteringEventArgs>
                (richTextContentControl_Entering);
    }
    

アドインのテスト

Word を開くと、RichTextContentControl コントロールが表示されます。コントロール内でクリックすると、コントロールのテキストが変更されます。

アドインをテストするには

  1. F5 キーを押します。

  2. コンテンツ コントロール内をクリックします。

    MSDN Content Service から情報がダウンロードされ、コンテンツ コントロール内に表示されます。

参照

概念

コントロールへのデータのバインド

履歴の変更

日付

履歴

理由

2008 年 7 月

トピックを追加

SP1 機能変更