Follow the XLL boot process. This is not a simple DLL loading that we expect when we write code loaded by a regular process.
If you have a test program that works fine, but then you add the working code to the Excel add-in and get the message "This file may be damaged or dangerous" ... I suggest you:
- Check the initialization code for calls that Excel does not allow during the download process.
If during initialization, your code makes a call that Excel does not like, you will receive a meaningless error message, and your add-in will be reloaded as a text document. Unfortunately, I did not do my homework regarding the restrictions imposed by Excel; In general, I found that:
- The problem can be solved very simply by delaying initialization.
So far, I have found that the AutoOpen event was convenient (although there may be a better way - let me know if you find it.) I have successfully used the following concept to avoid this problem:
// within the AutoLoad event handler static bool init_completed = false; if ( init_completed == false ) { initialize_all(); init_completed = true; }
This allows Excel to successfully load XLL. By the time the AutoLoad event occurs, it seems that Excel does not impose any restrictions on the code, allowing you to execute the initialization code.
Again, an AutoLoad event might not be the best place - YMMV - so please refresh this page if you find something better.
I really hope that I will quickly find this page the next time I make this mistake!
source share