在 WPF 中查看 Word 文档

本文关键字:Word 文档 WPF | 更新日期: 2023-09-27 18:30:14

我正在尝试使用此代码在 WPF 中查看 Word 文档进行编程我在这条行中有此错误public partial class MainWindow : Window
消息显示"窗口"是"System.Windows.Window"和"Microsoft.Office.Interop.Word.window"之间的模糊引用。如何纠正它?

private void BrowseButton_Click(object sender, RoutedEventArgs e)
{
    // Create OpenFileDialog
    Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
    // Set filter for file extension and default file extension
    dlg.DefaultExt = ".doc";
    dlg.Filter = "Word documents (.doc)|*.doc";
    // Display OpenFileDialog by calling ShowDialog method
    Nullable<bool> result = dlg.ShowDialog();
    // Get the selected file name and display in a TextBox
    if (result == true)
    {
        if (dlg.FileName.Length > 0)
        {
            SelectedFileTextBox.Text = dlg.FileName;
            string newXPSDocumentName =
    String.Concat(System.IO.Path.GetDirectoryName(dlg.FileName), "''",
                            System.IO.Path.GetFileNameWithoutExtension(dlg.FileName), ".xps");
            // Set DocumentViewer.Document to XPS document
            documentViewer1.Document =
                ConvertWordDocToXPSDoc(dlg.FileName, 
    newXPSDocumentName).GetFixedDocumentSequence();
        }
    }
}

在 WPF 中查看 Word 文档

别名单词的Window类。

using WordWindow = Microsoft.Office.Interop.Word.Window;
using Window = System.Windows.Window;

然后从 Word 更改使用Window的位置以使用新的别名WordWindow

它去哪里的例子:

...
using System.IO;
using Microsoft.Office.Interop.Word;
using Microsoft.Win32;
using WordWindow = Microsoft.Office.Interop.Word.Window;
using Window = System.Windows.Window;