Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tell when in the VBA window

Tags:

c#

.net

excel

I'm working in C#.net and basically I want my plug-in to be able to tell if when the user saves, the save is coming from Excel itself or if the save is coming from the "Visual Basic for Application" window.

Is there a way to tell if the user is currently in the "Microsoft Visual Basic for Application" or not?

Edit: I don't know if this is a full prof solution but I've found that .Application.ActiveWindow.ActiveSheet == null seems to be working.

like image 468
user1255276 Avatar asked Mar 20 '13 15:03

user1255276


2 Answers

on error resume next set wbk = workbooks("MyBook.xls") on error goto 0 is wbk is nothing then _ set wbk = workbooks.open("Mybook.xls")

like image 109
Anirban Piku Ghosh Avatar answered Oct 02 '22 21:10

Anirban Piku Ghosh


You could do something like this to return a boolean True/False if the project's VBE window is open. I'm not sure what the C# equivalent would be, but you may be able to simply check whether the Workbook's VBProject VB Editor is active:

Function IsVBEActive(wb as Workbook) As Boolean

Dim vbProj

Set vbProj = wb.vbProject

IsVBEActive <> vbProj.VBE.ActiveWindow Is Nothing

End Sub

NOTE This requires enabling trusted access to the VB Project Object Model:

enter image description here

like image 23
David Zemens Avatar answered Oct 02 '22 22:10

David Zemens