Find if workbook was opened using VBA or by user - excel

I have a macro workbook Excel 2007 with a workbook open event.
Inside the workbook open event is it possible for me find if the workbook was opened by the user from windows or from another workbook using vba...

I am not sure if it fits your needs, but you can open a workbook in your VBA code without firing the events:
'Disable Events.
Application.EnableEvents = False
'Open your book.
Workbooks.open(Filename)
'Enable Events.
Application.EnableEvents = True

If this suits your purpose:
When opening with vba, change the name of the workbook before opening. Then change it back when done. Workbook_Open procedure just needs to check for the name of the workbook it is in.
To change the name of the workbook
you can open it, then use SaveAs method to close it again with the new name
use the Shell function
use a Microsoft Scripting Runtime object.

Related

Is it possible to run a macro externally on an excel file?

Currently, I export data daily from software to excel files.
There's a lot of repetitive tasks so I created a macro.
I open the new exported excel file and then save it as "Macro-Enabled worksheet"
I open the Macro-enabled worksheet
I import the macros into the excel file
I run the macros
Is there a way to run the macro without doing all the steps above using VBS or any other way?
I don't know if there's a solution out there, but I would prefer if an external VBA operator would ask for the location of the exported file and then does the rest
You can easily open any other workbook and run any commands on that workbook. So you can have the following macro in an Excel file MyMacroFile.xlsm and manipulate data in C:\Temp\WorkbookToRunMacroOn.xlsx for example.
Option Explicit
Public Sub DoTasksOnOtherWorkbook()
'open another workbook
Dim OpenWorkbook As Workbook
Set OpenWorkbook = Application.Workbooks.Open(Filename:="C:\Temp\WorkbookToRunMacroOn.xlsx")
OpenWorkbook.Worksheets("Sheet1").Range("A1").Value = "Changed A1 in another workbook"
'don't forget to close the workbook and save or not
OpenWorkbook.Close SaveChanges:=True
End Sub
If you want to ask the user to select a file to open you can use the Application.FileDialog property it returns a file name that you can then use in the Application.Workbooks.Open to open it.

How to Activate a specific Workbook on non-VBA Excel Quit

I have a workbook ("CodeBook.xlsm") that runs code using a BeforeSave event. When a user has multiple workbooks open and chooses to quit excel via File/Exit, the user is prompted whether to save workbooks, and if yes to CodeBook.xlsm, then the BeforeSave code is run. The problem is, at that point the ActiveWorkbook may not be CodeBook.xlsm, unless that happens to be the workbook that the user was in when he/she selected Exit Excel. If the user quit excel from another workbook, the BeforeSave code is running but the activeworkbook is some random file of the user, so all the references to specific worksheets and ranges in the BeforeSave code do not work.
I have tried various ways using a Static declaration to retain the name of CodeBook and workbook().activate to activate it when the application is quitting, but when BeforeSave runs, it can't pick up the name CodeBook anywhere, short of hard-coding the name into the code.
Any suggestions? How to retain a variable name in memory when there is no code running, but is there when a user initiates a quit excel, OR how to activate a specific workbook when Excel is quitting from a user command and not from application.quit. Using excel 2010.
I overcame this by including a reference to the specific workbook.
For example, this code simply saves the date/time stamp in cell A1 of Sheet1 before closing the document. By adding ThisWorkbook, it works on the specific workbook that the code resides in. If you don't add ThisWorkbook then it will work on the active workbook when the user quits.
Private Sub Workbook_BeforeClose(Cancel As Boolean)
ThisWorkbook.Worksheets("Sheet1").Range("A1").Value = Now()
End Sub
Place this code in the ThisWorkbook module.

No existing macros availble when Excel is opened from Outlook macro

I have a macro in Outlook, and I have it opening an Excel file that is saved on my desktop. Once the file is open, I would like to run a macro that I have written in excel, but none of my excel macros are available. The macros are available whenever I open excel any other way, and macros are enabled when I open excel through outlook vba. My question is, how do I make these macros available when I open Excel via the Outlook macro? Please reference my code below.
'Pre:None
'Post:Excel will have been opened, and the macro "CreatePowerPoint()"
' will have been run on the excel document
Sub Gimba()
Dim xlApp As Object, xlWkb As Object
'open excel
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True ' can be False if you do not wont see reaction,
' byt make sure is not fail
'Do not show any alerts
xlApp.DisplayAlerts = False
'open excel document
Set xlWkb = xlApp.Workbooks.Open(file path goes here)
'call macro on excel document
Call xlApp.Run("CreatePowerPoint")
End Sub
I assume that the macros that you have normally available are stored in your personal.xls workbook? If so, then you just need to load that up before you try and launch your CreatePowerPoint macro.
Try something like (depends on where your personal workbook is stored):
xlApp.Workbooks.Open ("C:\Documents and Settings\YourUserNameHere\Application Data\Microsoft\Excel\XLSTART\personal.xlsb")
As an aside, you might find it easier to write the VBA code if you use early binding. To do this, you need to add a reference to the Excel object model, then instead of using CreateObject, you could use Set xlApp = new Excel.Application. That way you get all the nice Intellitype assistance.

