C# 中的 Word 自动化。 使用另存为时出错

发布于 2024-07-08 17:21:51 字数 2215 浏览 6 评论 0原文

当尝试在 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
246

   at 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

身边 2024-07-15 17:21:51

太痛苦了。 我记得是通过 Word/Excel Interop 实现的。 无意轻率,但其中一个参数的类型不正确。 希望以下内容有所帮助,但我做了一些此类定义。

static object s_missing = System.Reflection.Missing.Value;
static object s_true = true;
static object s_false = false;
static object s_forcesave = Word.WdSaveOptions.wdSaveChanges;

static Word._Application s_app = null;
...
return s_app.Documents.Open ( ref filename,
   ref s_missing,
   ref s_missing,
   ref s_missing,
   ref s_missing,
   ref s_missing,
   ref s_missing,
   ref s_missing,
   ref s_missing,
   ref s_missing,
   ref s_missing,
   ref s_missing,
   ref s_missing,
   ref s_missing,
   ref s_missing,
   ref s_missing );

'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.

static object s_missing = System.Reflection.Missing.Value;
static object s_true = true;
static object s_false = false;
static object s_forcesave = Word.WdSaveOptions.wdSaveChanges;

static Word._Application s_app = null;
...
return s_app.Documents.Open ( ref filename,
   ref s_missing,
   ref s_missing,
   ref s_missing,
   ref s_missing,
   ref s_missing,
   ref s_missing,
   ref s_missing,
   ref s_missing,
   ref s_missing,
   ref s_missing,
   ref s_missing,
   ref s_missing,
   ref s_missing,
   ref s_missing,
   ref s_missing );
放我走吧 2024-07-15 17:21:51

您不能将 null 传递到任何互操作函数调用中 - 请改用 Missing.Value (按照 Kenny 的建议)

You cannot pass null into any of the interop function call - use Missing.Value instead (as suggested by Kenny)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文