Excel - Keep macros enabled when sheet is saved as new - vba

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.

Related

How to move macros from one user login to another?

I have previously made macros file and trying to move to other user id.
However when i am clicking macros options in the same excel file from other user id i am unable to see personal.xlsb.
Can anyone please guide step by step how to implement (copy/paste) macros?
You have to save the original file as macro-enabled (.xlsm), not default (.xlsx). The other user will have an option to enable macros after opening macros.
You can also consider using macro-enabled template file (.xltm).
Since macros can be designed to damage system (like virus), it makes sense not to allow them in the default file format.

Run-time Error '32809' and Active X controls changed name

Huge problem here. I sent an excel form (a workbook with several sheets, thousands of active x controls and many many macros) to several people that they need to fill and return to me. Only one of the completed form I received is now completely impossible to use. All active X controls have changed name. If I select one I see the old name, but if I click on it the macro doesn't start and if I try to see its code it opens a new empty Macro with the new control name_click (Ex: a button called cmd_button1 opens a Macro called cmd_button2_click). When I start the Macro in VBA, I get the Run-time Error '32809' every time the code wants to unprotect sheets or to change the visible value of a sheet.
I looked on other posts and every solution I found would require a huge amount of work. What happened? Is there an easy solution to this?
Thank you.
I saved the workbook in .xlsx and copy/pasted all my macros back, transfered the forms and modules back then saved it back to .xlsm and now the buttons call the macros and no run time errors occur anymore.

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.

Allow for users to change sheet names, VSTO

This is similar to my question here, but my workaround dosn't help for these instances and the cause is more defined. I have a workbook level customization that will add sheets from a folder where it retains the name of the sheet that was copied in. This works well and doesn't cause any problems unless the user renames the sheet. If the user renames the sheet, saves the workbook, and then come backs to it, then tries to do any action which references the workbook or worksheet they get the error:
Microsoft.VisualStudio.Tools.Applications.Runtime.ControlNotFoundException:
This document might not function as expected because the following control is missing:
Sheet5. Data that relies on this control will not be automatically displayed or updated,
and other custom functionality will not be available. Contact your administrator or the
author of this document for further assistance. --->
System.Runtime.InteropServices.COMException: Programmatic access to the Microsoft Office
Visual Basic for Applications project system could not be enabled. If Microsoft Office
Word or Microsoft Office Excel is running, it can prevent programmatic access from being
enabled. Exit Word or Excel before opening or creating your project.
Now closing and opening the Excel doesn't help, moving the sheet to a new position doesn't help, and deleting the offending sheet doesn't help if more than 1 sheet has been added. The base template has 6 sheets, and I've tried adding the sheets to the middle (where we would want them) and at the end (just for kicks). This doesn't seem to help. If you delete all the added sheets, save the workbook, and open it again, then functionality is restored. The sheets that are added are not named sheets in the VSTO, but are referenced mostly by number or name (there are 6 sheets to start with, so if they get added after 3 they would have an index of 4 to (ThisWorkBook.Sheets.Count-3), or if Sheet.Name is not any of the names from the original 6 (which they would never be, and I have scripted a prevention for).
My problem is that I really need to be able to allow the users to add a generic report sheet if the ones I've designed won't work. This means I need to be able to add a sheet, allow them to rename it, save it and come back to it. This error doesn't happen until the sheet has been saved, closed, and re-opened if that helps anyone. I also can not replicate this on any machine with VS2012 installed, even if the project was never worked on in that computer (VSTO just installed). Though all those VS2012 machines were used for other Excel VSTO projects. I can however replicate it on XP-Win8 and Excel Office '07-365.
**So my distilled question: is there a way to allow a user to add sheets and rename them (outside of the VSTO preferably), without running into a Runtime.ControlNotFoundException?
Thanks.
Basically, you need to re-initialize the sheets as VSTO objects everytime the workbook is re-opened. Since these sheets aren't hard-coded into your solution, VSTO has no way of recognizing them unless your code looks for such things and "re-connects" them at run-time. This is explained in the VSTO documentation:
http://msdn.microsoft.com/en-us/library/cc442981.aspx
But I believe you require an add-in in order to do this for sheets. I don't think it can be incorporated into the workbook's "code behind".

How to check if code that is run belongs to active sheet

I have written some code for an excel spreadsheet. This sub has been added to the ribbon using the standard customizations found in File -> Options -> Customize Ribbon. What is funny though, is that the customization will run the Sub on the EXACT EXCEL FILE that the Sub is written in, i.e. the original.
My users copy the excel file and make adjustments and then run the sub (by clicking the customized button in the ribbon), which then opens the ORIGINAL template file and runs the code. This actually works fine - and I like it because it runs the TEMPLATE code on the ACTIVE sheet. But the only problem is it is also OPENING the original template file. How do I get it to close? Remember it is not the ActiveSheet anymore.
One possible answer is to check if the template is open, then close it (using a static reference), but I would prefer something a little more elegant than that.
You should be able to find the template/original using ThisWorkbook.
Also, if you deliver your workbook's code as a .xlam instead of .xlsm, it will be opened, but there won't be a window for it, so users won't see it.
Not sure if you're using this already, but you might also want to use "for this workbook only" when adding ribbon buttons for a .xlam . (Delivering as .xlam allows you to install buttons from a workbook into the ribbon, without relying on file path and without needing your users to also install buttons (they install .xlam instead).)