Excel 2013 vba created workbook opens for users in "Protected View" - excel

I have VBA code that spins through a worksheet and creates workbooks that are emailed to clients. It's pretty simple, really, it writes the output files to My Documents. Here is the save code:
With wkbOutputBook
.CheckCompatibility = False
.Save
.Close
End With
When the worksbook is opened, Excel says:
PROTECTED VIEW Office has detected a problem with this file. Editing it may harm your computer. Click for more details.
OK I know what this means, for reasons unknown Excel thinks this file was sent through Outlook, or downloaded from the Internet. But it wasn't. I've tried the following:
Save in different versions/file formats
Use Save As instead of Save
Explicitly turn off protected mode. Protected mode isn't the same thing as Protected View though.
Surely "My Documents" isn't considered an "unsafe" location.
Thanks.

If you can accept files being saved in read only mode, this should probably eliminate an error message.
Dim path As String
path = "full-path-with-filename-and-extension"
With wkbOutputBook
.SaveCopyAs (path)
SetAttr (path), vbReadOnly
.Close
End With
You try .SaveCopyAs (path) method without code line involving setting read only, but I guess it won't help.
What format do you try to save your file as? Does the problem appear only on your PC or have You tested your macro / output files on other PCs? The problem can involve stuff in system registry.

Related

Open protected Workbook in background when it is already open by other person - VB.Net

