Context: Excel 2013 VBA. When using an Add-in, the term "ActiveWorkBook" is supposed to refer to the document being edited, while "ThisWorkBook" refers to the add-in in the background. Consider the code
In the ThisWorkBook module of the Add-in
Private WithEvents App As Application
Private Sub Workbook_Open()
Set App = Application
End Sub
Private Sub App_WorkBookOpen(ByVal Wb As Workbook)
MsgBox Wb.Name & " " & Wb.Worksheets(1).Cells(1, 1)
If Wb.Worksheets(1).Cells(1, 1) = "AAA" Then
MsgBox "Cell OK",
MsgBox ActiveWork.Name
End If
End Sub
The Add-in is enabled, and Excel started. So far so good. Now if I open a file "Book1" which contains "AAA" in cell(1,1) of Sheet1 I receive:
"Book1.xlsm AAA" (in the messagebox, as expected), then "Cell OK", as expected.
But then the error "Object Required" referring to the line MsgBox "ActiveWorkBook.Name" So at that point Book1 is not yet the ActiveWorkBook. When does it become so? Or how do I make it so? (Something like "Wb.Activate" before the MsgBox doesn't help)
This problem is showing up in a much more complex real-world situation, which moreover seems tied up with security issues somewhow. I'm trying to understand the behaviour with a simple example
App.ActiveWorkbook.Name? - SierraOscarMsgBox Wb.Name? - Mr. Mascaro