Attaching a Solution to a Document Using Server Document

A common attaching scenario involves document processing. In this scenario, you receive an invoice from a customer and need to process it by uploading the data to a database in your company. The customer sends you an Office document, and you then attach an existing VSTO solution to the document. Now the VSTO customization runs whenever you open the document.

Or suppose you've created functionality that you want to use on any document. For example, if you have created a customization for formatting financial tables in a Word document, you can attach this code to any document and use these tools. Listing 13.2 shows how to attach a VSTO solution to a document.

❖ Note If the document does not have the Runtime Storage Control embedded, then the Word or Excel application will be started. This is important to keep in mind if you are attaching the solutions on a server where Office may not be installed.

Listing 13.2. Attaching a VSTO solution to a document

Imports Microsoft.VisualStudio.Tools.Applications.Runtime

Module Module1

Sub Main()

' Path to VSTO document.

Dim documentPath As String = "C:\Invoice\Invoice.xls" Dim assemblyName As String = _

"C:\Invoice\Invoice_1.0.0.0\Invoice.dll" Dim deploymentManifestPath As String = _

"C:\Invoice\Invoice.application" Dim applicationVersion As String = "1.0.0.0" Dim makePathsRelative As Boolean = False

' Attach the VSTO solution to the document. AttachSolution( _

documentPath, _ assemblyName, _ deploymentManifestPath, _ applicationVersion, _ makePathsRelative)

End Sub

Sub AttachSolution( _

ByVal documentPath As String, _ ByVal assemblyName As String, _

ByVal deploymentManifestPath As String, _ ByVal applicationVersion As String, _ ByVal makePathsRelative As Boolean)

' Check whether it is already attached.

If Not ServerDocument.IsCustomized(documentPath) Then

' Attach the solution to the document. ServerDocument.AddCustomization( _ documentPath, _ assemblyName, _ deploymentManifestPath, _ applicationVersion, _ makePathsRelative)

End If End Sub End Module

0 0

Post a comment