How to access VBA code from outside the Excel file? - excel

I implemented a password dialog box at the beginning of opening the file. Wrong password or closing the dialog leads to
Application.Quit
by mistake at queryclose event I also make it close the application with
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
'Here I should check type of close
Application.Quit
End Sub
What I've now closes the application if it's correct because the dialog unload and calls queryclose automatically, otherwise wrong password prompt.
I can't access the file. How can I edit the VBA code?

If you hold Shift whilst clicking open the file it will open without running macros:
http://www.jkp-ads.com/Articles/preventopenevent.asp

What about changing the extension from xlsm to xls? That is non macro enabled and should not execute itself automatically, you can also open any other excel and do the file--> options--Trust center--> settings and disable all mcaros in Macro setting tab.

Holding shift did not work for me. Trust Center may be unavailable for some users on domains. What can work is surprisingly simple, though: renaming the file and running it. Excel will treat it as a new file and block macros there by default (if security center is setup this way, though).

Related

Can I skip file validation when programmatically opening a workbook?

I have a roughly 100MB-large Excel file that I am opening with this code that runs when a UserForm button is pressed:
Public Sub SelectButton_Click()
fNameAndPath = Application.GetOpenFilename(Title:="Please Select a Report")
If fNameAndPath = False Then Exit Sub
End Sub
Because this file is so big, it takes a while to validate before opening, and this validation makes up about half the time my entire macro takes to complete (I'm working in Excel 2013 and this file is not opening from a network or shared drive). If I open the file manually, then I get the option to skip validation after three seconds of validating. The problem with this is that it opens the file in Protected View, where I can't work with it.
Using VBA, is there a way to "force skip" this time consuming validation while simultaneously avoiding Protected View?
When Excel is completely closed, this warning/guideline appears in the bottom left of the opening splash screen:
When Excel is already open, this warning/guideline appears in the bottom right:
So far there are two solutions that I've found to be useful for solving this problem, thanks to the comments above:
1. Disabling Office Validation for Excel files in the Registry settings for Windows:
I prefer to use early binding, so I would suggest in the VBA editor window, opening the Tools window, selecting 'References...', and then checking the box next to 'Windows Script Host Object Model', and clicking OK. This will enable IntelliSense as you are writing your VBA:
Dim wsh As New WshShell
wsh.RegWrite "HKCU\Software\Microsoft\Office\15.0\Excel\Security\FileValidate\DisableEditFromPV", 0, "REG_DWORD"
Set wsh = Nothing
This will work for Excel 2013. If you have a different version of Office or Excel, you will use something other than 15.0 in your file path.
However, this method has the glaring problem of removing validation of any Excel file on your computer, even those that might have malicious code in them or that might run from dangerous locations, such as your Temp or Downloads folder.
Additionally, writing to the Windows Registry might be disallowed depending on your current environment or permissions. For that reason, I recommend a different solution:
2. Setting the location of the Excel file to be a Trusted Location in Microsoft Excel:
It's possible to set a new Trusted Location in the Registry via VBA, but since this method is intended for use by those who can't access the registry, we won't get into that.
To add the file's location (let's say it's the "Excel Files" folder in C:\) as a Trusted Location, first click the File tab and then click Options at the bottom of the File Menu:
In the Excel Options window that opens up, click on Trust Center at the bottom of the side menu and then click the Trust Center Settings button that appears on the right:
Finally, click the Add new location... option and enter C:\Excel Files\ (or whatever your folder location is; you can even browse to it), and then click OK. You can also check the option to include subfolders if you want.

ActiveWorkbook.Followhyperlink

I'm currently working on an Excel program with VBA and I need to open a file. I have the code below, which all works fine except when a user opens a file a warning message appears stating that some files could contain viruses etc. This is fine, however if a user presses cancel so they decline the opening then VBA spits the dummy. Is there a way to avoid this? Thanks.
Public Sub openFile ()
Dim file As String
file = "c:\somefile"
ActiveWorkbook.FollowHyperlink Address:=file, NewWindow:=True
End Sub
By design, security is managed on the user's computer and is not to be overruled by a macro.
Have your users add the location of the file to their trusted locations so the file can be opened without being checked by the Trust Center (Excel Options -> Trust Center -> Trust Center Settings -> Add new location).
Unforetunately not, the problem I've found when using vba for excel is that it is an extension to a pre-existing user driven program so input requests are inevitable, they've caused me far too many headaches.
It could be possible to simulate a keylog to automatically hit enter and accept the warning but this solution would not be reliable and I'm not even sure possible in vba.

VBA Form disappearing

I have a VBA form that when I click on it, performs some long calculations (a few seconds to several seconds long) and then displays the results on frames in the form. However, once every so often, the form hides on me and I need to click around to the VBA editor and back to the sheet to make it display again
I have
Me.Repaint
at the end of the calculations on the form
but it still doesn't help
I also tried disabling the "EnableCalculation" attribute of the main sheet, but still no use
Anyone ever run into something like this? Do I need to load the form in some special way?
I have had the same problem. When saving an Excel workbook by clicking on a command button on a modeless Userform, the form disappears as the code completes.
I read that the problem was due to some inter-compatible changes that Microsoft has made and found some very elaborate solutions involving a lot of complex code that I was unable to integrate into my project. However, I did find a very quick workaround.
In my case the Userform only disappeared when the code executed the End Sub command of the command button's code. I simply added an Exit Sub immediately before it and the Userform no longer disappears.
Have you tried turning off and on screen updating so Excel is not redrawing the screen with each change.
sub doSomthing()
Application.ScreenUpdating = False
'do something
Application.ScreenUpdating = True
end sub
The form disappears because Microsoft has changed the model by which forms are owned by a 'superior' process (Excel in your case). I have the same problem and also cannot overcome it.
Just sit tight until Microsoft decides that it is worth their effort to fix yet another introduced and untested problem by making changes to something that already worked.
The problem is a side effect of the SDI (Single Document Interface) that was introduced with Excel 2013. The various suggestions that invoke DoEvents, ScreenUpdating, etc. may help in certain scenarios, but they do not cure the behavior.
There is a full solution posted here: Keeping Userforms on top of SDI windows in Excel 2013 and up
If that is too involved for your needs, a quick-and-dirty solution is to minimize and restore the active window, which forces the userform back on top of the stack.
'Do long-winded process first, then:
Application.ScreenUpdating = False
ActiveWindow.WindowState = xlMinimized
ActiveWindow.WindowState = xlNormal
Application.ScreenUpdating = True
This doesn't prevent the form from disappearing, but it makes it reappear the end of the process.
Try experimenting with the following two lines of code instead of the repaint.
Me.Show
And/Or:
Me.SetFocus
Hopefully, one of these will work for you!
In my case I had to use all these to make it happen. (frmGUI is my form)
DoEvents
frmGUI.Repaint
frmGUI.Show
With Office 2013 and Windows 7, simply adding DoEvents right before the end of the sub (where the form disappeared) kept the form open. Nothing else needed....for my situation.

Excel doesn't prompt to save unsaved files on Application.Quit

Using Application.Quit in Excel VBA is supposed to initiate the default quit procedure, which includes prompting the user to saved any unsaved documents currently open in the application.
In testing across numerous systems (Excel 2010 32-bit, Windows 7 64-bit), I discovered that some systems prompt the user to save, and some systems do not.
We checked the settings across the systems (including enabling all macros and disabling protected view) to no avail. Even using the same settings on the different systems did not force the one system to prompt the user to save their work before Excel shuts down.
It's as if Application.DisplayAlerts has been set to False, but it hasn't. We even tried the following lines of code:
Application.DisplayAlerts = True
Application.Quit
On certain systems, even that still doesn't prompt the user to saved unsaved documents. Excel just suddenly shuts down.
Any help solving this is greatly appreciated.
Thanks!
Update in response to comments:
I double checked the Workbooks.Saved property for all the workbooks that were opened and unsaved and it was false just before Application.Quit, so that must not be the problem.
I also just ran a simple test. I opened a workbook, edited it and executed Application.Quit in the immediate window in VBA. Excel quit without prompting to save the unsaved file. This indicates to me that it is something about my Excel setup and has nothing to do with my application.
Disabling the Solver Add-In returns the normal behavior.
I tried the same thing of disabling the Solver Add-In and that did not work. I decided to also try disabling the Hstbar Add-In. Both of them disabled made the prompt start working again.
I was using the following code to save the workbook and close the application
ActiveWorkbook.Save
Application.Quit
but after a crash excel asked me to disable the Solver. After disabling the solver Excel didn't recognise the save command and it was continuing to ask if I wanted to save the workbook despite the save command.
Then I have figured out that this bug could be solved by enable again the solver...
very weird

Disable the VBA code editor window in Excel 2010

I am looking for a possibility to add an additional level of security to my Excel projects.
We have multiple users. Some who know (and are meant to) the password that unlocks the VBA macros. Others are not meant to be able to access these.
I'm not sure if someone has the password that shouldn't (changing the password hasn't stopped tampering) so i would like to disable the VBA editing window for any user not approved. I can't find any way to do this though, is it possible?
I can't disable the save option as all users need to save data.
I have tried to disable the ribbon icons etc, but with no sucess. It is still possible to open the code window with Alt+F11.
Any help would be great.
I am coming from Excel 2003, but this concept should work for you as well. You could think of
trapping the Alt-F11 key (Application.OnKey "%{F11}" "MyNullSub") plus
disable the relevant menu entries (Application.CommandBars(...).FindControl(ID:=..).OnAction = "MyNullSub")
with
Sub MyNullSub()
' do nothing
End Sub
as a dependency of an entry in the registry that needs to be present (GetSetting(...)), but this will only help until this additional secret is spread around in the same way the (changed) passwords apparently did.

Resources