Click here to Skip to main content
15,907,233 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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(); 
        }
    }
}
Posted

1 solution

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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900