Visual Studio Tools for Office: Using Visual Basic 2005 with Excel, Word, Outlook, and InfoPath

The ServerDocument object was aptly named. It was primarily designed for exactly the scenario we have just explored: writing information into a document on a server. It can do a lot more, however, from reading the data back out of a document to updating the deployment information inside a document to adding customizations to documents. We discuss the portions of the ServerDocument object model used in deployment scenarios in Chapter 20, "Deployment," and spend the rest of this chapter describing the data-manipulating tools in the ServerDocument in more detail.

Let's take a look at another illustrative use of the ServerDocument object; then we'll give a more complete explanation of all its data properties and methods. Listing 18.4 gives a handy console application that dumps out the cached data manifest and serialized cached data in a document.

Listing 18.4. Creating a Cache Viewer with ServerDocument

Imports Microsoft.VisualStudio.Tools.Applications.Runtime Imports System Imports System.IO Imports System.Text Module Module1 Sub Main(ByVal args As String()) If args.Length <> 1 Then Console.WriteLine("Usage:") Console.WriteLine(" CacheViewer.exe myfile.doc") Return End If Dim filename As String = args(0) Dim doc As ServerDocument = Nothing Try doc = New ServerDocument(filename, False, FileAccess.Read) Console.WriteLine(vbCrLf & "Cached Data Manifest") Console.WriteLine(doc.CachedData.ToXml()) Dim view As CachedDataHostItem For Each view In doc.CachedData.HostItems Dim item As CachedDataItem For Each item In view.CachedData If item.Xml <> Nothing And item.Xml.Length <> 0 Then Console.WriteLine(vbCrLf & "Cached Data: " & _ view.Id & "." & item.Id & " xml" & vbCrLf) Console.WriteLine(item.Xml) End If If item.Schema <> Nothing And item.Schema.Length <> 0 Then Console.WriteLine(vbCrLf & "Cached Data: " & _ view.Id & "." & item.Id & " xsd" & vbCrLf) Console.WriteLine(item.Schema) End If Next Next Catch ex As CannotLoadManifestException Console.WriteLine("Not a customized document:" + filename) Console.WriteLine(ex.Message) Catch ex As FileNotFoundException Console.WriteLine("File not found:" + filename) Catch ex As Exception Console.WriteLine("Unexpected Exception:" + filename) Console.WriteLine(ex.ToString()) Finally If Not doc Is Nothing Then doc.Close() End If End Try End Sub End Module

After you compile this into a console application, you can run the console application on the command line and pass the name of the document you want to view. The document must have a saved VSTO data island in it for anything interesting to happen.

Now that you have an idea of how the ServerDocument object model is used, we can talk about it in more detail.

Категории