c# - 在 WPF 中查看 Word 文档

标签 c# .net wpf

我正在尝试编程以在 WPF 中查看 Word 文档,我使用此代码 我在这一行中遇到此错误publicpartialclassMainWindow:Window
消息显示“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();
        }
    }
}

最佳答案

Alias 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;

关于c# - 在 WPF 中查看 Word 文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26916575/

相关文章:

c# - 在 .Net WPF/DirectX 中显示视频信号

c# - “目标帐户名称不正确”-DirectoryInfo.GetFiles 出现 IOException

.net - 提高 NHibernate 性能的最佳方法是什么?

c# - 取消不接受 CancellationToken 的异步操作的正确方法是什么?

c# - 如何通过提供ID列表从列表中查询对象

c# - WPF - 两个不同 ViewModel 上的绑定(bind)和命令

c# - 在数据集中绑定(bind)检索到的 excel 列时,检索除最后一列之外的所有列

c# - 事件是否在另一个线程中运行? (.Net Compact Framework)

c# - 从另一个集合更新一个 observableCollection

c# - 如何读取没有 LINES 属性的 xaml 文本框的最后一行