Freigeben über


Using Word Automation Services with the Open XML SDK using VB

This is the complete sample that shows how to use Word Automation Services with the Open XML SDK from the article Developing with SharePoint 2010 Word Automation Services.

This blog is inactive.
New blog: EricWhite.com/blog

Blog TOCNote for those of you using the SP2010 beta, the capability to update fields (and update the table of contents) was not working in that release. It works in recent builds, and of course will work in RTM.

Imports System.Collections.ObjectModel
Imports System.IO
Imports System.Threading
Imports Microsoft.SharePoint
Imports Microsoft.Office.Word.Server.Conversions
Imports DocumentFormat.OpenXml.Packaging
Imports DocumentFormat.OpenXml.Wordprocessing
Module Module1
    Sub Main()
        Dim siteUrl As String = "https://localhost"
        ' If you manually installed Word Automation Services, then replace the name
        ' in the following line with the name that you assigned to the service when
        ' you installed it.
        Dim wordAutomationServiceName = "Word Automation Services"
        Using spSite As SPSite = New SPSite(siteUrl)
            Console.WriteLine("Querying for Test.docx")
            Dim list As SPList = spSite.RootWeb.Lists("Shared Documents")
    Dim query As SPQuery = New SPQuery()
            query.ViewFields = "<FieldRef Name='FileLeafRef' />"
            query.Query = ( _
               <Where>
                   <Eq>
                       <FieldRef Name='FileLeafRef'/>
               <Value Type='Text'>Test.docx</Value>
                   </Eq>
               </Where>).ToString()
            Dim collection As SPListItemCollection = list.GetItems(query)
            If collection.Count <> 1 Then
                Console.WriteLine("Test.docx not found")
                Environment.Exit(0)
            End If
            Console.WriteLine("Opening")
            Dim file As SPFile = collection(0).File
            Dim byteArray As Byte() = file.OpenBinary()
            Using memStr As MemoryStream = New MemoryStream()
                memStr.Write(byteArray, 0, byteArray.Length)
                Using wordDoc As WordprocessingDocument = _
                    WordprocessingDocument.Open(memStr, True)
                    Dim document As Document = wordDoc.MainDocumentPart.Document
                    Dim firstParagraph As Paragraph = _
                        document.Body.Elements(Of Paragraph)().FirstOrDefault()
                    If firstParagraph IsNot Nothing Then
                      Dim newParagraph As Paragraph = New Paragraph( _
                            New ParagraphProperties( _
                                New ParagraphStyleId() With {.Val = "Heading1"}), _
                            New Run( _
                           New Text("About the Author")))
                        Dim aboutAuthorParagraph As Paragraph = New Paragraph( _
                            New Run( _
                                New Text("Eric White")))
                        firstParagraph.Parent.InsertBefore(newParagraph, firstParagraph)
                        firstParagraph.Parent.InsertBefore(aboutAuthorParagraph, _
                                                           firstParagraph)
                    End If
                End Using
                Console.WriteLine("Saving")
                Dim linkFileName As String = file.Item("LinkFilename")
                file.ParentFolder.Files.Add(linkFileName, memStr, True)
            End Using
            Console.WriteLine("Starting conversion job")
            Dim job As ConversionJob = New ConversionJob(wordAutomationServiceName)
            job.UserToken = spSite.UserToken
            job.Settings.UpdateFields = True
            job.Settings.OutputFormat = SaveFormat.Document
           job.AddFile(siteUrl + "/Shared%20Documents/Test.docx", _
                siteUrl + "/Shared%20Documents/TestWithNewToc.docx")
            job.Start()
            Console.WriteLine("After starting conversion job")
            While True
                Thread.Sleep(5000)
                Console.WriteLine("Polling...")
                Dim status As ConversionJobStatus = New ConversionJobStatus( _
                    wordAutomationServiceName, job.JobId, Nothing)
                If status.Count = status.Succeeded + status.Failed Then
                    Console.WriteLine("Completed, Successful: {0}, Failed: {1}", _
                                      status.Succeeded, status.Failed)
                    Exit While
                End If
            End While
        End Using
    End Sub
End Module