I open a query from SAP BW... I make all the changes I need and I even check the box that says Refresh query every time you open the workbook. The problem is that when I reopen the workbook it's just like a regular Excel, because the SAPBEX.xla file is not loaded anymore.
Any ideas on how can I solve this?
I want the workbook updated with the latest data every time I open the workbook.
Thanks!
You can use an application event (see Chip Pearson's website).
Put this code in the This Workbook module of your PERSONAL.XLSM (see here).
Private WithEvents App As Application
Private Sub Workbook_Open()
Set App = Application
End Sub
And in a module:
Private Sub App_WorkbookOpen(ByVal Wb As Workbook)
MsgBox "New Workbook: " & Wb.Name
'or better check here if this is your workbook and activate the addin
End Sub
Related
I am using the following code to highlight spelling mistakes in the cell-on-cell change
Sub ColorMispelledCells()
For Each cl In ActiveSheet.UsedRange
If Not Application.CheckSpelling(Word:=cl.Text) Then _
cl.Interior.ColorIndex = 15
Next cl
End Sub
Now, the problem is that every time I have to run this code by pressing f5 and also this only works on a particular workbook (Workbook specific).
So, my question is, what is the process to make a universal/global macro that runs on every workbook and every sheet which is opened, and that too runs internally when the cell is changed? It should not open up with the VBA editor window again and again.
Along with this is there any way where I can make a enable and disable button highlighted on the excel toolbar for this macro?
see image to see my VBA editor project explorer hierarchy
I tried searching on the internet and also saw many sample codes but nob body explained to make a global/universal macro, I am unable to figure out how to develop a global/universal macro that runs automatically on cell changes. which is not bounded to any one workbook but works globally on any opened workbook on all sheets.
I'm interested in your case. I myself is not an expert, so just now I search the internet and playing around and try to "cheat" by making this kind of code - which I'm not so sure if that meets your requirement.
In PERSONAL.XLSB workbook - ThisWorkbook module :
Private WithEvents app As Application
Private Sub app_WorkbookOpen(ByVal Wb As Workbook)
On Error Resume Next
If Wb.Name <> "PERSONAL.XLSB" Then
Set src = Workbooks("PERSONAL.XLSB").VBProject.VBComponents("Sheet1").CodeModule
Set trg = Wb.VBProject.VBComponents("ThisWorkbook").CodeModule
trg.insertlines 1, src.Lines(1, src.countoflines)
End If
End Sub
Private Sub Workbook_Open()
Set app = Application
End Sub
In PERSONAL.XLSB workbook - Sheet1 module :
Private Sub Workbook_SheetChange(ByVal sh As Object, ByVal Target As Range)
If Not Application.CheckSpelling(Word:=Target.Text) Then _
Target.Interior.ColorIndex = 15 Else Target.Interior.ColorIndex = xlNone
End Sub
Source code from this link and this link
The sub in "ThisWorkbook" module of PERSONAL.XLSB will be triggered when opening any workbook. The sub will copy the macro in "Sheet1" module of PERSONAL.XLSB into "ThisWorkbook" module of the open workbook.
The sub in "Sheet1" module of PERSONAL.XLSB is an event handler to any sheet of the active workbook which will be triggered if there is a change on any cell of any sheet.
I noticed that if I edit the sub in the PERSONAL.XLSB to something else, then I open another workbook, the sub will not run. I need to close the Excel application first, then open it again. If I don't do any sub-editing, opening other workbook will trigger the sub to run.
The animation above is opening two xlsx workbook which for sure there can't be any code in that workbook. But because at the time it's opened the macro in PERSONAL.xlsb is triggered (copying a sub to "ThisWorkbook" module of that xlsx workbook), so then there is a sub in that xlsx workbook. If I close this xlsx workbook, it will prompt me if I want to save the workbook. If I click yes, it will complain because it can't be saved as xlsx while there is a macro in "ThisWorkbook" module of this workbook.
Still not sure though if this is the kind that you want. Also maybe what you want is something like this : in whatever computer which has Excel app, it can do the same thing without doing anything before hand. So it's not doable, because to any computer which has Excel app, the macro need to be copied to PERSONAL.XLSB on each computer before hand.
Please note, I don't test to open an xlsm workbook which already has Private Sub Workbook_SheetChange(ByVal sh As Object, ByVal Target As Range) in it's "ThisWorkbook" module. And I think most likely it will throw an error if the opened workbook has already Private Sub Workbook_SheetChange(ByVal sh As Object, ByVal Target As Range) in it's "ThisWorkbook" module
Workbook_Open event in Excel Add-ins works only for the first file but If I open next files keeping the first file is open it is not functioning, Can anyone please help me to resolve this issue?
I have used this below code in Thisworkbook module to create Add-in and selected Add-In name in options also
Code:
Sub Workbook_Open()
MsgBox "Welcome..!!"
End Sub
Any help is appreciated..!!
Thanks
It doesn't work for the first file opening.
ThisWorkbook represents the document that's hosting the VBA code - in this case, the add-in "workbook": this message box will pop when the add-in is opened, when Excel starts up.
If you want to run code whenever any workbook is opened, you need to handle events at the Application level.
Declare a WithEvents variable in your ThisWorkbook module:
Option Explicit
Private WithEvents AppEvents As Excel.Application
Assign that object reference on open:
Private Sub Workbook_Open()
Set AppEvents = Me.Application
End Sub
Now select AppEvents from the left-side dropdown at the top of the code pane, and pick the WorkbookOpen event on the right-side dropdown - that will create a handler that looks like this:
Private Sub AppEvents_WorkbookOpen(Wb As Workbook)
End Sub
If you put MsgBox Wb.FullName here, you should get a message box with the newly opened workbook's path/filename every time a workbook is opened.
I have an excel add in(xlam) which calls a userform when excel is opening.
When I now open an existing excel file, the userform show up as expected and I can click the "OK" button which closes the userform. Unfortunately afterwards, the workbook I intended to open does not show up.
it is working with vbmodeless, so it seems to be connected to "hide" or "unload" of the userform
when I open a new excel workbook it is working fine
Does anybody know why the workbook is not opening? Does excel forget the workbook he intended to open?
In your add-in you need to listen to Excel's events. So you need to create a class, based on the Excel Application.
So you would have a class called clsCustomExcelHandlers like so
Option Explicit
Public WithEvents ExcelApplication As Excel.Application
Private Sub ExcelApplication_WorkbookOpen(ByVal Wb As Workbook)
' Do something here
MsgBox "You have opened " & Wb.FullName
End Sub
and then in a standard module, have a public variable holding an instance of this class
Public clsCustomExcel As clsCustomExcelHandlers
and then in the add-in Workbook_open you set it up
Private Sub Workbook_Open()
Set clsCustomExcel = New clsCustomExcelHandlers
Set clsCustomExcel.ExcelApplication = Application
End Sub
I have researched a good amount and tried this on my own this afternoon but was unsuccessful. When users download and open .XLS files that begin with "Current Approved", I want a macro to automatically run from my "PERSONAL.XLSB" file on any "Current Approved*.XLS" file upon opening it (* is a Wildcard). That way I can just put the code in any given users "PERSONAL.XLSB" file one time, and the macro will just automatically be triggered without the user needing to remember to trigger the macro via a shortcut key or button.
From my research here and in other places, I have only seen ways to:
Run the macro when opening the workbook which contains the macro.
Run a macro when any workbook is opened.
I have tried to modify #2 from the link above, but I've NOT figured out how to automatically run macros in this manner on files with similar names.
'Declare the application event variable
Public WithEvents MonitorApp As Application
'Set the event variable be the Excel Application
Private Sub Workbook_Open()
Set MonitorApp = Application
End Sub
'This Macro will run whenever an Excel Workbooks is opened
Private Sub MonitorApp_WorkbookOpen(ByVal Wb As Workbook)
Dim Wb2 As Workbook
For Each Wb2 In Workbooks
If Wb2.Name Like "Current Approved*" Then
Wb2.Activate
MsgBox "Test"
End If
Next
End Sub
Essentially, if I download an excel file from our CRM that begins with "Current Approved" and open it, I would like to see a messagebox of "Test".
Your code doesn't look like what you're describing. The below code should display the "Test" MsgBox when opening workbooks meeting the rule of starting with "Current Approved"
'This Macro will run whenever an Excel Workbooks is opened
Private Sub MonitorApp_WorkbookOpen(ByVal Wb As Workbook)
Const cText As String = "Current Approved"
If UCase(Left(Wb.Name, Len(cText))) = UCase(cText) Then
' Wb2.Activate
MsgBox "Test"
End If
End Sub
I have a VBA macro which is called from a spreadsheet function (user defined function, UDF). When the spreadsheet is downloaded from the internet and the user has set "Trust Center" settings accordingly, the spreadsheet will open in so the called "Protected View". The function will not be called. A button "Enable Editing" is shown.
If the button is pressed, the spreadsheet is "trusted" and reopened normally, starting calculation, and hence calling the user defined function.
However, in that VBA function the value of Application.ActiveWorkbook is Nothing. This can be verified in the debugger.
Since I just need to read some properties (like path name) of the spreadsheet, I could alternatively inspect the availability of Application.ActiveProtectedViewWindow which should reference to the protected version of the workbook. In the debugger, this object can be inspected. However, running in release (without debug) the value of Application.ActiveProtectedViewWindow is also Nothing.
Both behaviors - especially the first one - appears to be a bug present in Excel 2010 and 2013 (see also a post at the MSDN forum ).
Question: Is there a way to get hold of properties of the active workbook after it has been enabled for editing?
PS: As a follow up to the nice observation of Siddharth Rout, that "ThisWorkbook" might work: In my case, the macro is not part of the Workbook being openend. The UDF is defined in an XLA. Hence, ThisWorkbook would reference the XLA. I do need to get the ActiveWorkbook (= the workbook calling the UDF) instead of ThisWorkbook (= the workbook running the UDF).
IMPORTANT REQUIREMENT:
My function is called as a user defined function, i.e., execution order is determined by Excel updating the cell.
The function is not part of the workbook being opened. It is part of an XLA.
I cannot add any code to the workbook which is opened.
Summary: The problem can be replicated and there are some possible workarounds. The most promising one - resulting from a chat - is to use ActiveWindow.Parent instead of ActiveWorkbook.
I was able to replicate the problem.
I tried
Private Sub Workbook_Open()
MsgBox "Application.ActiveWorkbook Is Nothing = " & _
CStr(Application.ActiveWorkbook Is Nothing)
End Sub
And I got True
However, then I tried this and it gave me False
Private Sub Workbook_Open()
MsgBox "Application.ActiveWorkbook Is Nothing = " & _
CStr(Application.ThisWorkbook Is Nothing)
End Sub
Now answering your question...
Question: Is there a way to get hold of properties of the workbook after it has been enabled for editing?
Yes. Use ThisWorkbook instead of ActiveWorkbook
Followup From Comments
Once the workbook completely loads after you exit the Protected Mode, you would be able to access the ActiveWorkbook object. To test this, put this code in the protected file.
Private Sub Workbook_Activate()
MsgBox "Application.ActiveWorkbook Is Nothing = " & _
CStr(Application.ActiveWorkbook Is Nothing)
End Sub
You will notice that you get a False
So once your workbook loads, your add-in can use ActiveWorkbook to interact with the opened file.
Here is another test
Private Sub Workbook_Activate()
MsgBox ActiveWorkbook.Path
End Sub
This is what I got the moment, I exit the Protected Mode
FOLLOWUP FROM CHAT
Using ActiveWindow.Parent.Path instead of ActiveWorkbook.Path would solve the problem.
I had this same issue today, and neither the accepted answer nor any other answer that I could find on this page or through searching the Google-verse worked for me. I'm using the version of Excel within Office 365, and I figured that was at the root of the problem.
I eventually came to a solution after finding a Microsoft Excel 2010 resource and hitting the old try-fail cycle for a few hours. Here's what I got:
Option Explicit
Public WithEvents oApp As Application
Private bDeferredOpen As Boolean
Private Sub Workbook_Open()
Set oApp = Application
End Sub
Private Sub oApp_WorkbookActivate(ByVal Wb As Workbook)
If bDeferredOpen Then
bDeferredOpen = False
Call WorkbookOpenHandler(Wb)
End If
End Sub
Private Sub oApp_WorkbookOpen(ByVal Wb As Workbook)
Dim oProtectedViewWindow As ProtectedViewWindow
On Error Resume Next
'The below line will throw an error (Subscript out of range) if the workbook is not opened in protected view.
Set oProtectedViewWindow = oApp.ProtectedViewWindows.Item(Wb.Name)
On Error GoTo 0
'Reset error handling
If oProtectedViewWindow Is Nothing Then
bDeferredOpen = False
Call WorkbookOpenHandler(Wb)
Else
'Delay open actions till the workbook gets activated.
bDeferredOpen = True
End If
End Sub
Private Sub WorkbookOpenHandler(ByVal Wb As Workbook)
'The actual workbook open event handler code goes here...
End Sub
The difference between the 2010 solution and mine is that I had to call Workbook_Open and explicitly set the oApp variable there, because without that assignment neither the oApp_WorkbookActivate nor oApp_WorkbookOpen functions would fire when I opened the file.
Figured that someone else might be able to benefit from this, so I posted it, despite the fact that the most recent update to this thread is better than 2 years old.
Best.
Try using Application.Caller.Parent.Parent instead of Application.Activeworkbook
This is not a complete answer to the original question, but a (dirty) workaround for a problem related to this.
I needed ActiveWorkbook to infer the workbooks path, that is ActiveWorkbook.Path.
An alternative to using ActiveWorkbook.Path is to check for Application.RecentFiles(1).Path which is the path of the most recently opened file. In many cases this will be the workbook for which the user just has "Enabled Editing". However, of course, this method may fail: In the case the used opened another sheet, then enabling the previously opened sheet.
(Note: ActiveWorkbook.Path give the path of the folder, while Application.RecentFiles(1).Path gives the complete path of the file, so there has to be some post-processing).
I know it's old thread, but i came across the same issue and i found solution ;)
The only way to go around it, is to use variable type Workbook
Dim wbk as Workbook
Set wbk = Application.ProtectedViewWindows(index).Workbook
Warning:
ActiveSheet returns Nothing when active window is protected too.
Dim wsh As Worksheet
Set wsh = wbk.Worksheets(index)
Try this code it works.
If (UCase(ActiveWorkbook.Name) = ucase("<YOUR XLA NAME WITH EXTENSION>")) Then
End
End If
Set wbObj = ActiveWorkbook
First time when you run the macro, it just ends without doing anything. Second time it picks up the proper file.