C# Office 2003 Interop code stops with AccessViolationException - but didn't before

This problem perplexes me. It's a program which was working fine, then the same code stopped working.
Something must have been 'updated' since.

It's a C# WinForms application being developed on VS 2010 under Vista.
It uses MS Office 2003 + Interop Libraries (ver. 11) (All Copy Local)

It is supposed to open a Word document, make changes to the text and then save the document to another location.

Code: -

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Interop.Word;

namespace Shared {
   class WordSession {
      public void Test(string FileName, string OldText, string NewText) {
         object X = System.Type.Missing;
         ApplicationClass _Word = new Microsoft.Office.Interop.Word.ApplicationClass();
         object DocumentName = FileName;
         object IsVisible = true;
         object IsReadOnly = true;
         object Format = WdOpenFormat.wdOpenFormatAuto;
         Document _Document = _Word.Documents.Open(ref DocumentName, ref X, ref IsReadOnly, ref X, ref X, ref X, ref X
               , ref X, ref X, ref Format, ref X, ref IsVisible, ref X, ref X, ref X, ref X);
         _Word.Visible = true;
         this.Document.SelectAllEditableRanges(ref _Missing);
         object Source = OldText;
         object Target = NewText;

       // This line fails with the AccessViolationException
         _Word.Selection.Find.Execute(ref Source, ref X, ref X, ref X, ref X, ref X, ref X, ref X, ref X
               , ref Target, ref wdReplaceAll, ref X, ref X, ref X, ref X);

This code used to work - in fact its released and live.
Now its stopped working on my development machine so I figure there's an obscure setting unset somewhere.
Anyone got any ideas?
Much appreciated.
Who is Participating?
MartynAdamsConnect With a Mentor Author Commented:
Thanks guys but I solved it in the end. I used the InvokeMethod rather than a direct call and it seems to work OK.

   object MatchCase = true;
   object MatchWholeWord = true;
   object MatchWildCards = false;
   object MatchSoundsLike = false;
   object nMatchAllWordForms = false;
   object Forward = true;
   object Format = false;
   object MatchKashilda = false;
   object MatchDiacritics = false;
   object MatchAlefHamza = false;
   object MatchControl = false;
   object ReadOnly = false;
   object Visible = true;
   object Replace = 2;
   object Wrap = 1;
   object[] Parameters = new object[] { FindText, MatchCase, MatchWholeWord, MatchWildCards, MatchSoundsLike, nMatchAllWordForms, Forward, Wrap, Format, ReplaceText, Replace, MatchKashilda, MatchDiacritics, MatchAlefHamza,MatchControl };
   _Word.Selection.Find.GetType().InvokeMember("Execute", System.Reflection.BindingFlags.InvokeMethod, null, _Word.Selection.Find, Parameters);

Thanks for your assistance anyway.

I provided solution regarding "MS.excel.introp" few days back in "Expert-Exchange"

please see the link below and perform all the actions step by step. Please see the top 2 "Msgs".



MartynAdamsAuthor Commented:
I discovered the InterOp ReadMe file quote: 3      Installing Office 2003 Service Pack 1 after installing the Office 2003 Primary Interop Assemblies may require a repair of the Office 2003 Primary Interop Assemblies

So I have just re-installed it - but still have the same problem.
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

MartynAdamsAuthor Commented:
This is not a Web application so changing the Web Config file is n/a. What about the App Config?
Just forget about that and look about all other steps and second message too
MartynAdamsAuthor Commented:
Hi - thanks for the help, but it didn't work.
I had rummage around to find Component Services program (its hidden under Vista), then I set everyone and network with full access as described in your posting - to both Microsoft Excel and Microsoft Word Document. I then re-booted (to ensure no remining tasks would interfere), re-checked the permissions and ran the code under VS again.

I got the same error:  Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

This error has been mentioned elsewhere but nothing I do seems to affect it.

Any other ideas?

use the Ex versions, otherwise you will be in trouble when more than 2GB is present:
public static extern void GlobalMemoryStatusEx(ref MEMORYSTATUSEX ms);	

public struct MEMORYSTATUSEX {
	/// <summary></summary>
	public int Length;			// DWORD
	/// <summary></summary>
	public int MemoryLoad;			// DWORD
	/// <summary></summary>
	public long TotalPhys;			// DWORDLONG
	/// <summary></summary>
	public long AvailPhys;			// DWORDLONG
	/// <summary></summary>
	public long TotalPageFile;		// DWORDLONG
	/// <summary></summary>
	public long AvailPageFile;		// DWORDLONG
	/// <summary></summary>
	public long TotalVirtual;		// DWORDLONG
	/// <summary></summary>
	public long AvailVirtual;		// DWORDLONG
	/// <summary></summary>
	public long AvailExtendedVirtual;	// DWORDLONG

Open in new window

Thats what I find from


Please find in FAQ section hope this will help you

another link
The problem can be fixed as follows:

Method 1 (recommended): Download the component in question from its respective web site and reinstall it. The component and interop assembly DLLs included in the installer are guaranteed to match each other.

Method 2: If reinstallation of the component is not possible, you should create a new interop assembly from the currently installed version of component dll using the command-line utility TLBIMP, and then use that assembly in your .NET application by placing it in the /Bin subdirectory. For example:

c:\>TLBIMP c:\path\asppdf.dll

got from : http://support.persits.com/show.asp?code=PS080121168
Happy to hear that best of luck with your project :)
MartynAdamsAuthor Commented:
I found the solution myself. I can't justifiably award myself credit for this.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.