Excel Add-In Configure Variables and Save - vba

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.

Related

How do I save an Excel Add-In with VBA

I have an Excel Add-In which uses a worksheet in its workbook to save some preferences data (eg the last used value of a refEdit control on a userform).
I then save the add-in workbook using vba thisworkbook.save when preferences are changed in the userform.
I have found that this sometimes creates an xlsm file in myDocuments rather than saving the add-in in place (see also a copy of excel add-in is created in my documents after saving).
How can I save the add-in in place (in the add-ins folder) without creating a copy? Note activeworkbook.save wouldn't work as it saves the open workbook not the add-in.
I could alternatively create a temp file for the preferences but using the sheets in the add-in workbook seems a good place to store data.
As far as I know you can't have a file saved as an add-in and use the sheet on the file.
An add-in file is a VBA only file.
You say you want to save settings on the sheet. What about using the registry?
Saving data to the registry is very easy (actually easier than cells in my opinion).
To save a setting:
SaveSetting "MyAddIn", "Settings", "Username", "BOB"
The above line creates a value in the registry of windows with the value "BOB"
as "Username".
To get the setting from the registry you use:
Username = GetSetting("MyAddIn", "Settings", "Username")
You can read about the method here
The good thing about saving it on the registry opposed to a sheet is that you can't by accident delete or manipulate the data.
I found the answer: The user had two copies of Excel open (2016 and 2010). This resulted in the add-in file being locked by one of the two open copies of Excel meaning that the second copy opened the add-in as read-only and couldn't save the changes.
A rare bug and one I didn't foresee. I'm going to rewrite to save the preferences into an %appdata% file instead.
I'd advise people to avoid using thisworkbook.save with addins.
Thanks everyone for your help.

Hide worksheet in Excel Add-In from other VBA Projects

I have an Excel Add-In which I am building which stores users settings in a Settings worksheet within the Add-In. I use the Settings sheet essentially as storage for boolean values and potentially, the user's registration details.
However, if I open up a new VBA Project and include the Add-In as a reference (In VBA: Tools > References > MY Add-In) then I can access the Settings worksheet and potentially break the password on it and hack the registration details.
The Settings sheet is set to be xlVeryHidden however it can be accessed via VBA code. Is there anyway to prevent this in Excel? In a module for example I can use the Option Private Module method.
If you password protect and lock the project then your code and the worksheet objects can't be viewed. Go to Tools > VBAObject properties > Protection tab then tick 'Lock Project For Viewing' and then set a password in 'Password to view object properties'.
However, there is nothing to stop a hacker from...
For s = 1 to Sheets.Count
Sheets(s).visible = True
Next s
...to see your Settings sheet.
There are a few options:
1) Have the settings sheet in a seperate workbook, or some other format like .txt and read from that (recommended).
2) Use some kind of encryption in your Setting sheet and decrypt it in your code, a really simple example is to use numbers in place of text so 'example' would = '5,24,1,13,16,12,5', but obviously you want to employ something more sophisticated that than and you can search online for many ways to do it.
3) Use 2, but store the decryption code somewhere else.
Be aware though, that for someone who knows what they're doing there really is no way to prevent a VBA hack, unless you use option 1/3 and have a way to store the file somewhere that isn't publicly accessible.

VBA Password Protect Excel Workbook without Save

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.

Only I am able to save a macro-enabled workbook?

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.

Excel - Keep macros enabled when sheet is saved as new

I have an excel macro-enabled sheet that users open, fill out a list of information (name, date, etc) and click a save button. This creates a new file name based on the information, and saves the file to a network location. The users then continue to work with the sheet.
I forced the users to enable macros (excellent tutorial here: http://datapigtechnologies.com/blog/index.php/forcing-your-clients-to-enable-macros/) and that works well, however once the file name changes, macros are disabled. I assume this due to the fact that it is an untrusted network location. The sheet will be used by many different computers, so I would like to find a vba workaround rather than relying on an excel setting.
Edit: I think I have the answer: As #mehow said based on the link I have posted, I can first run the part of the macro that finds the file name, then lock the workbook all except the start page, then save the workbook as the new name, then hide the start page and show everything else. That way if macros are disabled once the sheet is saved it should force the user to re-enable without compromising the protection. I'll post once I find outwhether it will work.
Edit: It works! Thanks.