Creating private intance of excel for app - vba

I am making an app using Excel, with all user interaction happening in userforms. I would like my app to run in a separate instance of excel than any other file already opened, or any file opened manualy by the user while my app runs. I found the folowing link to be relevant and interesting but I am not sure how to implement this in my application: Answers.microsoft.com Can I create an instance of Excel that can only be accessed by my VBA project?
Here is how I would like to implement it:
I use two files: StartFile.xlsb, SystemFile.xlsb
The user opens StartFile normaly by clicking on the file
Sheet1 of StartFile displays a warning if macros are not enabled.
Once macros are enabled, StartFile checks excel version etc...
If all is good StartFile then opens SystemFile in a new instance of excel, and closes itself
SystemFile opens and somehow makes its excel instance private (Using "Application.IgnoreRemoteRequests = True" ?)
Clarification: The user will not be able to interact with the private excel instance application at all. Its .visible setting will remain false. It is used to display userforms, so to the user this will look like a standalone app
Sidenote: How do I make sure my new instance of excel has macros enabled no matter the macro security settings? (the user has already enabled macros once) (I have not noticed this happening so far, could it?)

Is it something like this you are looking for?
You cannot change the macro security settings as that would undermine the whole purpose of having these settings. Alternative, you can properly search for a VBA module to changing the Windows registry for this.
Furthermore, be careful if you use IgnoreRemoteRequeststhen you have to reset it again everytime.
** Edited ** Hide the SystemFile workbook and just show the userform? And then close the SystemFile workbook when the userform is closed.
Private Sub Workbook_Open()
Application.Visible = False
UserForm1.Show
End Sub
'UserForm module
Private Sub CommandButton1_Click()
Unload UserForm1
Application.Quit
End Sub

Related

Getting a .xlsm file to not execute code when being opened with VBA

I have a macro in Excel. Part of that macro opens up other workbooks using Workbooks.Open(Filepath).
Some of the workbooks I'm opening have (badly done) VBA code inside of them, that try to then calculate a UDF and fail horribly.
Without resorting to an On Error Resume Next or an On Error GoTo, how do I say "Open this file, DO NOT RUN ANY CODE".
Trying to avoid letting the code execute in the first place due to possible security concerns. I feel comfortable enough opening these files, and there's a decent chance that they're not going to be compromised, but breaches can happen, and why have a hole when I don't need one? I also don't want error messages interrupting my code executing.
Searching the web just shows me shift+open opens the file without executing code, which is not at all what I'm looking to do. I'm looking for the equivalent VBA method.
AutomationSecurity is likely what you want:
https://learn.microsoft.com/en-us/office/vba/api/Excel.Application.AutomationSecurity
MsoAutomationSecurity can be one of these MsoAutomationSecurity
constants.
msoAutomationSecurityByUI . Uses the security setting specified in the
Security dialog box.
msoAutomationSecurityForceDisable . Disables all
macros in all files opened programmatically without showing any
security alerts. Note This setting does not disable Microsoft Excel
4.0 macros. If a file that contains Microsoft Excel 4.0 macros is opened programmatically, the user will be prompted to decide whether
or not to open the file.
msoAutomationSecurityLow . Enables all
macros. This is the default value when the application is started.
You can disable the macros for newly opened files, open the workbook, and then re-enable the macros:
Private Sub OpenWorkBookMacroDisabled(wbPath As String)
Application.AutomationSecurity = msoAutomationSecurityForceDisable
Workbooks.Open (wbPath)
Application.AutomationSecurity = msoAutomationSecurityByUI
End Sub

When opening a workbook via IE, a form that is activated on Workbook_Open appears behind internet explorer for some users

For some users of an excel workbook, a login form that is activated on workbook_open appears behind the Internet Explorer 11 window that they use to access the workbook. (Yes, the user has either made this a trusted document or thier security settings in excel automatically enable macros)
I am unable to replicate the problem myself but have seen it occur via screen sharing.
I have tried the workbook.activate and setfocus methods but this doesn't help the users in question. Best case scenario may be to automatically minimise IE from the workbook when the form is activated (if this is possible).
Is there a fool-proof way to ensure that the excel workbook, and specifically the login form, is visible when the user opens the application via IE?
My code as it stands is ludicrously simple having removed failed attempts to remedy the problem:
Private Sub Workbook_Open()
frmLogin.Show
End Sub
I have not asked many questions on this site. Please let me know if more information is required.

Auto displaying form on opening a template file, dotm from explorer

I have written a form based document generation macro (in VBA) for distribution to a sales team.
For their ease of use, I want to provide a self-contained file which will display the form as soon as the document is opened.
Using AutoOpen I can get the form to display as intended if word is already open and the dotm file is opened within. However, if I double click the file from within explorer, nothing happens and I have to launch the macro manually. I thought AutoExec might allow this but no luck there. I've spent considerable time trying to get this to work through googling etc. but I'm not getting anywhere.
How can I make the form display even when the file is opened with a double click? Is it possible to do this without having to change normal.dotm for each user?
For further background, I am using Word 2013 with macros fully enabled during testing. The dotm file is stored in a trusted location.
I am using a macro to launch the form like this...
Public Sub AutoOpen()
StartPage.Show
End Sub
I have tried using AutoExec as well to no avail.
In the "generator.dotm" file got to Visual Basic and go in to the "ThisDocument" Microsoft Word Object.
At the top of the Visual Basic Editor select "Document" in the left hand side and then click on "New" on the right hand side. Private Sub Document_New() method will appear for you to be able to edit. Then you can call your userform in there. Similar to:
Private Sub Document_New()
Dim myForm As UserForm1
Set myForm = New UserForm1
myForm.Show
End Sub
Save your Generator.dotm and double click it through Windows explorer and you should get the results that you would like.

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.

Can excel macro run on browser?

Is it possible to run a macro on web browser? I can open an excel in a browser but i am not sure if macro can run with it. And also, how am I going to enable it automatically within a browser?
If I am correct in assuming that you're asking if a vba macro can be run automatically after opening it from a link on a webpage rendered by a browser, the best way to do that is to set code to run automatically whenever the workbook linked in the webpage is opened.
To have a macro run when a workbook is opened double click the "ThisWorkbook" object in the vba project explorer of the workbook that has your code and add these lines of code
Private Sub Workbook_Open()
Call Module1.hello 'Change this line to start your code
End Sub
This automatically executes the sub "hello" in Module1 of the workbook.
Note that the user has to have Macros Enabled in their Trust Center Settings for anything to run.
Also note that depending on their browser and where the actual link points to the workbook file might have to be downloaded first and then opened from the download location for the macro to run.