<p>I've writen an Excel-based, database reporting tool. Currentely, all the VBA code is associated with a single XLS file. The user generates the report by clicking a button on the toolbar. Unfortunately, unless the user has saved the file under another file name, all the reported data gets wiped-out.</p>

<p>When I have created similar tools in Word, I can put all the code in a template (.dot) file and call it from there. If I put the template file in the Office startup folder, it will launch everytime I start Word. Is there a similar way, to package and distribute my code in Excel? I've tried using Add-ins, but I didn't find a way to call the code from the application window.</p>

[{'answer_id': 29719, 'author': 'paulmorriss', 'author_id': 2983, 'author_profile': 'https://Stackoverflow.com/users/2983', 'pm_score': 3, 'selected': True, 'text': "<p>You can modify the user's personal.xls file, stored in the excel startup directory (varies between Office versions). If you have lots of users though, that can be fiddly.</p>\n\n<p>An alternative way to get over your problem is to store the macro in a template (.xlt) file. Then when the users opens it they can't save it back over the original file, but have to specify a new filename to save it as. The disadvantage of this method is that you then get multiple copies of your original code all over the place with each saved file. If you modify the original .xlt and someone reruns the old macro in a previously-saved .xls file then things can get out of step.</p>\n"}, {'answer_id': 29778, 'author': 'saschabeaumont', 'author_id': 592, 'author_profile': 'https://Stackoverflow.com/users/592', 'pm_score': 3, 'selected': False, 'text': "<p>Simply move your code into an Excel Addin (XLA) - this gets loaded at startup (assuming it's in the %AppData%\\Microsoft\\Excel\\XLSTART folder) but if it's a addin, not a workbook, then only your macros and defined startup functions will be loaded.</p>\n\n<p>If the functions depend on a spreadsheet itself, then you might want to use a combination of templates and addins.</p>\n\n<p>I'm distributing part of an application like this, we have addins for Word, Excel and Powerpoint (XLA, PPA, DOT) and also Office 2007 'ribbon' versions (DOTM, XLAM and PPAM)</p>\n\n<p>The addin startup code creates toolbar buttons if they're not found, this means in any workbook/document/etc they can simply hit the toolbar button to run our code (we have two action buttons and one button that displays a settings dialog)</p>\n\n<p>Templates aren't really the way to go for VBA code, Addins are definitely the way to go...</p>\n\n<p>So to load the toolbars on startup we're using something like.. (checking to see if toolbar exists though - code will run for each worksheet that is opened, but toolbars are persistent for the user session)</p>\n\n<pre><code>Public Sub Workbook_Open()\n     ' startup code / add toolbar / load saved settings, etc.\nEnd Sub\n</code></pre>\n\n<p>hope that helps :)</p>\n"}, {'answer_id': 97271, 'author': 'Dick Kusleika', 'author_id': 4280, 'author_profile': 'https://Stackoverflow.com/users/4280', 'pm_score': 2, 'selected': False, 'text': "<p>I always use an Add-in(xla)/Template(xlt) combination.  Your add-in creates the menu (or other UI entry points) and loads templates as needed.  It also write data that you want to persist to a database (Access, SQLServer, text file, or even an xls file).</p>\n\n<p>The first rule is to keep your code separate from your data.  Then, if you later have bug fixes or other code changes, you can send a new add-in and all of their templates and databases aren't affected.</p>\n"}, {'answer_id': 97289, 'author': 'Keith Elder', 'author_id': 10624, 'author_profile': 'https://Stackoverflow.com/users/10624', 'pm_score': 0, 'selected': False, 'text': '<p>Have you looked into ClickOnce deploying the Excel file?</p>\n'}, {'answer_id': 32892684, 'author': 'Muflix', 'author_id': 2333663, 'author_profile': 'https://Stackoverflow.com/users/2333663', 'pm_score': 0, 'selected': False, 'text': "<p>What about to save an excel to network folder with read only permissions ? The authentication can be done with integrated windows authentication and you don't need to store connection password to the database in the VBA. Then you only need distribute a link to this location to your users only once. If you will do an update, you only change data in that folder without user notice. </p>\n"}]
['https://Stackoverflow.com/questions/29630', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2665/']