**EDIT**- Please see my next post in this thread for specific steps to reproduce error***
We have a fairly complex Word document that we use here at my company for creating reports. While it contains a good amount of VBA code, it has never had any VSTO associated with it. My intention with this current project is to bring the doc "up to date" by replacing the VBA code with VSTO equivilants.
Unfortunately, I can't even get started. I go into Visual Studio 2005 and select "Create project". I select Office --> Word Document, and click "OK". On the next dialog, I select "Copy an existing document", and navigate to the desired .doc file. Upon clicking OK, I get a popup reading "Attempted to read or write protected memory. This is often an indication that other memory is corrupt." At the bottom, it then says "Project Creation Failed".
Some notes:
- I have tried this after a clean reboot.
- I am able to successfully create VSTO projects based off of blank Word documents.
- If I create a new VSTO project off of a blank document, and then replace the default blank document with my desired file, the project does not compile, with the same error.
- Found a post advising me to run a repair on VSTO using C:\Program Files\Common Files\Microsoft Shared\vsto\8.0\Microsoft Visual Studio 2005 Tools for Office Runtime\install.exe. Did this and it did not solve the problem.
System / Program Info:
-Visual Studio version is 8.0.50727.42
- .NET Framework version is 2.0.50727
- Windows version 5.1.2600
- Word version 11.0 build 6502
Thanks,
Matt Gutberlet

Using existing word doc in VSTO project (VS2005) Causes "Attempted to read or write protected memory" error
doonm
From what you've said, the document you're basing the project off of, has VBA behind it. The issue is that when the project opens in VS, all the macros triggers as if you were opening the document outside Studio.
You may need to change your security settings in Word, you do this outside of studio, just in a "Word session" on it's own, as follows:
Under Tools->Options->Security->Macro Security
change it to Medium ...
Now when you open the document inside Studio, when prompted to execute the marcos, answer "No" ...
That might do it ... but don't hold me to that ...
Pinky98
Matt,
thank you for your investigation. This is a previously unknown issue in VSTO 2005 that I logged in our database. For now, you would have to remove the linked properties prior to creating the project.
Thank you very much for your feedback!
iouri
---
This posting is provided AS-IS and confers no rights.
John Szurek
I have isolated the problem, and can reproduce it consistantly. The problem occurs when you open your document outside of Visual Studio, then create a bookmark and link a custom property to that bookmark's value.
To reproduce:
1. Create a new Word VSTO project based off of a new blank Word Document. Let the project finish creating, then immediately close Visual Studio; do not touch the doc.
2. Open the created Word document via Windows Explorer, outside of Visual Studio.
3. Type some dummy text. Select some of your dummy text and Insert a Bookmark. The bookmark brackets should be visible on your screen.
4. Go to File --> Properties --> Custom. Type the name of a new custom property, and check "Link to content". Set its Source to the bookmark you created in step 3. Click Add. Then click OK. Save the document and exit.
5. Re-Open the VSTO project you created in step 1. Attempt to build.
The same thing will happen if you create a new Word Document independently with a linked custom property, then attempt to create a VSTO project based off of it.
Interesting Notes:
-If you add the bookmark from inside the Visual Studio IDE immediately after the new Word doc is created and presented, and then close and add the custom linked property externally, the problem does not occur.