我正在创建一个加载和编辑 Word 文档的 WPF 应用程序。代码如下:
public bool Generate(string path, ProjectVO project)
{
saveAs = path;
projectName = project.Name;
projectVersion = project.Version;
object missing = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();
//Setup our Word.Document class we'll use.
Microsoft.Office.Interop.Word.Document aDoc = null;
// Check to see that file exists
if (File.Exists((string)fileName))
{
DateTime today = DateTime.Now;
object readOnly = false;
object isVisible = false;
try
{
//Set Word to be not visible.
wordApp.Visible = false;
//Open the word document
aDoc = wordApp.Documents.Open(ref fileName, ref missing,
ref readOnly, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref isVisible, ref missing, ref missing,
ref missing, ref missing);
// Activate the document
aDoc.Activate();
// Find Place Holders and Replace them with Values.
this.FindAndReplace(wordApp, "{Name}", projectName);
this.FindAndReplace(wordApp, "{Version}", projectVersion);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
//Close the document
aDoc.Close(ref missing, ref missing, ref missing);
return false;
}
}
else
{
return false;
}
//Save the document as the correct file name.
aDoc.SaveAs(ref saveAs, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing);
//Close the document
aDoc.Close(ref missing, ref missing, ref missing);
return true;
}
它工作正常,但是当客户端没有 Microsoft Word 时,不会创建 Word 文档。有没有办法创建它,并保存在一个文件夹中,即使客户端无法打开它?或者有没有办法将它保存为 PDF 或 TXT 作为未安装 Word 时的替代形式?非常感谢!