C# 打开word 语法拼写错误太多 解决方案

在项目里有个把word转换成htm在网页显示的控件,同事说word拼写错误太多的时候就不能用了,我试了试还真是,原来的打开方式是

 Document doc = (Document)docsType.InvokeMember("Open",
            System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { fileName, true, true });

到这里就会出错,按理说应该是这个方法参数有忽略拼写错误的地方,但是就是没找着,于是换了个思路。

private static Document OpenEx(Application word, object fileName, Document doc)
    {
        object docPath = HttpContext.Current.Server.MapPath(tempDocPath);
        object objMissing = System.Reflection.Missing.Value;

        doc = word.Documents.Open(
            ref docPath,    //FileName
            ref objMissing,   //ConfirmVersions
            ref objMissing,   //ReadOnly
            ref objMissing,   //AddToRecentFiles
            ref objMissing,   //PasswordDocument
            ref objMissing,   //PasswordTemplate
            ref objMissing,   //Revert
            ref objMissing,   //WritePasswordDocument
            ref objMissing,   //WritePasswordTemplate
            ref objMissing,   //Format
            ref objMissing,   //Enconding
            ref objMissing,   //Visible
            ref objMissing,   //OpenAndRepair
            ref objMissing,  //DocumentDirection
            ref objMissing,  //NoEncodingDialog
            ref objMissing   //XMLTransform
            );
        doc.Activate();
        doc.SpellingChecked = false;//不检查语法
        doc.ShowSpellingErrors = false;

        object objFalse = false;
        object confirmConversion = false;
        object link = false;
        object attachment = false;
        word.Selection.InsertFile(
            fileName.ToString(),
            ref objMissing,
            ref confirmConversion,
            ref link,
            ref attachment
            );
        return doc;
    }

新建了一个方法,这个方法是打开一个空的文档,然后把原来的文档和他合并,这样能得到和要转换的文档一样的对象,接着操作就行了。

不得不说微软的API真坑爹

posted on 2012-08-06 11:36  加温  阅读(629)  评论(0编辑  收藏  举报

导航