Workbook protect / unprotect wihtin a macro - excel

I'm developing a macro-enabled spreadsheet that will be used by a select group of semi-intelligent people (managers). I am complete except for one minor hiccup. I want the sheet to be read-only for their use and to require a password to open for editing for my use. Within the macro I have written, once they are complete with their actions the data they have entered is automatically saved on another tab they can not see, then the sheet is reset for further entries/interactions. Once they are done, they should be able to close the sheet without a save prompt as their data has already been saved.
Here is my paradox: I can not get the macro to save over the existing instance of this workbook. If I remove the workbook password within the macro, it saves the workbook without password protection. I have tried to:
unprotect the workbook, save it, then protect it again. Of course, the saved copy is not read/write protected.
SaveAs with the password for writing to it intact, but then I get an error message that the workbook already exists (yes, I have turned off error reporting). Researched a lot of places, can't find a solution.

Related

Protecting/Un-protecting worksheet in shared workbook with VBA

I am wanting to put a notes section into a workbook, where notes can be added about a particular row in a worksheet. There will be lots of rows.
I want these notes to be uneditable once they have been written, so they can't be tampered with.
Previously I have done this by unprotecting, then protecting the worksheet to enter the note which is captured in VBA. However this workbook needs to be shared, which means you can't unprotect or protect a workbook either in VBA or manually.
Excel really isn't the right platform for this, as it's basically a small CRM, but unfortunately for the moment it's the only thing available and that can't change for now, so need to try and find a way to make it work, any idea's?
I have tried protecting the workbook with UserInterfaceOnly:= True, and that works great until the workbook is closed and re-opened then it fails and then can't unprotect or protect the workbook.

VBA Excel display message box for all users of a shared workbook

I have a shared workbook that is updated with new data throughout the day. I'd like to be able to run a macro that will accept some text. for example: " Workbook has been updated" and display this in a message box for each user of the shared workbook. Is this possible?
I do not have an option to in share the workbook in case that matters.
I'm sure you already know this, but shared workbooks do not maintain a "connection" to the shared file per se. What happens is that each user opens a copy of the file into their own computer's memory, and then only when they save will Excel check their local copy against the file. It is only at that point that any changes are known.
When the user saves the workbook, then if other people's changes have been made in the meantime, Excel's native functionality already pops up a message saying that the workbook has been updated by other users.
If, on the other hand, you're looking for a function which would automatically pop up a message immediately when other users save the file, then it can't be done (because, as I said above, there is no live connection present). The closest thing to that would be to ensure that the workbook was saved regularly via an Application.Ontime recurring event.

Detect when password is used for opened or closed Excel workbook

