我正在尝试编程以在 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/