I have a issue.
My code opens protected workbook in background - and it works fine, no issue
But then workbook is already open by other user password window pops up - how to change that so user won't be able to see that password window?
I add on error goto errhandler which contains that workbook is already open - it works fine when item is no protected, but with password this password window pops up.
Thank You in advance
on error goto errhan
Workbooks.Open Filename:= "C:\johndoe.xls", Password:="Password"
exit sub
errhan
messagebox.show("File is already open")
Any suggestions?
I've been unable to reproduce your exact issue. Using Microsoft.Office.Interop.Excel if I try to open a password protected workbook that is already open, I get the following message:
This is how I'm opening the file:
wb = wbs.Open(Filename:=Location, Password:="password", Notify:=False, IgnoreReadOnlyRecommended:=True)
This is what I would expect, but you're saying isn't happening.
As a side note, I don't think the Notify property works as expected; even with Notify:=False it still raises the notification.
The main issue you've got is the when using Microsoft.Office.Interop.Excel all of the file opening logic is handled by the Excel.Application instance rather than your application. For example, if you don't provide a password it will ask for one, rather than throwing an exception. This is useful in some cases, but for automating tasks it's a bit annoying as without any feedback you can't write proper logic control.
I would recommend you look into using something like EPPlus to handle your Excel file reading/writing. Not only is it less resource intensive (you don't have to spin up an Excel application instance) it also throws exceptions when the file needs a password, or when it's being used by another user, which allows you to handle all these different situations in your application and minimalise the amount of user interaction that's required.

Running vba code beforesave despite read-only status

I've created an essentially blank workbook with lots of macros which loads data from variable sources ("Dashboard"). Using the workbook_beforesave event, I've hijacked the Save option into a function that saves the data as a separate .csv file, which can then be loaded into the workbook on opening. The dashboard is stored on a shared drive, where the end-users can access it. I have a test version and a live version.
The issue began when one of the end-users started leaving the dashboard open (locking it), and even somehow disabling events and saving over the blank workbook with their data. Easy enough to restore from the test version, but annoying.
Setting the file to read-only on the server creates a host of unmitigatable issues (server is password protected, excel starts just asking for access credentials repeatedly).
My solution was inserting the following line in the workbook_open event:
If Left(ThisWorkbook.Name, 4) <> "TEST" Then ThisWorkbook.ChangeFileAccess Mode:=xlReadOnly
Which created another problem. Now, when I click the Save button, the workbook_beforesave event doesn't fire until I've clicked OK on the "Can't save because it's read-only" notification, and then selected a folder to save it in.
DisplayAlerts does not get rid of the "Can't save" notification.
The question: Can I programmatically move directly into my save macro, dismissing the alert and avoiding the folder selection step? Or do I need to create my own Save button and sidestep the issue entirely?

Workbook.SaveAs Sharepoint Integration

I have developed an Excel file that contains a button with a macro that will save the workbook for the user. This has worked well until recently when we are migrating to Sharepoint and users are trying to save the workbook to a mapped drive that is mapped to a Sharepoint location.
So, for example, my user has \\<server#SSL\sites\<sitename>\<documentlibrary> mapped to her Z drive.
When I use ThisWorkbook.GetSaveAsFilename the result returns a url like so https://<server#SSL\sites\<sitename>\<documentlibrary>\<filename>.xlsm
Before making any changes, I was getting a run-time error on the line
ThisWorkbook.SaveAs Filename:=excelFile
where excelFile is https://<server#SSL\sites\<sitename>\<documentlibrary>\<filename>.xlsm
My first attempt was to do some find/replace on the string. First replacing https: with nothing and then replacing the forward slash / with the backslash \ to make it resemble a file path like so:
\\<server#SSL\sites\<sitename>\<documentlibrary>\filename.xlsm
There was some progress made here. I received no VBA error, however I did get a message:
UPLOAD FAILED We're sorry this didn't work. Try saving again later
and if that still doesn't work, click Save a Copy
and I see the file attempting to be uploaded in the Microsoft Office Upload Center, so this is progress.
I can't seem to make it around this upload failure message.
I have tested that my permissions to the document library are sufficient by manually uploading the file that I am attempting to automatically upload.
So, how can I use ThisWorkbook.SaveAs to save a file to a Sharepoint document library?
I am using Excel 2013 and Sharepoint 2013.
While I cannot explain it, after some tinkering around, I believe I have found the solution.
Changing
ThisWorkbook.SaveAs Filename:=excelFile
To
ThisWorkbook.SaveAs fileName:=excelName, FileFormat:=xlOpenXMLWorkbook, ConflictResolution:=xlUserResolution
Seemed to make it work.
I explicitly set the FileFormat and ConflictResolution arguments and the user is able to save.

Open Excel workbook as read-only via VB6

I have an application written in VB6 that writes data to a spreadsheet. I'm using the MS Excel 11.0 Object library to create an instance of Excel and open the book:
Dim xlApp As Excel.Application, remoteBook As Workbook
Set xlApp = New Excel.Application
Set remoteBook = xlApp.Workbooks.Open(sheetName)
In addition to writing to the workbook "sheetName", the program also allows the user to launch the workbook in order to view the cumulative results.
There is a chance, however slim it may be, that a user could have the workbook open for viewing the results while someone else is trying to write to it. I want to give the user writing to the sheet priority. Is there a way I can launch the sheet for viewing as read-only? There is a read-only property of the excel application object, but it is (of course) read-only.
How can I set up my program to write data to the workbook even if someone has accidentally left the file open at their desk?
Simply do this:
Set remoteBook = xlApp.Workbooks.Open( sheetName, , true)
Where true is whether or not to open as Read Only. ReadOnly is the third parameter to this method.
I think you might be able to do it via the Workbook.ChangeFileAccess method as described here. Not sure if it will suit your circumstances though.
Let me make sure I have properly interpreted your issue:
Your app writes an excel file
The App launches the file in Excel
to the User
Now here's what I think you're saying:
Once the user is viewing the sheet, they may or may not want to edit that sheet.
In other words, you don't want to use
Set remoteBook = xlApp.Workbooks.Open( sheetName, , true)
100% of the time because the user viewing may want to change the data.
The downside is that this dastardly user may leave the file open to prevent other users from writing to that file.
Is that correct?
If so, it sounds like you may need to explicit state in your app to "Open for viewing" or "open for read-only" access and then toggle the Read Only property appropriately; which is probably undesirable.
However, you can't force a save on an office doc once someone else has it open.

Excel VBA - Password prompt after Form Show and Import Data

I have this macro that is password protected and shows a form. Recently I added code to import data from excel and every time I close it asks me for a password - I can cancel through it but I'd like to make it go away, I see no plausible reason why it would come.
I've separated out the few lines that causes this problem
Sub a()
UserForm1.Show
strConnString = "Provider=Microsoft.Jet.OLEDB.4.0" & _
";Data Source=" & "C:\Book1.xls" & _
";Extended Properties='Excel 8.0; IMEX=1'" & _
";Persist Security Info=False"
Set objRSGlobal = CreateObject("ADODB.Recordset")
objRSGlobal.Open "select * from [Sheet1$]", strConnString
objRSGlobal.Close
Set objRSGlobal = Nothing
End Sub
This goes into Module1. I have a blank form called UserForm1. I lock the code (for viewing as well) using a password and save everything in Book1.xls.
Now I open the file, do an Alt+F8 to run the macro, close the form and then close the file the password prompt comes up. I can't understand why it comes up and how to make it go away.
Thanks in advance!!
Note - for the record, this also happens if I just show the form and do an Import Data via the UI (Data > Import External Data > Import Data)
Update - it looks like its happening only when I import from the same file I have open.
This looks like the problem described in Microsoft Knowledge Base Article KB280454
http://support.microsoft.com/kb/280454
I encountered this problem working with .NET and COM interop. The article advises ensuring COM references are released. I was unable to do this successfully in my case. I tried delegating commands from Excel to a non-VBA-password protected workbook which reduced the occurrence and finally set up a process to kill my app (with Xtreme Prejudice, using a second process). Neither of these seem appropriate in the case of this question but a similar kludge may be required.
http://www.officekb.com/Uwe/Forum.aspx/excel-prog/40862/VBA-project-does-not-close-when-workbook-is-closed
looks like a google desktop issue.
I had the same issue, and it is related to the Excel/Jet queries from open Excel Workbooks (including the current workbook). The following Microsoft kb article describes the problem.
The issue seems to be fixed in Excel 2010, although that did not help me (as my clients are still using Excel 2007). One way to do it is to save the data into another workbook, query it there and then copy the result back in to the original workbook, but that is a bit cumbersome.

Resources