How to open Excel after closing it through VBA? - vba

While coding for an automation tool using VBA for Excel, I came across this situation. I have coded the "Close" command button as well as the "X" on the top-right of the form window. Both codes are same and serving their purpose.
I would like to know, if I close the workbook using the ActiveWorkbook.Close method in both the sub-procedures, then how to view the same Excel sheet's VBA project?
I am using Microsoft Office 2010.
Below is my code for reference:
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = vbFormControlMenu Then
MsgBox "Thanks for using! Tool will now close.", vbInformation + vbOKOnly
Unload Me
ActiveWorkbook.Close
End If
End Sub

Visual Basic for Applications lives in a host... application - here Microsoft Excel.
If you have 10 workbooks opened, the Visual Basic Editor (VBE) will have 10 VBA projects in your Project Explorer window (Ctrl+R); closing one, will remove its VBA project from the VBE - the VBA code is in that workbook you just closed!
There are two types of VBA projects: one is the macro-enabled workbook (.xlsm), the other is a Microsoft Excel add-in (.xlam). The former lives and dies with the workbook it's written in; the latter lives and dies with the application it's loaded in.
If you need code that can manage multiple workbooks, consider saving as (F12) an add-in; ActiveWorkbook.Close would close the active workbook, and then the add-in is still loaded... and you can still view its VBA source.
If you only mean to close a form, I suggest you don't implement the QueryClose handler, and let the form close by itself - and leave the workbook open!. If you mean to close another workbook, I strongly recommend you avoid ActiveWorkbook and use a Workbook object reference instead.

Related

Workbook_Open sub won't run automatically when the workbook is opened

I need this macro to run when the .xlsm workbook is opened. It only runs if I manually run the macro, it does not start when the workbook is opened. FYI, I've checked/verified macro security (this is not signed yet).
Any ideas what I'm doing wrong?
Sub Workbook_Open()
MsgBox "Hello World!"
End Sub
Your code needs to be located in the ThisWorkbook module.
Open the VBA Editor (Alt+F11)
Open the Project Explorer (Ctrl+R)
In the Project Explorer pane, double click ThisWorkbook. (If you have multiple workbooks open, make sure you choose the ThisWorkbook under the correct project.)
In the code editor pane, click the drop-down that says General.
This will bring you to a new (or existing) Workbook_Open procedure:
Private Sub Workbook_Open()
End Sub
That's where your code should be placed.
Note that clicking the other drop-down at the top-right of the Code Editor pane, will list the other Workbook-level procedures you can add.
More Information:
Chip Pearson : Code Module And Code Names

vba programatically have excel forget window structure

In Excel 2013, Excel likes to remember Excel windows (not worksheets) that were open, and when the workbook is opened again, also open up those windows:
From user interface: With a new workbook, Ribbon Tab "View" and then "New Window". Now two windows are open. Edit at will. Save the WorkBook. Then when that workbook is opened again, both windows are opened.
I'd like to prevent this this programatically within VBA, as part of WorkBook_Open I suppose, so that just one window would open. How can I do that? I've tried closing all windows using WorkBook_Close, that didn't work.
Just loop through the Windows collection and close them until you only have one open:
Private Sub Workbook_Open()
Do While Me.Windows.Count > 1
Me.Windows(1).Close
Loop
End Sub

Excel Opened Documents History Log - AddIn

Apologies in advance if this ends up being generic. I have done some research on this and drawn a complete blank.
Excel is great, I love Excel. So much so that the "Recent Documents" section is of almost no use to me as I use that many spreadsheets in an insane amount of locations.
I have been researching a way to log (using VBA as an AddIn) documents when they are opened. Even if it is into something as simple as a text file with the date, however I cannot figure out how to have the VBA code "know" when a file is opened (which, along with outputting to a Text file, is all I want it to do at this stage).
Is there a way to have the VBA look for this action from within an excel instance?
The following steps (adapted from the excellent post at http://www.cpearson.com/excel/AppEvent.aspx ) is the "minimally viable" way to do what you need.
open a new workbook
open the VB editor
Insert a class module; in the properties window, set class name to CExcelEvents
Add the following code in the class module:
Private WithEvents App As Application
Private Sub Class_Initialize()
Set App = Application
MsgBox "initialized the class!"
End Sub
Private Sub App_WorkbookOpen(ByVal Wb As Workbook)
MsgBox "New Workbook was opened: " & Wb.Name
End Sub
5. Right-click on the "ThisWorkbook" element in the project explorer, and select "View Code"
6. Add the following code:
Private XLApp As CExcelEvents
Private Sub Workbook_Open()
Set XLApp = New CExcelEvents
End Sub
This creates an instance of the CExcelEvents class, and "turns on event handling" when the addIn is loaded.
Finally, save the file as myEvents.xlam in the location where addIns are stored - this varies depending on your machine...
If you now close the file, and add the addIn (again, depends on your environment whether that is from the Developer ribbon or the Tools menu), you should see a dialog box that says "initialized the class!". This shows the addIn is properly installed and working.
Now, when you open a workbook, another message box will appear: "New Workbook was opened: " with the name.
Obviously you will want to get rid of the message boxes, and put in some "useful" code that does whatever you want to do (for example, log the name of the workbook to a file). It sounds to me like you don't need help with the latter - if I am wrong then please let me know.

Powerpoint VBA code to appear on all new documents

I have a macro in Powerpoint 2010 to open a new document based on a template based in a central location, to be installed on several machines.
Sub PowerpointTemplate()
Application.Presentations.Open ("file")
End Sub
Powerpoint does not save the macro. Even when setting "Macro in:" to "All open presentations", it seems to reset this selection to "Presentation 1". The macro works for the time that I have this current document open, but once I close Powerpoint and reopen it, the macro has been removed.
Is there anyway to set the macro to permanently apply to all presentations?
You need to create an Add-in and load it in order to make the code available to all open presentations.
Setting Macro In to All Open Presentations simply SHOWS you the available public macros in all open presentations; it has no effect on where the macro code is saved.

VBA add-in: How to run code on "enabled"

I am writing an add-in for Excel 2003, using VBA.
I have an Auto_Open subroutine, which automatically runs some code (setting up menus, etc) whenever the add-in is Opened as a file.
What subroutine name (or other logic) do I need to use in order to have code that automatically runs when the add-in is "Enabled" through Excel's Add-in manager? (And, relatedly, when it is Disabled)
Auto_Open and Auto_Close will do what you want. Checking the addin in the Addins dialog opens it, and unchecking it closes it.
Check out the Workbook_AddinInstall Event.
From Excel's VB Help, this event:
Occurs when the workbook is installed as an add-in
Ex:
Private Sub Workbook_AddinInstall()
MsgBox "This workbook was installed as an addin."
End Sub
The Workbook_AddinUninstall Event fires when the workbook is uninstalled.