Created On:  20 October 2012


Is there a way to open up and view a Microsoft Word document using the Windows Presentation Foundation (WPF) Document Viewer control?


The WPF Document Viewer control opens up and allows viewing of XPS documents.

The attached demo shows how this can be used to display Microsoft Word documents by using the Microsoft.Office.Interop.Word class to first convert the Word documents to XPS prior to viewing them.

*                                      WPFViewWord                                            *
*                                                                                             *
* This sample program demonstrates how to use the WPF Document Viewer control to view MS-Word *
* documents. The control supports the viewing of .XPS documents so this program uses the Word *
* Interop classes to open the word document and save it as an XPS document prior to opening   *
* it up with the Document Viewer.                                                             *
*                                                                                             *
* Run the application and click the Browse button to navigate and select a MS-Word document   *
* file for viewing. Once it is loaded, you can use the icons within the control to change the *
* viewing properties.                                                                         *

$set ilusing"System.IO"
$set ilusing"Microsoft.Office.Interop.Word"
$set ilusing"Microsoft.Win32"
$set ilusing"System.Windows.Xps.Packaging"
class-id mywpftest.Window1 is partial
   inherits type System.Windows.Window.
working-storage section.
method-id NEW.
procedure division.
   invoke self::InitializeComponent()
end method.
method-id ConvertWordDocToXPSDoc public.
local-storage section.
01 wordApplication type Microsoft.Office.Interop.Word.ApplicationClass.
01 templateFileAndPath object.
01 xpsDocNameTemp object.
01 wdsftemp object value type WdSaveFormat::wdFormatXPS.
01 doc type Document.
procedure division using wordDocName as string, xpsDocName as string
                   returning xpsDoc as type XpsDocument.
   set templateFileAndPath to wordDocName
   set xpsDocNameTemp to xpsDocName
*> Create a WordApplication and add Document to it
   set wordApplication to new type Microsoft.Office.Interop.Word.ApplicationClass
   invoke wordApplication::Documents::Add(templateFileandPath, type Type::Missing, type Type::Missing,
      type Type::Missing)
   set doc to wordApplication::ActiveDocument
*> You must make sure you have Microsoft.Office.Interop.Word.Dll version 12.
*> Version 11 or previous versions do not have WdSaveFormat.wdFormatXPS option
     invoke doc::SaveAs(xpsDocNameTemp, wdsftemp, type Type::Missing, type Type::Missing,
        type Type::Missing, type Type::Missing, type Type::Missing, type Type::Missing, type Type::Missing,
        type Type::Missing, type Type::Missing, type Type::Missing, type Type::Missing, type Type::Missing,
        type Type::Missing, type Type::Missing)
     invoke wordApplication::Quit(type Type::Missing, type Type::Missing, type Type::Missing)
     set xpsDoc to new XpsDocument(xpsDocName, type System.IO.FileAccess::Read)
   catch ex as type Exception
     declare str as string = ex::Message
     set xpsDoc to null
end method.
method-id BrowseButton_Click final private.
procedure division using by value sender as object e as type System.Windows.RoutedEventArgs.
*> Create OpenFileDialog
   declare dlg as type Microsoft.Win32.OpenFileDialog = new Microsoft.Win32.OpenFileDialog
*> Set filter for file extension and default file extension
   set dlg::DefaultExt to ".docx"
   set dlg::Filter to "Word documents (.docx)|*.docx"
*> Display OpenFileDialog by calling ShowDialog method
   declare result as condition-value = dlg::ShowDialog
*> Get the selected file name and display in a TextBox
   if result = true
      if dlg::FileName::Length > 0
         set SelectedFileTextBox::Text to dlg::FileName
         declare newXPSDocumentName as string = type String::Concat(type System.IO.Path::GetDirectoryName(dlg::FileName),
            "\\", type System.IO.Path::GetFileNameWithoutExtension(dlg::FileName), ".xps")
*> Set DocumentViewer.Document to XPS document
         set documentViewer1::Document to
            self::ConvertWordDocToXPSDoc(dlg::FileName, newXPSDocumentName)::GetFixedDocumentSequence
end method.
end class.