Messagebox on file open does not get focus - excel

I have an excel file that opens a messagebox when the file is opened . However, when I open the file, because of the messagebox, the file and its messagebox do not come to the front of my windows gain focus (they appear on my taskbar and I have to click on it from there). After the messagebox is completed, the file gains focus as normal.
In its simplest form, this code seems to trigger this
Sub Auto_Open()
MsgBox ""
End Sub
I have tried adding code in to give the window focus before executing the messagebox, but to no success. I don't mind this issue but other people will be using this document. Is there a solution to this?

The legacy Auto_Open auto-macro is going to run regardless of the state of Application.EnableEvents, which may be a problem if another macro-enabled workbook needs to open that workbook.
I can't reproduce the issue on Excel 2010, with this code in Module1:
Sub Auto_Open()
MsgBox "Auto_Open()"
End Sub
And this code in ThisWorkbook:
Private Sub Workbook_Open()
MsgBox "Workbook_Open()"
End Sub
I get two successive message boxes, first from Workbook_Open, then from Auto_Open, both show up right where I'd expect them - right in the middle of an empty worksheet:
Try putting the message box in the Open event handler of ThisWorkbook, see if it helps.

Related

Excel Error when using button made from Custom UI Editor- Cannot run the macro "***". The macros may not be available in this workbook or all macros

I built a series of macros, all of which have worked with the custom ribbon I built using the Custom UI Editor.
I added one more macro and updated my Ribbon with a new button. I test the macro first and it works when run from the developer tab or just running directly from VBA and the sub heading is as follows: "Sub GetDES()".
Once I make it so it works with the button on the ribbon by changing to:
Sub GetDES(control as IRibboncontrol)
I get the "Cannot run macro..." error
Sub GetDES(control as IRibboncontrol)
'PURPOSE: Get description page and insert into each confirm page
Dim Fpath As String
Fpath = ActiveSheet.Range("I6").Value
With Application.FileDialog(msoFileDialogFilePicker)
.ButtonName = "Add this DES!"
.AllowMultiSelect = False
.InitialFileName = Fpath
.Filters.Add "All Pictures", "*.*"
.Show
If .Show = -1 Then
Dim img As Object
Set img = ActiveSheet.Pictures.Insert(.SelectedItems(1))
img.Left = 75
img.Top = shOutput.Range("B40").Top
img.Width = 450
Else
MsgBox ("Cancelled.")
End If
End With
End Sub
The only thing I can think of is I still have something weird happening when I run it without the Control. When I run it the dialouge picker opens twice. I'm wondering if that is my problem with running it from the button on the ribbon.
When I step through the code, the line
set img=Activesheet.Picture.Insert(.SelectedItems(1))
is what causes the file dialogue picker to open again.
Ideas? Thank you
To make sure the callback is invoked by the Office you may try to assign a sample sub to a button, for example, with a simple MsgBox statement to make sure it works correctly.
Sub GetDES(control as IRibbonControl)
MsgBox "test"
End Sub
If that works correctly you can try putting your original code. But you may try to avoid shorthand properties like ActiveSheet or With operator. Hope it helps.
I believe I stumbled across my issue. I built a new module that I wrote this code in, I named the module with the same name as the sub. When Testing, I never went to run it from the macro list, I just ran it from the VBA window. When I looked at the Macro's List window, I saw that the new macro I wrote in this workbook was not displaying like the other macros. As you see in the photo, "Confirm Tool-Master_V5.xlsm'!GetDES.GetDES"
I removed that module and took the copy, listed as GetDES3 and renamed it
GetDES(control as IRibboncontrol)
and it runs from my ribbon. I'm not sure why it looked different or if there was something to do with the module name and the sub being named the same that caused it to be like that. so I tested it. I wrote a test sub and named the module test and it did the same thing.
So, Lesson is, don't name your sub and the module with the same name

Why macros are disabled each time I re-start my Excel workbook, even after enabling content...?

