VBA Macro changing macro - vba

Is it possible to create a macro in MS Office (in this case Word) that will change other macro code? I was trying to find information but no results.
I have a doc which works as a template. Content of template is changed and then saved to another file. However it is important to have current date in it. It cannot be self-updated. Those docs go to folder of people and it is important to know when they get the document, so it must be simply data (or something that does not update).
I was thinking about an on-start event macro that would input current date and on exit it would ask "Do you want self-update functionality" Yes / No
If Yes, delete that event. However I have no idea if it is possible. If it is I still don't know how to search for it.

No this is not possible. In VBA, unlike some lower level languages when you define an event you can not disable it, even using other VBA code.
In C# or VB.NET, Java or C++ you can disable an event by un-wiring it from the handler, but this is not possible in VBA.
Maybe if you be more clear on what you need I can give you a better answer.

Related

How to run section of VBA code in Outlook only once?

I would like my VBA code in Outlook's "ThisOutlookSession" module to display a message box on the first occurrence of an event only, even if the event occurs in subsequent sessions. All the ways I have found online to achieve this are excel specific and involve changing a value of a cell and then checking this value, is there an equivalent in outlook or another way to achieve this?
I believe this could be achieved using the VBIDE library to change a variable value in the VBA code, however I would prefer not to do this.

Disable Macros from Safe Mode

I wrote a macro that now is causing issues with MS Word. I can only open the document in safe mode and cannot figure out how to delete the macro from safe mode. I don't have access to any of the features in safe mode and the macro isn't even showing up. I tried changing the Trust Center settings but none of those options make a difference. Unfortunately I was very dumb and made it a macro on all documents so I can't open any documents without going into safe mode. help!
To be more specific, I believe the code that broke my MS Word was a OnError GoTo ErrorHandler where I tell it to skip to the next if there is an error. I am working on converting equations to a professional format and some of them don't have the right syntax for the conversion so for now I wanted it to ignore those equations and convert the next equation. That is the only change I have made recently that could be the problem.
An additional edit: the macro is coded to run on open. It was running fine before I added the ErrorHandler. I'm sure this is also a huge part of the problem, but a simple solution may not be the right fix since it automatically runs when I open the document. I can't access anything in safe mode.
If you are talking about the "Normal" document in the project explorer, resetting it is easy.
Simply navigate to %AppData%\Microsoft\Templates and either delete or rename the Normal.docm file. Once you reopen Word it will regenerate a brand new file automatically - one that contains no code.
You created an AutoOpen macro which only fires when a document is opened. Presumably this macro is stored in your Normal template.
When you first start Word the macro should not run as you are not opening a document. If Word doesn’t automatically create a new blank document you can safely do so. You will then be able to access and edit the macro you created.

Word VBA: Make macro easy to run

I've made a form-letter document with a macro that performs the mail merge. I don't want the user to have run it from the menus, and I want this to be portable. If there's a way for a button to appear on each user's ribbon or quick command menu, I'm not familiar with it.
So I put a button in the document itself. Unfortunately, every form-letter created has the same button in it. I suppose I could write the code to delete every one, but I think that would be slow.
Is there a way to assign a shortcut key to an existing macro, and have it reside in the document?
I had to implement something pretty similar to what you were referring to some 10 staff. My solution (by no means as portable as desired) was to export the macros and forms from my Normal template to the other users, I coupled this with Ribbon customization and it worked well. Unfortunately, when a change was needed, I had to trudge over to everyone's machine individually.
I would suggest you stick with your solution of deleting button after the merge is complete. Here's some code to help with that:
Sub DeleteCommandButton()
For Each o In ActiveDocument.InlineShapes
If o.OLEFormat.Object.Name = "CommandButton1" Then
o.Delete
End If
Next
End Sub
Good luck, hopefully this helps.

Prevent Word macro in Normal.dotm for some templates