run workbook open event from workbook saved with new name

I have code which uses the saveas dialog to create and open a new workbook based on certain criteria which works perfectly however the newly created workbook does not execute the workbook.open event which opens a userform.
Opening a workbook in code will not cause the auto-open event to run. You need to add the line:
wb.RunAutoMacros xlAutoOpen
... to your code, assuming wb is the workbook you just opened.

Is it possible to deploy a VB application made in Excel as a stand alone app?

Basically wondering if there's a way for me to create a VB application in Excel and have it run without a full version of MS Office. The VB application would load an Excel sheet that would import a CSV onload, then print a PDF of the sheet and close.
If you have any questions, let me know.
No. Not without converting to a standalone application.
If you had were familiar with VB6 (and had access to it; it's no longer for sale), you could create a VB6 app. that references the excel COM components (still need to be installed on each target PC).
Otherwise, build an app. using VB.NET and use Office VSTO 2010 (need to reference the Office PIAs)
How to: Target Office Applications Through Primary Interop Assemblies
Just a little conflict. In office, you code with VBA, which is different than VB. What you would need to do is create a VB app that uses excel libraries or something to do some meaningful work.
The short answer is no.
You could write an external visual basic script that calls in to office and opens excel using some excel libraries, if memory serves me correctly however - you'd still require office installed on this machine. (Unfortunately I can't find a link at the moment to back this up)
Your best bet is to parse the CSV data yourself and generate a PDF from that.
There is some information here: http://www.rlmueller.net/ReadCSV.htm on how to Read CSV data using VBS (to get the examples to run, you should simply have to rename the .txt to .vbs and double click it.)
I'll leave you to find out how you'd then generate the PDF.
I don't think however this is the best solution to your problem - a full .NET application or perhaps some Python would likely serve you better.
Code will go in several different places, "ThisWorkbook" object and the "UserForm" code.
"ThisWorkbook" contains code that will determine if the UserForm is the only Excel thing (workbook) open, and if it is it will hide the Excel application and hide the workbook itself. and if other workbooks are open it simply hides the workbook. I have it set to hide the application and the workbook in both cases so that a new instance of Excel can be opened after the UserForm is running without pulling up the workbook associated with the UserForm. The code for this is below (goes into the "ThisWorkbook" object):
Private Sub WorkBook_Open()
Dim wb As Workbook
Set wb = Workbooks("MyBook.xlsm")
If Workbooks.Count > 1 Then
wb.Windows(1).Visible = False
Else
wb.Windows(1).Visible = False
Application.Visible = False
End If
UserForm1.Show vbModeless
'Put defaults and populations here
End Sub
The UserForm1.Show vbModelessallows for Excel to be used while the UserForm is active.
A couple of notes on this section:
"UserForm1" is the name of my UserForm, change this to the name of yours
Where I Set wb = Workbooks("") change inside the quotes to the name of the
workbook the UserForm is in
The IfElse statement could be eliminated and moved to the If, if you don't need any other action on the opening with no other workbooks open
The next section of code goes in the UserForm Code. I have a button set up to show the Excel workbook in order to edit it and whatnot, you could have a region you click if you don't want a button to show up. When you want to activate the Excel sheet and the application will need to be activated. I unload (deactivate) the active thing (the UserForm). This bit of code isn't necessary if the user doesn't need access to the spreadsheet:
Private Sub See_Excel_Click()
Dim wb As Workbook
Set wb = Workbooks("MyBook.xlsm")
wb.Windows(1).Visible = True
Application.Visible = True
wb.Sheets("Sheet1").Activate
Unload Me
End Sub
Within the userform there should be a way to handle what happens when the userform is closed, as the excel application and workbook will stay open hidden in the background. I have the action close the workbook and the application. A quick note, if you set the Cancel = True then the red x button won't close the userform. The code I use for this is:
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = 0 Then
Cancel = False
Dim wb As Workbook
Set wb = Workbooks("MyBook.xlsm")
wb.Windows(1).Visible = True
Application.Visible = True
ThisWorkbook.Saved = True
ThisWorkbook.Activate
If Workbooks.Count > 1 Then
ActiveWorkbook.Close
Else
Application.Quit
End If
End If
End Sub
That is it for the code that goes inside the UserForm. And the code that is necessary to have the UserForm in VBA act as it's own application while allowing for Excel to operate normally at the same time as the UserForm.
To summarize what happens:
When the Workbook is launched the workbook is hidden, and if no other workbook is open the the Excel application is hidden.
The UserForm is initiated to allow for Excel to be used at the same time
When the spreadsheet is activated again excel is re-enabled and the application and un-hide the worksheet
When the user form is closed, the workbook is closed, and if there are no other workbooks the excel application is closed
If you set defaults or populate ComboBoxes put them in the "WorkBook" object code.

Resources