Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Word Interop AccessViolationException when saving

I want to write a program which will read in a whole bunch of word 97 files (.doc) and save them as .docx files. I'm restricted to .Net 2.0.

At this stage, I just want to get it working with my stub code - then I will write the GUI and logic to open multiple files in multiple locations, etc...

Here's what I have so far:

using MSWord   = Microsoft.Office.Interop.Word;
using MSPPoint = Microsoft.Office.Interop.PowerPoint;

then

OpenFileDialog ofd = new OpenFileDialog()
{
  CheckFileExists = true,
};

if (ofd.ShowDialog() != DialogResult.OK)
  return;

MSWord.Application app = new MSWord.Application();
MSWord.Document    doc = new MSWord.Document();

doc = app.Documents.Open(ofd.FileName);

try
{
  doc.SaveAs2(ofd.FileName + ".docx", MSWord.WdSaveFormat.wdFormatDocument);
}
catch (Exception ex)
{
  MessageBox.Show("Could not save because:\r\n" + ex.Message,
    ex.GetType().ToString());
}

doc.Close();
app.Quit();

return;

As far as I can tell, the word document is being opened. However, the SaveAs2() command seems to throw an AccessViolationException and the .docx is not saved.

Can someone please let me know what is wrong with the above code, why it's not saving, and how to fix it?

Thanks

like image 543
Ozzah Avatar asked Apr 03 '11 23:04

Ozzah


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C in C?

Originally Answered: What is the full form of C ? C - Compiler . C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell Labs.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.


1 Answers

You are stuck in DLL Hell. Only use SaveAs2() when you have Office 2010 installed on the machine. Any prior version is indeed going to bomb with an AccessViolation, the method isn't implemented. Using the proper PIA version would go a long way as well to avoid this problem, be sure to use the lowest version you are willing to support.

Use the SaveAs() method.

like image 98
Hans Passant Avatar answered Sep 29 '22 08:09

Hans Passant