I have a Normal.dotm file that contains an AutoNew macro.
This macro is automatically executed each time a new document is created using any other template.
Is there any way I can prevent this automatic behavior for a specific template?
I have a Word VSTO add-in running, so I can hook into Word's events, but so far I havn't found a way to prevent this.
I do know that I can prevent macro execution when using templates programmatically, for example like this:
' Disable auto-macros before opening document
wordApplication.WordBasic.DisableAutoMacros(1)
' Open document
newWordDocument = wordApplication.Documents.Open(template.FullName, ConfirmConversions:=False, [ReadOnly]:=True, AddToRecentFiles:=False, Revert:=True)
' Re-enable auto-macros
wordApplication.WordBasic.DisableAutoMacros(0)
But this solution doesn't work when the user uses a Word template from Windows explorer or the Open-dialog in Word, since in those cases I can't execute code before it's too late already.
Or can I?
I hope someone has a trick for me :-)
-
Edit: While trying different solutions, I discovered something that might help others in similar situations, though unfortunately it doesn't help me.
It seems that if a template contains a module containing an AutoNew (or AutoOpen for that matter), that local macro is executed instead of the one in Normal.dotm.
Example:
Normal.dotm contains the following macro:
Sub AutoNew()
MsgBox "Normal.dotm"
End Sub
Test.dotm contains the following macro:
Sub AutoNew()
MsgBox "Test.dotm"
End Sub
When executing Test.dotm the message "Test.dotm" is displayed, while the message "Normal.dotm" is not displayed.
If the AutoNew macro is removed from the Test.dotm template, the message "Normal.dotm" is indeed displayed.
So it is possible to easily override the auto-macros.
The local versions of AutoNew and AutoOpen can even be empty subs that do nothing. It still works.
This is not possible in my case though, since the template I use is generated by code, and cannot contain macros (because adding macros to templates programmatically requires the user to manually activate the option "Trust access to the VBA project object model", and that's something I cannot ask my customers to do for all users. It's also a security risk.)
Based on the workaround described in the "Edit" part of the question - providing a template with "empty" Auto-macros - it's possible to use the Open XML SDK to create a template and add the VBA project to it in order to provide this functionality. This approach avoids the user needing to allow access to the VBA project on his installation. The only "macro security" that could be triggered is that for not allowing macros to run. But since the client uses macros, anyway, this should not be a major obstacle.
The simplest method is to create as much of the basic template as possible in the Word UI and use this as a starting point.
Since you're unfamiliar with the Open XML SDK, the next step would be to create one (or more) templates in the Word UI using the basic template as the starting point, saving under a different file name.
You can then use Open XML SDK Productivity Tool to see the code required to generate any one of these files, as well as, using the Compare tool, the code for converting the basic template to the derived version. This should give you a decent start with the SDK and it's object model. Once you get a feel for how the Open XML SDK works, if you're familiar with Word's object model, using that provided by the SDK is relatively straight-forward as an effort was made to make it correspond as closely as possible to the "COM" object model.
The VBA project can be added later, but you can also include it in the basic template. That would be the simplest approach.
Include this "starting point" basic template as part of your solution, installing it as part of the solution.
Within the AutoNew macro you can check the AttachedTemplate property. Only if it is a template where you want to apply the cleaning you can execute the respective macros.
Sub AutoNew()
If ActiveDocument.AttachedTemplate <> "Normal.dotm" Then
Exit Sub
End If
' rest of the macro
End Sub
If you don't control the Normal.dotm you can put an empty AutoNew macro in your own templates. As Word only executes the auto macro in the closest context, the macro in the Normal.dotm file would not be executed.
If you don't control the other templates either, you can tell your users to hold down the SHIFT key while creating a document. This prevents the execution of the auto macro.
Probably it is best, however, if you ask the owner of the other system to find another solution that does not rely on polluting the Normal.dotm file.

How to update an Access VBA app with 30 forms?

I need to update an Access VBA app with around 30 forms in it.
I have to amend a screen that seems to have been set up right at the start of the app, it uses a lot of SQL tables. Is there an way of finding my way to the start of the code?
I come from a procedural coding background and I am unused to code that doesn't have a start and an end; I also know a bit of VB, some ASP, some .Net and general computing.
When something "automagically" happens upon opening an Access database, it is almost always because
A "startup form" has been specified. (In Access_2010 that's done in File > Options > Current Database > Display Form.) ...or...
The database has a Macro named AutoExec which is automatically run when the database is opened (unless you bypass it by holding the [Shift] key down while opening).
In addition to #Gord's answer, there's a few things you need to know. I'm going to give you the quick & dirty version.
First, there's 2 types of code in Access. VBA & macros. Sometimes what's called a macro, is really VBA.
In Access, a macro is a set of instructions to do something to the database. It's very limited in what it can do. These are often used by novices who don't know how to program in VBA.
VBA is the real powerhouse behind the scenes. It can do everything a macro can do, but a whole lot more.
Access uses an Event-Driven / Object-Oriented (at least close enough for this discussion) interface. Do a Google search on those meanings. But very quickly, the listbox on a form is an object. It has properties (like width), methods (add an item), and events (click on an item).
To see the code, for macros look to to your navigation window to your left. For VBA (modules), look to the same window, or just press Alt-F11. VBA can be used standalone in a module, or behind the scenes of a form or report.
Once you get the hang of it, you'll find Access to be a handy RAD tool for small projects.
Good luck.
It appears that you already have found the form that opens when the app starts (if not, check out Gord Thompson's answer).
The first things that happen when an Access Form opens (the "start of the code", as you called it) are the Load and Open events.
If there is any code in this form that is connected to these events, then it's in the Form_Load() and Form_Open() functions in the code of the form.