방법: 문서에 관리 코드 확장 연결

사용자 지정 어셈블리를 기존의 Microsoft Office Word 문서 또는 Microsoft Office Excel 통합 문서에 연결할 수 있습니다.문서 또는 통합 문서의 Microsoft Office 프로젝트와 개발 도구 Visual Studio 의해 지원 되는 파일 형식에서 수 있습니다.자세한 내용은 문서 수준 사용자 지정 아키텍처을 참조하십시오.

적용 대상: 이 항목의 정보는 Excel 2013 및 Excel 2010, Word 2013 및 Word 2010의 문서 수준 프로젝트에 적용됩니다. 자세한 내용은 Office 응용 프로그램 및 프로젝트 형식에 따라 사용 가능한 기능을 참조하십시오.

사용자 지정을 Word 또는 Excel 문서에 연결하려면 ServerDocument 클래스의 AddCustomization 메서드를 사용합니다.ServerDocument 클래스는 Microsoft Office가 설치되어 있지 않은 컴퓨터에서 실행하도록 디자인되었으므로 콘솔 또는 Windows Forms 응용 프로그램과 같이 Microsoft Office 개발과 직접 관련되지 않은 솔루션에서 이 메서드를 사용할 수 있습니다.

[!참고]

코드에서 컨트롤을 지정된 문서에 없는 것으로 예상하는 경우 사용자 지정이 로드되지 않습니다.

비디오에 링크 관련 비디오 데모를 보려면 How Do I: Attach or Detach a VSTO Assembly from a Word Document?를 참조하십시오.

문서에 관리 코드 확장을 연결하려면

  1. 프로젝트에서 Microsoft Office 콘솔 응용 프로그램 또는 Windows Forms 프로젝트 같이 있지 않아도, Microsoft.VisualStudio.Tools.Applications.ServerDocument.dll 및 Microsoft.VisualStudio.Tools.Applications.Runtime.dll 어셈블리에 대 한 참조를 추가 합니다.

  2. 코드 파일의 맨 위에 다음 Imports or using 문을 추가합니다.

    Imports Microsoft.VisualStudio.Tools.Applications
    Imports Microsoft.VisualStudio.Tools.Applications.Runtime
    
    using Microsoft.VisualStudio.Tools.Applications;
    using Microsoft.VisualStudio.Tools.Applications.Runtime;
    
  3. 정적 AddCustomization 메서드를 호출합니다.

    다음 코드 예제에서는 AddCustomization 오버로드를 사용합니다.이 오버로드에서는 문서의 전체 경로 및 문서에 연결할 사용자 지정에 대한 배포 매니페스트의 위치를 지정하는 Uri를 사용합니다.이 예제에서는 WordDocument1.docx라는 Word 문서가 데스크톱에 있고 배포 매니페스트가 데스크톱에 있는 Publish라는 폴더에 있다고 가정합니다.

    Dim documentPath As String = System.Environment.GetFolderPath( _
         Environment.SpecialFolder.Desktop) + "\WordDocument1.docx"
    Dim runtimeVersion As Integer = 0
    
    Try
        runtimeVersion = ServerDocument.GetCustomizationVersion(documentPath)
    
        ' Make sure that this document does not yet have any Visual Studio Tools 
        ' for Office customizations.
        If runtimeVersion = 0 Then
            Dim deployManifestPath As String = System.Environment.GetFolderPath( _
                Environment.SpecialFolder.Desktop) & "\Publish\WordDocument1.vsto"
            Dim deploymentManifestUri As New Uri(deployManifestPath)
            ServerDocument.AddCustomization(documentPath, deploymentManifestUri)
            System.Windows.Forms.MessageBox.Show("The document was successfully customized.")
        Else
            System.Windows.Forms.MessageBox.Show("The document is already customized.")
        End If
    Catch ex As FileNotFoundException
        System.Windows.Forms.MessageBox.Show("The specified document does not exist.")
    Catch ex As DocumentNotCustomizedException
        System.Windows.Forms.MessageBox.Show("The document could not be customized." & _
            vbLf & ex.Message)
    End Try
    
    string documentPath = System.Environment.GetFolderPath(
        Environment.SpecialFolder.Desktop) + @"\WordDocument1.docx";
    int runtimeVersion = 0;
    
    try
    {
        runtimeVersion = ServerDocument.GetCustomizationVersion(documentPath);
    
        // Make sure that this document does not yet have any Visual Studio Tools 
        // for Office customizations.
        if (runtimeVersion == 0)
        {
            string deployManifestPath = System.Environment.GetFolderPath(
                Environment.SpecialFolder.Desktop) + @"\Publish\WordDocument1.vsto";
    
            Uri deploymentManifestUri = new Uri(deployManifestPath);
            ServerDocument.AddCustomization(documentPath, deploymentManifestUri);
            System.Windows.Forms.MessageBox.Show("The document was successfully customized.");
        }
        else
        {
            System.Windows.Forms.MessageBox.Show("The document is already customized.");
        }
    }
    catch (FileNotFoundException)
    {
        System.Windows.Forms.MessageBox.Show("The specified document does not exist.");
    }
    catch (DocumentNotCustomizedException ex)
    {
        System.Windows.Forms.MessageBox.Show("The document could not be customized.\n" +
            ex.Message);
    }
    
  4. 프로젝트를 빌드하고 사용자 지정을 연결하려는 컴퓨터에서 응용 프로그램을 실행합니다.컴퓨터 Visual Studio 2010 도구 Office 런타임이 설치 되어 있어야 합니다.

참고 항목

작업

방법: 문서에서 관리 코드 확장 제거

개념

ServerDocument 클래스를 사용하여 서버의 문서 관리

Office 솔루션의 응용 프로그램 및 배포 매니페스트