在使用文件名调用SaveAs方法后,MS Word SaveAs对话框显示出来

时间:2015-08-15 07:51:11

标签: c# ms-word office-interop

在我的C#应用程序中,我使用Microsoft.Office.Interop.Word.Application将Rtf转换为Xml。

string tmpWordFullname = Path.GetTempFileName();
string fileContents = myRtfText2Convert;
File.WriteAllText(tmpWordFullname, fileContents);
Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();
var currentDoc = wordApp.Documents.Open(tmpWordFullname);
currentDoc.Activate();

string newTempName = Path.GetDirectoryName(tmpWordFullname) + "\\" +
                       Path.GetFileNameWithoutExtension(tmpWordFullname) + "1.xml";

currentDoc.SaveAs(newTempName, WdSaveFormat.wdFormatXML);

它在Office 2007上工作正常,但在以后的版本中,即使我已经给出了一个名字而且用户必须输入一个不会成为新名称的新名称,也会弹出saveas对话框。我已在newTempName中设置了一个。

事件知道如何解决它?

1 个答案:

答案 0 :(得分:0)

wordApp.DisplayAlerts = WdAlertLevel.wdAlertsNone; // wordApp.DisplayAlerts = false;

经过测试并完美运作

private void Button_Click(object sender, RoutedEventArgs e)
{
    string tmpWordFullname = System.IO.Path.GetTempFileName();
    string fileContents = "test test test";
    File.WriteAllText(tmpWordFullname, fileContents);
    Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();
    wordApp.DisplayAlerts = WdAlertLevel.wdAlertsNone;
    var currentDoc = wordApp.Documents.Open(tmpWordFullname);
    currentDoc.Activate();

    string newTempName = System.IO.Path.GetDirectoryName(tmpWordFullname) + "\\" + System.IO.Path.GetFileNameWithoutExtension(tmpWordFullname) + "1.xml";

    currentDoc.SaveAs(newTempName, WdSaveFormat.wdFormatXML);
}