在WPF中查看Word文档

时间:2014-11-13 19:11:59

标签: c# .net wpf

我正在尝试使用此代码在WPF中编程查看word文档 我在这一行public partial class MainWindow : Window中有这个错误 消息显示' Window'是一个模糊的参考之间的' System.Windows.Window'和' Microsoft.Office.Interop.Word.window&#39 ;.怎么能纠正它?

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();
        }
    }
}

1 个答案:

答案 0 :(得分:3)

Alias Word&#39; 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;