Excel VBA project not getting closed

天涯浪子 提交于 2019-12-07 17:26:53

问题


I have a COM addin in which I am handling excel events. I click Alt F11 to view the VBA project related to an excel. However, when I close the workbook the Excel VBA project in the project explorer is not closed as well.

It gets closed normally when my COM addin is not installed. What is going on, and what should I do differently?


回答1:


The problem was with workbook objects remaining in the memory. By clearing the objects in the workbook deactivate event, the VBA project is getting closed when I close the workbook.

See this: http://social.msdn.microsoft.com/Forums/en-US/vsto/thread/377fbebc-021d-4e48-aaef-d4658cc221b3

Thanks




回答2:


I had a similar problem in my COM-Add-In (C# / VSTO):

  • You have to release all Workbook references in your code when the workbook is closed and set them to null.
  • If you internally store a reference to a workbook, always use WeakReference to let the Garbage Collector do its work.

In my case I had a Windows.Forms.Tree of all workbooks in a custom task pane. The referenced Workbook objects were stored in the TreeNode.Tag. After I had closed a workbook, the tree was updated by events, but at this time I had already generated what I've called a VBA Workbook "ghost". I solved this by replacing the Workbook-Objects in the TreeNode tags by the Workbook.FullName-String (to reference them later).

Hope it helps, Jörg



来源:https://stackoverflow.com/questions/10696636/excel-vba-project-not-getting-closed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!