C# 中的 Word 自动化。 使用另存为时出错
当尝试在 C# 中实现文字自动化时尝试另存为文档对象
时,我收到以下错误:
System.Runtime.InteropServices.COMException
(0x80020005):类型不匹配。 (HRESULT 异常:0x80020005 (DISP_E_TYPEMISMATCH))
在 Microsoft.Office.Interop.Word.DocumentClass.SaveAs(Object&
文件名、对象& 文件格式、对象和 LockComments、Object& 密码, 对象& AddToRecentFiles、对象& WritePassword、对象& ReadOnlyRecommended、对象& 嵌入TrueTypeFonts、对象和 SaveNativePictureFormat、对象和 SaveFormsData、对象和 另存为 AOCELetter、Object& 编码, 对象& InsertLineBreaks、对象& 允许替换、对象和 行尾、对象和 添加BiDiMarks)
在 TestWordAutomation.Form1.SaveAs(String
文件名) D:\ dotnet \ WordAutomation \ TestWordAutomation \ TestWordAutomation \ Form1.cs:行 246
在 TestWordAutomation.Form1.button4_Click(对象
sender, EventArgs e) 中 D:\ dotnet \ WordAutomation \ TestWordAutomation \ TestWordAutomation \ Form1.cs:行 557
我正在调用另存为
方法,如下所示:
MySaveAs("Doc1.doc");
MySaveAs("Doc2.doc");
MySaveAs("Doc3.doc");
//I have a MySaveAs function
public void MySaveAs(string fileName)
{
object FileName = null, FileFormat = null, LockComments = null, _Password = null, AddToRecentFiles = null, _WritePassword = null, _ReadOnlyRecommended = null, _EmbedTrueTypeFonts = null, SaveNativePictureFormat = null, _SaveFormsData = null, SaveAsAOCELetter = null, Encoding = null, InsertLineBreaks = null, AllowSubstitutions = null, LineEnding = null, AddBiDiMarks = null;
FileName = (object) fileName;
oDoc.SaveAs(ref FileName, ref FileFormat, ref LockComments, ref _Password, ref AddToRecentFiles,
ref _WritePassword, ref _ReadOnlyRecommended, ref _EmbedTrueTypeFonts,
ref SaveNativePictureFormat, ref _SaveFormsData, ref SaveAsAOCELetter, ref Encoding,
ref InsertLineBreaks, ref AllowSubstitutions, ref LineEnding, ref AddBiDiMarks);
}
有人可以帮我解决这个问题吗?
I am getting the following error when trying to Save as Document Object
while trying to implement a word automation in C#:
System.Runtime.InteropServices.COMException
(0x80020005): Type mismatch.
(Exception from HRESULT: 0x80020005
(DISP_E_TYPEMISMATCH))at Microsoft.Office.Interop.Word.DocumentClass.SaveAs(Object&
FileName, Object& FileFormat, Object&
LockComments, Object& Password,
Object& AddToRecentFiles, Object&
WritePassword, Object&
ReadOnlyRecommended, Object&
EmbedTrueTypeFonts, Object&
SaveNativePictureFormat, Object&
SaveFormsData, Object&
SaveAsAOCELetter, Object& Encoding,
Object& InsertLineBreaks, Object&
AllowSubstitutions, Object&
LineEnding, Object& AddBiDiMarks)at TestWordAutomation.Form1.SaveAs(String
fileName) in
D:\dotnet\WordAutomation\TestWordAutomation\TestWordAutomation\Form1.cs:line
246at TestWordAutomation.Form1.button4_Click(Object
sender, EventArgs e) in
D:\dotnet\WordAutomation\TestWordAutomation\TestWordAutomation\Form1.cs:line
557
I am calling the Save As
method like so:
MySaveAs("Doc1.doc");
MySaveAs("Doc2.doc");
MySaveAs("Doc3.doc");
//I have a MySaveAs function
public void MySaveAs(string fileName)
{
object FileName = null, FileFormat = null, LockComments = null, _Password = null, AddToRecentFiles = null, _WritePassword = null, _ReadOnlyRecommended = null, _EmbedTrueTypeFonts = null, SaveNativePictureFormat = null, _SaveFormsData = null, SaveAsAOCELetter = null, Encoding = null, InsertLineBreaks = null, AllowSubstitutions = null, LineEnding = null, AddBiDiMarks = null;
FileName = (object) fileName;
oDoc.SaveAs(ref FileName, ref FileFormat, ref LockComments, ref _Password, ref AddToRecentFiles,
ref _WritePassword, ref _ReadOnlyRecommended, ref _EmbedTrueTypeFonts,
ref SaveNativePictureFormat, ref _SaveFormsData, ref SaveAsAOCELetter, ref Encoding,
ref InsertLineBreaks, ref AllowSubstitutions, ref LineEnding, ref AddBiDiMarks);
}
Can any one help me to resolve this ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
太痛苦了。 我记得是通过 Word/Excel Interop 实现的。 无意轻率,但其中一个参数的类型不正确。 希望以下内容有所帮助,但我做了一些此类定义。
'Tis painful. I remember getting that working with the Word/Excel Interop. Not to be flippant, but one of the parameters has an incorrect type. Hopefully the following helps, but I did a number of these types of definitions.
您不能将 null 传递到任何互操作函数调用中 - 请改用 Missing.Value (按照 Kenny 的建议)
You cannot pass null into any of the interop function call - use Missing.Value instead (as suggested by Kenny)