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.
Related
When you click a link in an Excel workbook, you get a warning 'Some files contain virus ... Would you like to open this file?'
Is there a way to turn this off/include it in a macro or script?
I have a macro that opens the link, but you have to manually click 'Yes' to keep the macro running. I want to run the macro and not have to do any user inputs. I've google'd it and there's a bunch of Registry Edit methods which don't seem to work for me. Any other ways to accomplish this?
There is a good reason for the macro runs to be prevented per default. Power plants have been attacked successfully because someone did run an Excel macro. The world would be a better place if the macros never existed. Look at least over the first 10 minutes.
https://youtu.be/xVBaPZqoLkc?t=910
Security that can be bypassed is no security! Actually that is why you cannot bypass security.
There is no way to bypass this warning. The messeage is there for a good reason.
Otherwise your macro could download a dangerous file and run the code in that file without warning the user.
I automatically open, edit, save and close several Excel workbooks from a Sharepoint location. The following code opens the workbooks (path loops through a list to hit each workbook name):
Workbooks.Open Filename:=path, ReadOnly:=False, Editable:=True
The files open in Read-Only mode, and the yellow dialogue option to Enable Editing does not appear.
I edit these workbooks manually and through a macro, but I am unable to save the files back onto the Sharepoint afterwards without saving as a new file.
I am using Excel 2013. This was working as intended about a year ago, but I believe there may have been updates to Office 365. I checked all of the Excel workbook security options, and nothing is set to open by default as Read-Only.
Is there any way to open the file in an editable mode through the macro, or at the very least allow the Enable Editing option to appear for each workbook?
I have been trying to fix the same problem for my files, and eventually did! So I felt that I could maybe let others know. And this old-ish thread came up near the top of my google search.
What fixed it for me was to edit the link.
from:
https://Company.sharepoint.com/:x:/r/teams/TeamNo/Shared%20Documents/Example/CoolFolder/TheBestExcelFile.xlsm
To:
https://Company.sharepoint.com/teams/TeamNo/Shared%20Documents/Example/CoolFolder/TheBestExcelFile.xlsm
Note that I only used replace to get rid of :x:/r/. I feel like I should have noticed this before but I didn't and no amount of meddling with the Workbook.Open parameters got me anywhere. It just seems odd that the default link copy thing gives you one with special commands in it. For our company most folders have spaces so the link has tons of "%20" in there so I simply read over the ":x:/r/".
Hope it helps someone.
Just for clarity, try this:
Sub Example()
'1.) Get filepath from somewhere
FilePath = Replace("https://Company.sharepoint.com/:x:/r/teams/TeamNo/Shared%20Documents/Example/CoolFolder/TheBestExcelFile.xlsm", ":x:/r/", "")
'2.) Open the file
Set StatisticsFile = Workbooks.Open(FileName:=FilePath, Password:="123")
'3.) Do things
'4.) Close the Sheet, save the changes. I simply like it this way, could be done in a single line.
StatisticsFile.Save
StatisticsFile.Close savechanges:=False
End Sub
I noticed this solution because I could still save the .xlsx file manually with the same name if I navigated to SaveAs. So if you guys can still do that after opening the file via macro, try a similar solution.
The interaction designed for Excel-OneDrive-SharePoint is new in 2016 apps and that version is a requisite to properly work.
The version 2013 may work by tweaking the OnDrive “Account” settings regarding Office co-authoring configuration which is specifically applied to Excel and Word
Right click the OneDrive icon in the taskbar to reach settings
Good luck!
I know your query was posted long ago but I have found the solution to remove the Read-Only blocker and update the excel document via Macro:
If you add "ActiveWorkbook.LockServerFile" after the code of opening the file, then it removes the Read-only and updates the excel as normal.
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.
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).
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.