我正在創建一個加載和編輯Word文檔的WPF應用程序。代碼如下:在WPF應用程序上管理Word文檔
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未安裝時的替代形式? 非常感謝!