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);
         _Document.Activate();
         _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.
MartynAdamsAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

khan_webguruCommented:
Hello,

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".

http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/Q_26913857.html

Regards,

AAK
0
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.
0
MartynAdamsAuthor Commented:
This is not a Web application so changing the Web Config file is n/a. What about the App Config?
0
Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

khan_webguruCommented:
Just forget about that and look about all other steps and second message too
0
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?
0
khan_webguruCommented:
Hi,

use the Ex versions, otherwise you will be in trouble when more than 2GB is present:
 
[DllImport("kernel32.dll")]
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

http://www.codeproject.com/Messages/3010770/Error-Attempted-to-read-or-write-protected-memory.aspx

Please find in FAQ section hope this will help you

another link
http://social.msdn.microsoft.com/forums/en-US/clr/thread/1b6864bd-0cb4-415a-a164-b75efb15fd29
0
khan_webguruCommented:
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
0
MartynAdamsAuthor 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.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
khan_webguruCommented:
Happy to hear that best of luck with your project :)
0
MartynAdamsAuthor Commented:
I found the solution myself. I can't justifiably award myself credit for this.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.