Link to home
Start Free TrialLog in
Avatar of MartynAdams
MartynAdamsFlag for United Kingdom of Great Britain and Northern Ireland

asked on

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.
Avatar of khan_webguru
khan_webguru
Flag of Australia image

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

https://www.experts-exchange.com/questions/26913857/Open-Excel-from-ASP-Net.html

Regards,

AAK
Avatar of MartynAdams

ASKER

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.
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
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?
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
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
ASKER CERTIFIED SOLUTION
Avatar of MartynAdams
MartynAdams
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Happy to hear that best of luck with your project :)
I found the solution myself. I can't justifiably award myself credit for this.