使用Microsoft.Interop.Word我尝试保存Word文件(docx)。在大多数情况下,它可以节省很多,但偶尔也没有。我无法弄清楚具体导致错误的原因。
根据当前相关数据生成文件和路径。在此(失败)示例中,输出文件路径(c#代码中的_fullpath进一步向下)是
G:\ Red Group \ Varme \ Tilbud_tilbudsprogram \ 108472_Test-客户A_S Projektafregning Kun直到HE Pro Kolding \ 108472_Test-Customer A_S Projektafregning Kun til HE Pro Kolding _201802062146.docx
错误消息为" 文件名无效 ..."
例外是:
at Microsoft.Office.Interop.Word.DocumentClass.SaveAs2(Object& FileName,Object& FileFormat,Object& LockComments,Object&密码, 对象和放大器; AddToRecentFiles,Object& WritePassword,Object& ReadOnlyRecommended,Object& EmbedTrueTypeFonts,Object& SaveNativePictureFormat,Object& SaveFormsData,Object& SaveAsAOCELetter,Object&编码,对象& InsertLineBreaks,Object& AllowSubstitutions,Object& LineEnding,Object& AddBiDiMarks,Object& CompatibilityTode)在Quotation.Word.OpenAndShow(String MergeFile, 字符串OutPath)在F:\ dbConsult \ Kunder \ Danfoss \ Quotation 2017 \ Danfoss中 Quotation \ Danfoss Quotation \ Classes \ WordMerge.cs:第333行
第333行是wordDoc.SaveAs()行。代码的那部分看起来像这样:
wordDoc.Activate();
wordApp.Visible = true;
wordApp.Activate();
try
{
wordDoc.SaveAs(_fullpath);
}
catch (Exception ex)
{
using (StreamWriter sw = File.CreateText(_fullpath))
{
sw.WriteLine("Hey");
}
Helper.Log(ex.Message, ex.StackTrace, "WordMerge.OpenAndShow()");
return false;
}
"使用"我抓到的部分是为了测试。如果我使用StreamWriter ,则文件应按创建。因此,我认为这是SaveAs()mehtod中的一个缺陷。这个结论似乎是对的吗?如果是这样的话:任何线索该做什么?
祝你好运 的Morten
答案 0 :(得分:0)
通过替换空格字符解决:替换(“”,“ - ”)。
我无法确切地知道是什么触发了这个,因为我有很多成功的保存而没有错误发生 - 以及我有空格的地方。
此外,所有失败的保存都适用于StreamWriter。所以它似乎是Word.Interop中的一些内部缺陷。
BR