当前位置:  开发笔记 > 编程语言 > 正文

C#中的Word自动化.使用SaveAs时出错

如何解决《C#中的Word自动化.使用SaveAs时出错》经验,为你挑选了1个好方法。

Save as Document Object尝试在C#中实现单词自动化时,我收到以下错误:

System.Runtime.InteropServices.COMException

(0x80020005):类型不匹配.(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

文件名)在D:\ dotnet\WordAutomation\TestWordAutomation\TestWordAutomation\Form1.cs:第246行

   at TestWordAutomation.Form1.button4_Click(Object

发件人,EventArgs e)在D:\ dotnet\WordAutomation\TestWordAutomation\TestWordAutomation\Form1.cs:第557行

我这样称呼Save As方法:

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

任何人都可以帮我解决这个问题吗?



1> kenny..:

"很痛苦.我记得使用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 );

推荐阅读
虎仔球妈_459
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有