Click here to Skip to main content
15,896,288 members
Articles / Productivity Apps and Services / Microsoft Office

Generate XPS from Word documents with VB 2008 and WPF

Rate me:
Please Sign up or sign in to vote.
2.77/5 (4 votes)
9 Mar 2008CPOL2 min read 79.2K   1.1K   21  
An alternative way to create XPS documents via Visual Basic code and VSTO assemblies.
'Creates XPS documents from Word document using VB 2008 and VSTO assemblies

'Source code provided "AS IS" by Alessandro Del Sole - http://community.visual-basic.it/Alessandro

'The author is not responsible for the usage, bad usage or damages deriving from improper usage
'of this source code.

Imports Microsoft.Office.Interop.Word
Imports Microsoft.Win32
Imports System.Windows.Xps.Packaging

Class Window1
    Private Function exportAsXps(ByVal fileName As String) As Boolean

        Try
            'Starts a background instance of Word 2007
            'and adds the specified document to the documents collection 
            Dim wordInstance As New Microsoft.Office.Interop.Word.Application
            wordInstance.Documents.Add(fileName)

            'Obtains the output file name, replacing the extension with.Xps
            Dim outputName As String = String.Concat(IO.Path.GetDirectoryName(fileName), "\", IO.Path.GetFileNameWithoutExtension(fileName), ".xps")

            'Retrieves the active document instance and saves specifying name and format
            Dim doc As Document = wordInstance.ActiveDocument
            doc.SaveAs(outputName, WdSaveFormat.wdFormatXPS)

            'Shuts down Word 2007
            wordInstance.Quit()

            'View the generated XPS Document
            Dim xpsDoc As New XpsDocument(outputName, IO.FileAccess.Read)
            DocumentViewer1.Document = xpsDoc.GetFixedDocumentSequence

            Return True

            'TODO: Add here specific exceptions
        Catch ex As Exception
            MessageBox.Show(ex.ToString, "", MessageBoxButton.OK, MessageBoxImage.Information)
            Return False
        End Try

    End Function

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button1.Click
        Dim ofd As New OpenFileDialog

        'The Win32 OpenFileDialog can return True, False or Nothing
        Dim result As Nullable(Of Boolean)

        With ofd
            .Title = "Select a Word document"
            .Filter = "Documents|*.docx;*.doc|All files|*.*"

            result = .ShowDialog

            'If the dialog returns True, starts exporting
            If result = True Then

                Dim exportResult As Boolean = exportAsXps(.FileName)

                'TODO: Add here check code
            End If
        End With

    End Sub
End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Other
Italy Italy
I'm a Microsoft Visual Basic MVP. I'm an Italian .NET developer and I write articles and books about Visual Basic, Visual Studio LightSwitch, and the .NET technologies.

Check out my blog at: http://community.visual-basic.it/AlessandroEnglish

Comments and Discussions