The following code, which is part of an Excel Add-In that I created, will create a file name of the current data extract that we have open.
Sub ExtractSave()
'
If InStr(LCase$(ActiveWorkbook.name), "extract") > 0 Then
Exit Sub
Else
Dim MyDir As String, fn As String
MyDir = CreateObject("WScript.Shell").SpecialFolders("MyDocuments") & "\Extract Files" ' change this to valid path
If Len(Dir(MyDir, vbDirectory)) = 0 Then MkDir MyDir
fn = MyDir & "\Extract - " & Format(Now, "mm-dd-yyyy hh_mm")
ActiveWorkbook.SaveAs Filename:=fn, FileFormat:=xlOpenXMLWorkbook
End If
End Sub
When I close the workbook, it does require a save to occur as changes will be made to the workbook. All of this works just fine. The issue is that the company has a .COM Add-In that is required to run on all documents. It labels the document footer with a specific security level identifier. While I do not need to bypass this, I would like to know if it is possible to write code that will replicate hitting enter when this box appears? There are two buttons, Yes and Cancel. The default button is Yes on the popup. I did try adding
Application.DisplayAlerts = False
If Me.Saved = False Then Me.Save
Application.DisplayAlerts = True
into the Workbook_BeforeClose private Sub, but the op still appeared. Also, if I am correct, this bit of code will not work for the Saved workbook because this is running from within the Add-In.
I have not been able to find any equivalent information for this particular issue, so any help would be appreciated. Also, I do not know what other information would be useful in your assistance to finding a solution, so please ask and do not down vote because I missed something.
Order of events:
Import data into new workbook
Run ExtractSave routine
See the .COM addin popup (need to have YES button pressed, YES is
the default button)
User performs duties on the workbook
Click on close
prompted to Save the changes to the workbook (again, default is YES,
need this "clicked")
See the .COM addin popup again(need to have YES button pressed, YES
is the default button)
What VBA code is available to automatically click on the YES buttons for the .COM add-in while saving the workbook?
From comments:
Adding Application.SendKeys ("~") before this line ActiveWorkbook.SaveAs Filename:=fn, FileFormat:=xlOpenXMLWorkbook worked when the first save event (#2 from the list in the OP) occurred.
And For #7 .COM addin popup, adding Application.SendKeys ("~") in Workbook_BeforeSave event didn't help (I hope someone can help us to fix it):
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
SendKeys "~"
End Sub
Related
I could not find a simple answer to this question, which I thought was odd. I was making changes to an Add-in I wrote and I was pressing the save button ever so often in the excel VBA editor. When I came back to make some more changes I found that all my changes over the last few days are gone. Is there any hope of recovering my work? More importantly, if I am making changes what is a better way of saving the project. I usually work out of my add on and only save a new copy when I need to send it out to a teammate.
I remember having this bad surprise at some point, but then I started to look closely at the save button before clicking and I noticed that VBA is telling you which workbook will be saved if you click on the floppy disk.
The reason why it is important to have a look at this is because the file that will be saved depends on what is currently selected in the Project Explorer and not what is currently visible in the VBA Code Editor Window.
You can also make sure that your add-in is properly saved by selecting one of its elements and running the following command in the Immediate Window (which should replicate exactly what the VBE save button does):
Thisworkbook.Save
Finally, unlike for other types of workbooks, Excel won't tell you when you close the application without saving changes to an add-in. For that reason, you can take extra precautions by adding the following code to the object Thisworkbook of your add-in:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
If ThisWorkbook.Saved = False Then
Dim strMsg As String
' Specify the message to display.
strMsg = "Want to save your changes to '" & ThisWorkbook.Name & "'?"
Dim Decision As Boolean, ireply As Variant
ireply = MsgBox(prompt:=strMsg, Buttons:=vbYesNoCancel)
If ireply = vbYes Then
Decision = True
ElseIf ireply = vbNo Then
Decision = False
Else 'They cancelled (VbCancel)
Cancel = True
Exit Sub
End If
If Decision = True Then
ThisWorkbook.Save
End If
End If
End Sub
Which will prompt you with a message allowing you to save your add-in if it has an unsaved status when the Excel application is closing.
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.
I have a sheet with a Workbook_Open code loading a userform.
After closing the form and the workbook Excel prompts me for the VBA Project password.
I've found some info on this issue on this pages:
http://support.microsoft.com/kb/280454
http://social.msdn.microsoft.com/Forums/office/en-US/8cb79e54-26ae-487c-8945-69b84b2e4eeb/com-addins-and-vba-password-prompt-bug
But it seems to be a problems with COM add-ins, which I have some.
The problem is that the add-ins are not mine and I can't change or disable them.
Is there any other solution?
For me, the problem was not some add-ins or unreleased references. It was the Dropbox Badge. As soon as I removed it, the password was not solicited anymore when I closed Excel. To disable Dropbox Badge, open the Dropbox app, go to Settings, then Preferences and in the General Tab select "Never show" under Dropbox Badge.
I found this solution on the following forum:
https://www.excelforum.com/excel-programming-vba-macros/1100960-ever-annoying-vba-password-prompt-at-close-of-excel-2.html
This happens when you have hanging pointers to objects.
For every 'Set xyz=' you do in VBA, make sure that you have a corresponding 'Set xyz=Nothing'.
Similarly for anything pointing to or obtained from COM objects.
Make sure you close any ADO connections etc.
Be especially careful to handle all errors so that ALL object variables are set to Nothing before the workbook can close, something along these lines:
Option Explicit
Option Compare Text
Public Sub Refresh()
On Error GoTo FAIL
Set wb = ThisWorkbook
wb.Activate
' do something useful
DONE:
On Error GoTo 0
Set wb = Nothing
Exit Sub
FAIL:
MsgBox Err.Description
GoTo DONE
End Sub
I have a number of workbooks that were experiencing the same problem with some users. We went through the process of checking for COM add-ins and various combinations of Windows and Office.
In the end we included the following code as part of the workbook_beforeclose event and the problem was resolved for our users.
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim intResponse as Integer
'If the workbook needs to be saved then ask the user if they want to save the workbook, if not then exit without saving
'Need a global boolean to ensure the request to save the workbook is not shown twice
If Not ThisWorkbook.Saved And Not blnStartedClose Then
blnStartedClose = True
intResponse = MsgBox("Do you want to Save the this Workbook" & vbNewLine & vbNewLine & _
"Select 'Yes' to save the workbook" & vbNewLine & _
"Select 'No' to close without saving", vbYesNo, "Confirm - Workbook Save?")
If intResponse = vbYes Then ThisWorkbook.Save
End If
'If the user has clicked on 'No' to save the workbook then reset the "Saved" property to TRUE so that when we exit this routine no attempt to save the workbook is made
ThisWorkbook.Saved = True
End Sub
I've had some clients have this issue and I finally started getting it after installing BlueBeam Extreme. I unchecked the BluebeamOfficeAddIn in the COM Add-Ins and the password box stopped popping up when closing my .xlsm file. I'm going to do more digging to see if it's my code but I haven't had this issue until now and disabling that addin seemed to help...
I have a workbook that I would like to open and not have it ask to update links, (exact message is :
"This workbook contains links to other data sources. If you update the links, Excel will attempt to retrieve the latest data. If you odon't update the links, Excel will use the previous information. Note that data links can be used to access and share confidential information without your permission and possibly perform other harmful acts. Do not update the links if you do not trust the source of this workbook." )
What I would like to do is open the workbook by clicking on the file in Internet Explorer and have the links update but not ask for the user to click the button to update.
I have tried the following code in the Open Event for the work book with not success:
Private Sub Workbook_Open()
Application.DisplayAlerts = False
Application.ScreenUpdating = False
End Sub
I have also tried the following lines of code in the above Sub:
ActiveWorkbook.UpdateLink Name:=ActiveWorkbook.FullName, Type:=xlExcelLinks
Application.ActiveWorkbook.UpdateLink Name:=ActiveWorkbook.FullName, Type:=xlExcelLinks
Application.ActiveWorkbook.UpdateLink
Workbooks.Open ActiveWorkbook, UpdateLinks:=True
ActiveWorkbook.UpdateLink Name:=ActiveWorkbook.LinkSources, Type:=xlExcelLinks
The version of MS Excel 2010 and saving to an .xls file for sake of those with legacy versions.
Your help would be very appreciated. Thank you in advance for all your help.
Respectfully,
Robert
Just in case this might help anyone in the future the following is what I did:
Private Sub Workbook_Activate()
Application.AskToUpdateLinks = False
End Sub
This prevented the Update Links message box from appearing when the file is opened.
Robert
Just to add to Robert's (#user2320821) answer -
I had to modify the code to:
Sub Workbook_Open()
Application.DisplayAlerts = False
Application.AskToUpdateLinks = False
Application.DisplayAlerts = True
End Sub
The key differences being that
1) It's a Workbook_Open sub instead of a Workbook_Activate sub. The Activate sub was not suppressing the Update Link request.
2) I had to throw in a DisplayAlerts flag toggle to suppress a second warning about the links not being updated, even after the first Update Link request was suppressed.
In case it wasn't obvious in Robert's answer, this sub worked when I put it in the ThisWorkbook object.
I'm using this code in my vba .xlm file..
Private Sub Workbook_Open()
ThisWorkbook.UpdateLinks = xlUpdateLinksNever
End Sub
M Office2013
I have a macro that creates a back-up copy of an Excel document.
How do I make it so that the macro runs automatically every time the document is saved?
Here is the code:
Sub BUandSave()
'Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
'Saves the current file to a backup folder and the default folder
'Note that any backup is overwritten
Application.DisplayAlerts = False
ActiveWorkbook.SaveCopyAs Filename:="G:\1 Processing\Christine\" & _
ActiveWorkbook.Name
ActiveWorkbook.save
Application.DisplayAlerts = True
End Sub
I got this code from online and have zero skills with VBA, please help!
EDIT: Alright, thank you for your answers and insight. now I have one other question. How do I make it so that the back-up copy saves under a different name. I want to make it something like DO NOT DELETE or DO NOT EDIT, something like that so that the person editing the file doesn't accendentally try to edit the wrong one. How do I add that into the code?
The answer is in the code you posted:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
'Saves the current file to a backup folder and the default folder
'Note that any backup is overwritten
Application.DisplayAlerts = False
ActiveWorkbook.SaveCopyAs Filename:="G:\1 Processing\Christine\" & ActiveWorkbook.Name
ActiveWorkbook.Save
Application.DisplayAlerts = True
End Sub
Place the above code in your ThisWorkbook module (Alt + F11 will get you into the VBA Editor).
Workbook_BeforeSave is a workbook Event. Code within it is executed whenever the event is triggered, in this case, before the workbook is saved. Events in Excel are pretty darn cool and a powerfull tool for code slingers :-)
The code you posted in your question is now no longer necessary.