Here the context : I'm working on a local workbook (c:) that directly opens a UserForm within the Workbook_Open() Sub. Then, when UserForm is closed, Excel is also automatically closed. Nothing special there.
The problem : Even with a correct setup in my Trust Center Settings (Disable all macros WITH notification), the warning security message pops up again each time I reload that workbook.
Here my code :
Private Sub Workbook_Open()
' modal display of my form
myUserForm.Show vbModal
' upon return from my form, save and close that workbook
Me.Close savechanges:=True
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
' also quit Excel app if no more workbook is opened
If Application.Workbooks.Count = 1 Then Application.Quit
End Sub
I'm pretty sure that's related to the fact that Excel is automatically closed by program, and not by user himself.
For example if I reduce the code to the strict minimum, like :
Private Sub Workbook_Open()
myUserForm.Show vbModal ' modal display of my form
End Sub
In that situation, so without any Close or Quit, I just fall into my main worksheet when the UserForm is closed, then I just manually close Excel, with or without saving, and that's it, warning popup will not appear next time I'll open that workbook.
I don't know if there is a solution without playing with Trust Locations or All macros enabled.
Any ideas ?
Thanks for your help

Custom Tab Order macro and functional disabilities when second Excel Workbook opened

I have a series of document templates created in Excel macro-enabled workbooks with an array of different macros coded in vba. I'm fairly new to vba, but I was able to cobble together code from several different sources that mostly achieved what I was looking to do: create a customized tab order, which also incorporates ActiveX Comboboxes.
For the most part, all code in each modules works great on the respective files saved locally on my machine. However, if these files are Emailed or saved on an intranet site or somewhere where they must be downloaded by end-users, something squirrelly happens that I haven't been able to sort out. When the first file is opened, the user will receive Excel's "Protected View" prompt, requiring that they click the "Enable Editing" box to access/modify the file. This is fine and the user can manipulate the first xlsm file without issue. However, with that first xlsm file open, if they additionally open any of the other xlsm workbooks that I've created (with ostensibly identical code), they are not prompted with the "Protected View" message and the following happens in that second workbook:
The cell focus is redirected to cell A1 instead of whichever cell should have been selected when the second workbook was opened,
The customized tab order I've delineated is disabled,
Both the horizontal and vertical scrollbars vanish from view, and
All tabs for each sheet in the second workbook are no longer visible.
I've tried manipulating and rewriting the code in each of these workbooks every which way I could think of, but I can't figure out what could be the culprit causing the above issues. I'd wager it has something to do with how Excel is initializing the second workbook when it's been downloaded, and that it has more to do with the code in the ThisWorkbook module -- here are the snippets that I think are relevant:
Private Sub DisableTabbing()
If Not (Application.ActiveProtectedViewWindow Is Nothing)
Then Application.ActiveProtectedViewWindow.Edit
End If
Application.OnKey "{TAB}"
Application.OnKey "+{TAB}"
Application.OnKey "~"
Application.OnKey "+~"
Application.OnKey "{ENTER}"
Application.OnKey "+{ENTER}"
End Sub
Private Sub Workbook_Activate()
EnableTabbing
End Sub
Private Sub Workbook_Deactivate()
DisableTabbing
End Sub
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
EnableTabbing
End Sub
Private Sub Workbook_SheetDeactivate(ByVal Sh As Object)
DisableTabbing
End Sub
On my machine, I don't encounter this at all when any two workbooks are locally saved and opened, but only when the files are Emailed/downloaded. I'm sure there's a relatively simple fix for this, but I'm not as experienced with VBA, so I'm hoping someone here might be able to point me in the right direction. Is there something in the code that could be causing that, or better yet, some additional code that I could insert that would prevent it from happening?

Workbook_Open() in Excel 2016 not firing