I need to modify Excel workbooks from within VBA itself, where the user will point out which workbook to modify, after which a copy of the workbook will be modified and saved under a new name. This can be either an already open workbook, or a closed one on disk, and I need to support all workbook types from 2000 onward (2000-2016, binary, add-ins, templates, etc.). I need to modify the workbook's content as well as any custom UI (ribbon xml) in it. This all has to be done from random Excel installations not under my control.
The problem I'm facing is dealing with password protected workbooks - encrypted ones, i.e. a password for opening. My code needs to be able to handle those, and ideally apply any used password to the saved, updated copy as well.
The code flow is as follows:
let the user select the workbook to modify (via a form)
if the workbook is open:
.SaveCopyAs it to a temp folder
point to the saved copy for further processing
open the user-selected file in a 2nd instance of Excel (invisible)
update the opened temp copy's content
.SaveAs the temp copy to a temp folder without a password and close it
update the closed temp copy's custom UI
re-open the updated temp copy and .SaveAs it again with any password that was on the original workbook
With step 2.1 above, the .SaveCopyAs will save the open workbook and apply any password to the copy as well, which results in step 3 asking for the password in all cases. I cannot remove the pass in step 2.1 by using a .SaveAs, since that will result in the open workbook not being open anymore in the end. It would also only be a half-measure since it doesn't stop the same thing happening with closed files.
In this case, when Excel asks the user for the password (in Excel 2010 at least), the password prompt only shows the filename of the file with an edit box for the pass, all with a popped-up empty 2nd Excel window beneath it open, which is an ugly sight to behold. And it doesn't allow me to capture the entered password as well for step 7.
The best I can do I think is to detect when a closed workbook on disk is encrypted, and show my own password prompt instead before trying to open it. But how to do this? These are the options I can come up with;
When I use Workbooks.Open(Filename:=...), then Excel shows the password prompt, which I like to evade by asking for any pass beforehand myself.
When I use Workbooks.Open(Filename:=..., Password:="notthepassword"), at least Excel won't show a password prompt anymore, workbooks without a password open fine, and those with a pass now generate error 1004. However, I can't act on that to infer that a password is needed, since 1004 is Excel's catch-all error number, and I cannot inspect the Err.Description either for "wrong password" or such, since I do not know the Excel GUI language running on the client. And then there's also Check whether Excel file is Password protected ; when encrypted files have workbook structure protection on as well, apparently Excel won't open them anymore this way - I tested this and it does work with my 2010 Excel, but it's not very encouraging to hear.
Ignoring the ugliness of Excel asking for the pass, reading any Workbook.PasswordXxx property afterwards doesn't reveal anything; they always return the same values in all cases (with or without a password on the workbook).
For OOXML files (.xlsm / .xlsx etc.) I can inspect the file's zip content beforehand for the presence of the two files "EncryptionInfo" and "EncryptedPackage", indicating the file is encrypted, but what about 2000-2003 (.xls) files? There is Microsoft's documentation on the BIFF file structure used in those files telling to check for a FilePass record in the workbook stream; while I know I could implement that logic (there's e.g. the now unsupported Koogra project), I'd rather just not :) (side-question: since when did Microsoft publish these details without first signing NDA's and jumping through their legal hoops?!)
Does anyone have any insight as to how I can make code step 7. above work, short of just living with Excel's prompt and adding a key logger to my app? :)

How do I close an excel workbook using Windows "X" without prompting to save

I have a macro enabled excel file that I use as a master workbook. All this does is open up a form on workbook load and allows me to choose some options on the form to manipulate a second workbook. All works fine - I can choose the second workbook, do some actions on it, save it from code behind and close it. When I am all done I want to close the master workbook but it prompts me to save "master.xlsm". I do not want it to prompt for this as my users should never save into the master book. I can only assume excel thinks there were changes because I used the form housed within.
How can I disable the save warning or dispose of the form so excel won't prompt?
Set <Workbook Object>.Saved = True before closing it

Copy from unprotected workbook to protected workbook - Excel 2013

I received a protected excel file which only allows me to add values or select from a drop down list. The file has lots of different tabs, all in different format and questions.
I then unprotected the workbook to make my life easier (i.e. copy, paste, make notes etc)
using the code found here:
http://uknowit.uwgb.edu/page.php?id=28850
Now i am looking for a way to transfer all the values from the unprotected file back to the original file they sent me as I cannot submit an unprotected file. It is too many questions to do manually.
What is the best way to do this in excel 2013/VBA?
Thank you
It looks like you're wanting to do this on a sheet that previously had a password you didn't have access to. Also, it seems that you can't ask the worksheet creator to simply remove the password.
If both these are true, the best solution would be to save the modified sheet with the same name and send that sheet. From what I've seen, VBA and developer tools don't include a way to copy values to locked regions in a password-protected sheet, without unlocking that sheet:
ActiveSheet.Unprotect 'method to unlock current sheet, password may be required
ActiveSheet.Protect 'same to lock again
But, once a sheet is unlocked, you could loop through all cells in the region you want to copy from, and copy those values to the new sheet / workbook.
Please let us know if there's something else, or if something needs to be explained in more detail.

Resources