I think that question has been asked in different ways. But I didn't found a satisfying solution. I want to unlock a VBA-project using vba code. Possibly something like the .unprotect method for workbooks or -sheets. I know the password, I do not need to crack open the project.
Is there a simple way to realise this?
Thats not possible. The Password is not exposed in the VBA Object, so you cant directly access it trough VBA Code. There is only a method to send the keys for the password, but i wouldnt recommend it.
Related
I am about to submit my big VBA project in Excel, and have come across one last concern.
I have protected all my macros, but how can I set this Excel so that users cannot either A) Access the 'Developer' Tab, or B) Create new macros?
You can't.
I mean, you can try, and probably stop an accountant or sales analyst from looking at or editing the code, but you won't stop anyone that wants or needs to go in.
In other words "protecting" the VBA code will only annoy you, the maintainer. It will prevent a cat from accidentally accessing the code, but anyone with access to a hex editor (or not) will laugh at it.
Unless you're using some good 3rd-party tools to obfuscate/encrypt/protect (/corrupt) your VBA project (listing 3rd-party tools is beyond the scope of this site)...
VBA code is not secure, full stop.
My personal workbook (PERSONAL.xlsm) has stopped allowing me to view the code or any details of the modules I have saved there. It also will not allow me to run any of the Macros it once included. I don't know if they got deleted or are hiding somewhere (but I am hoping it is the later). I have not done anything out of the ordinary. Before this issue was happening all I had done with excel today was run one of these macros (without issue), make an edit to include the solver in a macro, and save it.
In addition to this problem there are a few others that seem to accompany it:
Occasionally when I close excel without saving a file, excel crashes.
If I attempt to record a macro (either in the Personal Workbook or to
another location) one of the following happens:
a. Excel crashes.
b. Excel gives me an Invalid Name Error (despite the name being perfectly valid.
It is worth noting that macros not saved in the Personal Workbook work just fine. I am very stumped and cannot find a solution to this problem anywhere. I have tried the obvious (rebooting my computer), the not so obvious (restoring previous versions of files), and the weird (disabling and enabling random things in the excel options section). Please, if you have a solution let me know!
Thank you in advance, all and any suggestions are appreciated!
I had a similar problem a while ago. I had to:
close Excel
re-name PERSONAL.xlsm to something else (like temp.xlsm)
open Excel and verify it does not "see" or attempt to open PERSONAL
create a new PERSONAL.xlsm
copy all VBA from temp.xlsm to the new PERSONAL.xlsm
Can't comment yet, so i apologize in advance.
It is definitely Excel at fault, it happens to me so often (on big files including VBA) that my before_save saves a copy of the file with time in its name, and all module's, userform's, sheet's code as *.frm, *.cls, *.bas.
I usually can also open corrupt files by holding SHIFT key (force designer mode) and then copy manually stuff.
I am currently working on a VBA project in Excel where I need to unlock a VBProject and also lock another VBProject. So far, I have been doing this with SendKeys, but I keep reading that it is not a good method, and that API is better? (For example in this thread: Unprotect VBProject from VB code)
However, I could not find any detailed information as to why during my research.
Could someone please tell me why exactly SendKeys is bad? What are the things that could go wrong? (Please note that my SendKeys sequence is only 1.5 seconds long at most.)
Also, why is API the better approach?
Thanks! :)
WinAPI uses things like window handles (you may have seen hWnd in code before?) to target a specific window. Once you have this you can send and receive messages to that window regardless of it's window state (active/inactive) etc.
You are working directly with the object, which is the way programming should be.
The SendKeys() method just emulates a user hitting keys on a keyboard, irrespective of what window is open and where - so it naturally sends the output to whatever object is active and able to receive it.
Another way to think about it
If you're coding to place a value in a cell on a certain sheet in VBA you can do the following:
Range("A1").Value = "Foo"
This is all well and good, but it assumes that the sheet we want is the active sheet at that moment in time. If it isn't, the wrong cell on the wrong sheet will be populated instead. This is effectively what you are doing with SendKeys()
This on the other hand:
Workbooks("Target Workbook.xlsx").Sheets("Target Sheet").Range("A1").Value = "Foo"
Specifies the exact cell, in the exact sheet, in the exact workbook that we want to target - so if that sheet isn't active at that point in time then no worries! It will still go to the right place (this is kind of what you're doing with API)
A WORD OF CAUTION
Playing with WinAPI in VBA can be risky if you don't know what you're doing - the code for these methods is pre-compiled in an external library which means your VBE error handler isn't going to be of any use. If you make a mistake with API you run the risk of corrupting your workbook (or worse depending on what you're actually doing).
You also need to look at conditional compilation in VBA, because you have to declare functions and parameters differently depending on whether you're using a 32-bit or 64-bit version.
I have been searching this for a while without any positives . We can create new modules, form ,report, but macros .Can we at all do this? This post asks the same question but answers another one.
"Create a macro for Microsoft Access via Interop "
here VBA module is being added not macro.
Theoretically it seems feasible as macro is an access object so why we cant do it ?
I dont think you can create macros via vba.
There is an object AllMacros (Application.currentproject.allmacros) and its members are representations of the individual macros in your project - but they are of the type Object, thus there doesn't seem to be any vba representation of the macro object.
You also can't import macros, autogenerate them or convert from vba to macros... So it seems pretty obvious ms isnt encouraging using them and hasn't done anything for us to create them
I am making a primitive database in Excel and need a routine to run in the background constantly. I will be able to fill in the actual actions it needs to do, but I don't know how to make something run independent of a key press or some sort of Macro. If someone can give me an example of code that runs independently which I can simply fill with contents, that would be much appreciated.
Try this http://www.ozgrid.com/Excel/run-macro-on-time.htm
I haven't worked with VBA for quite some time now, but you would have to create a thread in VBA, maybe by COM-Threading, but I am not too sure if Excel is threadsafe or if you even can use Threads of some kind in Excel. Another alternative would be a Timer. If it is not implemented in VBA and I don't remember it to be, you would have to create the Timer yourself and react to the Windows Message yourself, if you can do that in VBA.
All in all I think this uses case might be to big for an Excel VBA Macro. If I were you I would consider doing this in a different way.