Excel 2016 (or 365) does not seem to fire the Workbook_Open() sub reliably or more precisely, not at all!
The simple event sub
Private Sub Workbook_Open()
MsgBox "Work book is open"
End Sub
does not seem to work. However, if a workbook is already open and then the workbook containing the above Sub is then opened, it does run as expected.
I notice that unlike Excel 2010, 2016 (365) opens each workbook in its own window, not a workbook window in the Excel application window. Is this a bug in 2016 and is there a workaround?
I have produced a work around for my own applications and that is call the activation of a worksheet and call my initialization routines from there. But a bit "rough" and it would be good to have the Workbook_Open() sub working correctly.
It is a simple single Sub in the ThisWorkbook module. Macros are enabled. In Excel 2010 it works perfectly, as do two other macros in other workbooks for which I have written macros. It is just this one thing with Excel 2016. Given that the Workbook_Open() sub is the gateway to a workbook it seems a little strange that we have to go to a workaround to make it function.
Try encapsulating the call with a new instance of Excel. Example below:
Sub OpenInNewExcel()
Dim Background_Excel As Excel.Application
Dim pathName As String
Dim fileName As String
Let pathName = "Enter your path here" 'include "\" at the end
Let fileName = "Enter your file name here"
Background_Excel.Workbooks.Open fileName:=pathName & fileName
Background_Excel.Parent.Quit ' This is how you close the file completely using VBA otherwise the file will close and the Excel Shell will remain.
End Sub
Also make sure that enable macros is turned on in the Options-Trust Center.
You have to add the file/folder location of your workbook as a "Trusted Location".
You can see more info about that in Designate trusted locations for files in Office 2016.
I have same problem then I found solution after google it:
https://www.myonlinetraininghub.com/excel-forum/vba-macros/excel-2016-workbook_open-event-doesnt-trigger
Then I also use "Private Sub Workbook_Open()" and "Public Sub Auto_Open()" open in excel 2016 that work fine:
Private Sub Workbook_Open()
CustomStartUp
End Sub
Public Sub Auto_Open()
CustomStartUp
End Sub
Private Sub CustomStartUp()
MsgBox "Work book is open"
End Sub
I've had this problem (I'm using Microsoft 365), and I found this thread.
It happens for me sometimes when I have another workbook already open, then, on trying to open my macro-enabled workbook, before any sheet is displayed I get the Microsoft warning message about macros. Then, although I click its 'Enable' button, the Workbook opens, macros do get enabled, but Workbook_Open doesn't run.
I've never known the problem to occur if no other workbook is open. (Of course, the user might still get the yellow-backed messages at the top of the workbook, asking them to click the Enable Editing and/or Enable Macros buttons.)
Note that my Workbook_Open just calls another 'workbook-opening' sub in a module to do all the opening processing.
My solution: When my workbook-opening sub is called, it sets a global variable to True to indicate it has run.
I've made it obvious to the user that the problem has occurred, by means of a 'Welcome' sheet with all its cells locked, so the user can do nothing; at this point all other sheets are very hidden. The workbook-opening sub, when it runs, deactivates this sheet and makes it very hidden, so the user never normally sees it, and makes the other sheets visible. But if this screen remains, it instructs the user to select the other workbook, then select this one again. My Workbook_Activate code then runs, and because the global variable isn't True, it calls the workbook-opening sub. If this global variable is True, it does nothing.
To make this work, the Workbook_Close sub makes the other sheets very hidden and the Welcome sheet visible, ready for the next time the Workbook is opened.
Hey presto, the problem is solved.
The Welcome sheet actually has a dual purpose, in that if either of the yellow-backed warning messages are displayed, it will remain and force the user, with suitable instructions, to click Enable Editing and/or Enable macros. If the users aren't au fait with macro-enabled Excel, they will just ignore these and try to carry on regardless.
All this is much easier to implement than to explain. I hope it's clear.
And I hope this might be of help to someone.
I had this issue with one of my files as well. I managed to fix this issue by running Workbook_Open manually in the VBA editor once open and saving the file in another location. The file in the new location should have no issue with auto-running Workbook_Open. If this doesn't work, copy the original file to a new location before manually running & saving.
If the newly saved file does not run Workbook_Open, repair your version of Office.

Workbook_Open() won't execute Excel 2011

Using Excel 2011 (should be same as Excel 2010)
Code is under the "ThisWorkbook" module in Excel
Events are enabled
Macros are enabled
I can't seem to get either Workbook_Open() or Workbook_BeforeClose() to execute. I've read numerous posts on the subject but no solution. Here is some simple test code that should execute but doesn't. Any help would be greatly appreciated.
Private Sub Workbook_Open()
ActiveSheet.Range("BL4").Value = "Open is working"
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
On Error Resume Next 'in case the menu item has already been deleted
ActiveSheet.Range("BL5").Value = "Close is working"
End Sub
First make sure you have put this in this in the right place and have macros enabled.
Then, try adding this line to the workbook_open method:
MsgBox "HELLO"
Do you see the msg box? You're choice of cell looks a bit strange
Also, I think you need to use a .xlsm file not .xlsx (Although not sure on that one)
FInally, if a plugin calls something like this line, it could cause your events not to fire..
Application.EnableEvents = False
So make sure you have tested it with no other sheets or addins open.

Resources