Question

This is similar to my question here, but my workaround dosn't help for these instances and the cause is more defined. I have a workbook level customization that will add sheets from a folder where it retains the name of the sheet that was copied in. This works well and doesn't cause any problems unless the user renames the sheet. If the user renames the sheet, saves the workbook, and then come backs to it, then tries to do any action which references the workbook or worksheet they get the error:

Microsoft.VisualStudio.Tools.Applications.Runtime.ControlNotFoundException: 
This document might not function as expected because the following control is missing:
Sheet5. Data that relies on this control will not be automatically displayed or updated, 
and other custom functionality will not be available. Contact your administrator or the 
author of this document for further assistance. ---> 
System.Runtime.InteropServices.COMException: Programmatic access to the Microsoft Office 
Visual Basic for Applications project system could not be enabled. If Microsoft Office 
Word or Microsoft Office Excel is running, it can prevent programmatic access from being 
enabled. Exit Word or Excel before opening or creating your project.

Now closing and opening the Excel doesn't help, moving the sheet to a new position doesn't help, and deleting the offending sheet doesn't help if more than 1 sheet has been added. The base template has 6 sheets, and I've tried adding the sheets to the middle (where we would want them) and at the end (just for kicks). This doesn't seem to help. If you delete all the added sheets, save the workbook, and open it again, then functionality is restored. The sheets that are added are not named sheets in the VSTO, but are referenced mostly by number or name (there are 6 sheets to start with, so if they get added after 3 they would have an index of 4 to (ThisWorkBook.Sheets.Count-3), or if Sheet.Name is not any of the names from the original 6 (which they would never be, and I have scripted a prevention for).

My problem is that I really need to be able to allow the users to add a generic report sheet if the ones I've designed won't work. This means I need to be able to add a sheet, allow them to rename it, save it and come back to it. This error doesn't happen until the sheet has been saved, closed, and re-opened if that helps anyone. I also can not replicate this on any machine with VS2012 installed, even if the project was never worked on in that computer (VSTO just installed). Though all those VS2012 machines were used for other Excel VSTO projects. I can however replicate it on XP-Win8 and Excel Office '07-365.

**So my distilled question: is there a way to allow a user to add sheets and rename them (outside of the VSTO preferably), without running into a Runtime.ControlNotFoundException?

Thanks.

Was it helpful?

Solution

Basically, you need to re-initialize the sheets as VSTO objects everytime the workbook is re-opened. Since these sheets aren't hard-coded into your solution, VSTO has no way of recognizing them unless your code looks for such things and "re-connects" them at run-time. This is explained in the VSTO documentation: http://msdn.microsoft.com/en-us/library/cc442981.aspx

But I believe you require an add-in in order to do this for sheets. I don't think it can be incorporated into the workbook's "code behind".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top