It is possible to password protect an excel workbook for opening without the use of SaveAs method (without any method to save the file)?
Currently in our application, we use the SaveAs method to save and set the password after a workbook is generated and filled with data. The generation of this workbook takes a while as we use a lot of data and the resulting file is quite big (120MB).
We were searching for some ways to accelerate the workbook generation and we found that the SaveAs method takes around 3 minutes to complete because a lot of formulas needs to be calculated. After a small talk with the users, we realize that everyone make some small changes to the UI of the workbook anyway so then they need to save again and wait for another 3 minutes.
We have decided to suppress the SaveAs method and will be the users responsibility to save the workbook after they finish with their changes. And here is the problem, without the SaveAs I couldn't find a way to protect the file with a password.
I have tried the Workbook.Protect but it seems it only protects the structure and the windows from changes but not the file from opening:
ActiveWorkbook.Protect Password:=cPwd, Structure:=True, Windows:=False
I also found the Workbook.ProtectSharing method but I did not tried because the documentation says that it saves the file.
So, is there any way to set a workbook password without saving the file that will be applied when the user save it manually after his changes?
You can use the Workbook.Password property, which is read/write. The password will be applied the next time the file is saved.
Related
I have a chunck of VBA code, which opens 2 files, copy the content in each, and paste that into a third file.
The problem is, that the two files (lets say "alm" and "fiber") often are used by other users, thus when i use Set alm = Workbooks.Open(alm_path) I get an error, since Excel cannot open it. I assume it is due to the file being opened by another user, and I then have to open it as write protected. Is there any smart way to do so? I am fairly new to VBA code
As mentioned in the comments you can open the workbook as ReadOnly, an example below:
Dim alm As Workbook
Set alm = Workbooks.Open(Filename:=alm_path, ReadOnly:=True)
I have been asked to create an Excel Add-in for people to load a series of xml documents into Excel spreadsheets from Excel. I will write an add-in and publish its location for people to download their own local copies for use. I would Ideally like to have some constants for the VBA configurable by each end user, to customize their experience and function.
I thought it would be nice to have one button to run the Add-in and another button called "Configure" or something, which would edit the values of some of the variables in the VBA, and save the new values so that next time the user open Excel, the Add-in remains configured for them. Is anything like this possible and do you have some suggestions about what path I should follow to get there?
Is there any way to get the variables to persist? I guess I would need to save the values somewhere on the local version of the Add-in, but if so, what is the best way to store a set of parameters?
Any general advice would be most welcome; I have not actually written the add-in yet, being still in the design stages.
I do not advise using cells to store settings as the user may F it up.
However VBA does support Registry edits.
SaveSetting "Macro name", "Settings", "Username", "John Doe"
The above code will save a setting or registry key called Username with the name John Doe in the appname called Macro name and the section Settings.
So in short you only need to change the last two strings when you save new settings. The first two should (to make it easy for you) be the same all the time.
The keys will be stored in : HCU\Software\VB and VBA Program Settings\Your app name\
To get the setting you use:
GetSetting("Macro name", "Settings", "Username")
Another solution is to use txt-file.
It's still better than cells but not as good as above mentioned registry.
settings = "Username=John Doe, Worktitle=Janitor"
MyFile = "C:\myapp\settings.txt"
fnum = FreeFile()
Open MyFile For Output As #fnum
Print #fnum, settings
Close #fnum
Now you have a txt file with the settings that you can read and parse.
The Workbook.Save line in my macro is holding everything up, and while it's important that there's a save step at the end of the macro, I don't mind if it just starts saving and then hands control back to the user.
Is there such a thing as Workbook.Save BackGround or Workbook.Save vbModeLess?
Is there such a thing as Workbook.Save BackGround or Workbook.Save vbModeLess?
Definitively, no. The full list of methods available to the workbook object:
http://msdn.microsoft.com/en-us/library/office/ff847316(v=office.14).aspx
The .Save method does not have any optional arguments:
http://msdn.microsoft.com/en-us/library/office/ff197585(v=office.14).aspx
It seems you are perceiving a "problem" with your code which is not actually a problem, but normal and expected functionality, as I explained in the comments above:
When a user manually saves the file, the application is not interactive. The user can't do anything except wait for the save to finish.
The same occurs when you invoke the .Save method of the workbook object, or the Application.CommandBars.ExecuteMso "FileSave", etc.
This is necessary because (obviously) changes made while saving would not be saved, but the workbook's .Saved property would display True.
This property is used in determining whether to show the "Close this workbook with unsaved changes?" dialog when the user closes the file. If the property is True, then the user can close without any prompt. But of course if you let them make changes this will inevitably lead to unwanted data loss as the user may then close the file with saved state True and unsaved changes to the workbook which have not been reflected in the Saved property.
(Note: there are probably more technical reasons, too, but this is just the common-sense explanation)
If the length of time it takes to save the file is burdensome, you have at least a few options I can think of, first you would want to consider notifying the user that the file is going to be saved and this may take upwards of 45 seconds. This way, they do not think the program is unresponsive or otherwise hanging. You can do this with a MsgBox or a UserForm pretty easily.
Alternatively, you could use either of the above methods to prompt the user, i.e., "Do you want to save the file?"
Etc.
When excel saves a file it created a temporary file with a name like A82732KS.tmp and the quickly delete the original file and rename the temp file (possibly in an atomic operation). To do this excel has to release control of the file to avoid a sharing violation so it necessarily disables any changes in memory in order to guarantee that was is written on file and what is loaded in memory is identical.
All, I have created a workbook that has some macros in it to import data. The idea is that the file is a master file, and every time you import data with it, it is supposed to place that data on the end of the existing data, and then you save it and move on.
The problem is, I am the only user that can save the workbook. Now, two of the sheets in my workbook I have protected, so that they cannot be edited. I have done this so that nothing can accidentally be removed (buttons, instructions, notes, etc). My users have agreed that this protection is a good thing.
But what I think is happening is our network is making anyone who didn't author this file open it as Read Only, and then they cannot save to it. I first thought maybe if added a save macro (and command button) that it would fix it. No dice.
Next, I had the workbook unprotect, and then re-protect itself when the user clicks the save button. Nope, still opening as Read Only.
I then put code in the Workbook_Open() Sub that changed it from Read Only to Read Write. This caused a box to popup when opening the sheet that said the file was in use by "Another User," and it was locked for editing.
The last thing I tried was adding the other users as Authors to the workbook. And it STILL opens as a Read Only file.
I think this has to do with the network settings here in our office (well, corporate-wide, but anyway). These are policies that cannot be changed. Can anyone help me find a work-around that allows my sheets to be locked for editing, but allows my users to save to my workbook?
You can see here that I have added three other users as Authors of the file (This is the information page of the file as opened by a user.):
So it turns out that the issue was not related at all to the workbook...
When attempting to Save As in the folder where the workbook was located, we found out that the user I was using for the tests simply did not have write permissions to the folder in question. I had write permissions to it based on a previous assignment.
So much frustration over something so simple.
All of this is being done in VB.NET using the Excel 14.0 Interop Services
I am at my wits end. I keep getting prompts from windows and excel during the middle of a batch run.
The program i have takes in a workbook with batch records, then runs simulations on each batch record, then writes the results back out to the excel file.
The steps:
Open workbook
check to see if workbook is already in use by another program.
if it is in use. we try to close the workbook. then we wait for a set amount of time before trying again.
if the workbook is not in use we continue.
Get the contents
Mark the records as being processed
save and close the file.
process the records.
do the same process above to open the workbook.
save the results to the workbook.
close the workbook.
loop these processes until all the records have been simulated.
Ok the problems that can occur:
Workbook is already in use or two programs are trying to interact with the save workbook at the same time.
Ok now for the problem that i am having.
When the workbook is being interacted with by two programs at the same time. a prompt will show saying the file is currently in use.
another problem that happens that i can't explain is excel will show a prompt saying that the file is now ready to be modified with the options read-write, notify, cancel.
I need to find a way to handle these prompts programmatically.
If any one can point me in the right direction I would be very greatful.
You can prevent the prompts from appearing by setting:
Dim xlApp As Excel.Application = New Excel.Application
xlApp.DisplayEvents = False
But I've not found a way to actually "catch" the prompts and do something useful. I've noticed that often if Interop cannot get hold of a file then it will throw an exception. The exception rarely contains any way to distinguish what the actual error is, but you can sometimes work it out based on what could happen at that point.