2015-08-15 49 views
0

调用SaveAs方法后,MS Word SaveAs对话框出现,我正在使用Microsoft.Office.Interop.Word.Application将Rtf转换为Xml。在我的<code>C#</code>应用程序中使用文件名

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,但在以后的版本中,另存为对话框弹出,即使我已经给了一个名称,用户必须输入这不会是一个我有一个新名字设置在newTempName

事件知道如何解决它?

回答

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); 
} 
+0

这是'wordApp.DisplayAlerts = WdAlertLevel.wdAlertsNone;',它不是反正工作。 –

+0

它不适用于Office 2010及更高版本中的某些特定文件 –

相关问题