打开docx时出现System.AccessViolationException [英] System.AccessViolationException when open docx

查看:69
本文介绍了打开docx时出现System.AccessViolationException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用MS Office Word的另存为"功能将docx转换为doc.
我写了一个简单的测试代码,如下所示,
但它始终满足''System.AccessViolationException:尝试读取或写入受保护的内存.这通常表明其他内存已损坏.''
有谁知道为什么会发生异常? 请给我一些建议,谢谢!!

p.s.我不确定是否会发生这种情况,因为我的域帐户在磁盘C上没有完全特权.我无法在C中打开MSOCache文件夹.

I try to convert a docx to doc, using MS Office Word''s "save as" function.
I write a simple testing code like below,
but it always meet a ''System.AccessViolationException:Attempted to read or write protected memory. This is often an indication that other memory is corrupt.'' during runtime.
Does anyone know why the exception would occur??
Please give me some advice, thanks!!

p.s. I am not sure whether it occurs because my domain account doesn''t have full privilege on disk C. I can''t open the MSOCache folder in C.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Core;
using Microsoft.Office.Interop.Word;
namespace _2007Convert2003
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Microsoft.Office.Interop.Word.ApplicationClass oWord = new ApplicationClass();
                object oMissing = Type.Missing;
                object fileName = @"D:\Test\Combo.docx";
                Document oDoc = oWord.Application.Documents.Open(ref fileName, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
                object fileName2 = @"D:\Test\Combo2.doc";
                object fileFormat = WdSaveFormat.wdFormatDocument97;
                oDoc.SaveAs(ref fileName2, ref fileFormat, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
                oDoc.Close(ref oMissing, ref oMissing, ref oMissing);
                oWord = null;
                Console.WriteLine("Done");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            Console.Read(); 
        }
    }
}

推荐答案

我找到了原因. 在我的项目中,我为Word 12.0引用了.NET单词.dll,
但是我在PC上安装了Office 2000(Word 9.0).
这会导致.dll和.exe的依赖关系问题.
将Office更新到Office 2007之后,程序运行正常.
I have found the reason.
In my project, I reference the .NET word .dll for Word 12.0,
but I install Office 2000(Word 9.0) on my PC.
It causes the issue of the dependency of .dll and .exe.
After I update my Office to Office 2007, the program runs fine.


这篇关于打开docx时出现System.AccessViolationException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