AutoExec in Excel VBA - vba

I have a Word macro (.dotm) in the Word startup folder. When it gets loaded, it automagically calls
Sub Autoexec
Call myMacro
End Sub
as explained on MSDN.
Is there a way to achieve the same effect with an Excel AddIn (.xlam) stored in the Excel startup folder?

You should use Workbook_Open as Maddy suggested in his comment, that is the recommended mtehod. Or you put in a normal module a macro which is named a little bit differently than the one in word. This also works in an AddIn
Sub auto_open()
MsgBox "Welcome"
End Sub
Also have a look for the naming convention here: https://msdn.microsoft.com/en-us/vba/excel-vba/articles/workbook-runautomacros-method-excel

Related

Document_Open does not work in Word 2007 template

For employees to work, I need to put some functionality into the Document_Open macro. I want to put this macro in a template (.dot) and place it on the network. Office 2007 is installed on most machines, and I'm testing on it.
I create a document, insert the macro code, save it as a template (.dot). I use the following macro code as a test:
Sub Document_Open()
MsgBox "ABC"
End Sub
Next, I create a document (.doc) and change the document template on my created template. I allow the execution of macros, the macro work. I save and close the document, when I reopen the document and enable macros, our macro does not work. What the problem is I can’t understand. Thank!

With Word VBA, can I divide the code module of a Form into two code modules?

In my Word file, I have a form called MyNiceForm. There is a code module associated with it, of course, which contains the _Click() routines. (Maybe those are called event handlers, but that doesn't affect my question.)
I have so many _Click() routines that I'd like to split them into a separate code module, but I can't quite figure out how to do that. I'm somewhat sure that VBA does not allow me to put a _Click() routine in a regular code module (a 'non-Form' code module).
No, the Click routines have to stay in the UserForm code module. In the strictest sense, the UserForm is a class within the document "container". The UserForm designer and the code module are both part of the class / UserForm object. All the buttons' "Click" routines depend on this link.
(This is a similar situation with the Document, itself, and the ThisDocument module in a document's VBA project. The Document is a class/object and ThisDocument the code module. ActiveX controls (which are UserForm controls) on the document surface also rely on the link between the visual part and the code.)
What you can do is move the code inside the Click procedures to another (plain) module. Leave just one line of code in the procedure that "calls" the code in that module. If the code works with the UserForm and/or controls on it you need to pass these objects as arguments.
That could look like this:
'Code in the UserForm module
Private Sub CheckBox1_Click()
CheckBoxClick Me, Me.CheckBox1
End Sub
'Code in a regular module
Sub StartUserForm()
Dim frm As frmTest
Set frm = New frmTest
frm.Show
End Sub
Sub CheckBoxClick(frm As frmTest, ck As MSForms.CheckBox)
Debug.Print frm.NAME, ck.Caption, ck.value
frm.CheckBox2.value = Not ck.value
End Sub

Macro not launching on workbook open

I have a workbook that I've been using an sub auto_open macro on and it's been fine.
However I am now trying to open it from another workbook and the auto_open macro doesn't work. It just opens and no macros run.
If I add a Workbook_open in "This workbook" to run the same macros it works fine. However if I run the workbook normally (outside of the link) it now opens and doesn't run any macros.
Weirdest thing is if I allow both auto_open and workbook_open it runs twice which is not what I want obviously.
Private Sub Workbook_Open()
StartMacro
End Sub
Public Sub Auto_Open()
StartMacro
End Sub
My ideal would be to have either as long as it will open when launched normally or via a link in a workbook.
Any ideas why I'm getting these issues?
auto_open subs needs to be in a module, not in an Excel object (like a sheet's code, nor ThisWorkbook). Here is further reference about auto-startup options in Excel.

Application.OnTime in Add-In Workbook_Open

I've built a custom Excel add-in and I'm currently trying to figure out a way to prompt users via VBA when new versions of the add-in are available.
I tried just using the workbook_open event to check for the latest version and then prompt the user with a userform, but I discovered that when Excel loads an add-in that trigger a userform, Excel stops loading the workbook the user actually tried to open and reloads the add-in. So while the userform works like I wanted, the user gets a blank (read no sheets) Excel shell with a loaded add-in.
So I considered using Application.OnTime to postpone the VBA until after the add-in and target file were both open. I got the impression both here and here that this is possible, but I am only able to make it work in an .xlsm file and not a .xlam file.
Here's the code I'm testing with:
Sub Workbook_Open()
Application.OnTime Now() + TimeValue("00:00:05"), "Test_Addin.xlam!Versioning.Notify_User"
End Sub
And in a regular code module:
Sub Notify_User()
MsgBox "Hello"
End Sub
So, my question: Am I doing something wrong here?
I'm wondering if there's something about how an add-in is loaded/designed that keeps it from allowing this type of action to be performed.
Alternatively, is there a different way to do this that you can think of?
I read an interesting blog post (and comments) by Dick Kusleika on this topic, but it sounded like some people put a version check in each sub procedure... I have a lot of procedures, so this doesn't sound like a good alternative, although I may have to resort to it.
Well, time is of the essence so I resorted to the least desirable option: a check at the beginning of each procedure.
To the interested parties, here's how I did it:
Somewhere towards the beginning of each sub procedure I put this line of code:
If Version_Check Then Call Notify_User
And in my Versioning module I put this function and procedure:
Function Version_Check() As Boolean
Installed = FileDateTime(ThisWorkbook.FullName)
Available = FileDateTime("\\NetworkDrive\Test_AddIn.xlam")
If Installed < Available Then Version_Check = True
End Function
Sub Notify_User()
Update_Check.Show
End Sub
So each time a procedure is run this code checks for a version on our corporate network with a modified datetime greater than the datetime of the installed add-in.
I'm still very open to alternative ways of checking for new versions so feel free to post an answer.

Calling a function within ThisDocument in Microsoft Word (VB.Net/VBA)

I have a VB.NET program that adds code to the ThisDocument module in MS Word in the following way:
wrdDoc.VBProject.VBComponents("ThisDocument").CodeModule.AddFromString(sCode)
Which creates the following sub:
Public Sub Example_Click()
Msgbox "Working"
End Sub
I used to call this via the click event of a command button however it would be desirable call it from a ribbon button created as part of a COM Add-In.
I have been able to call global macros successfully in the Add-In by adding the following code to the button's click sub,.
Globals.TestWordApp.Application.Run("Macro1")
All attempts to run code contained within ThisDocument have failed however.
Any help would be greatly appreciated.
Application.Run runs macros.
Macros exist in modules, and ThisDocument is a class module.
Try creating a new module or use an existing module instead of ThisDocument.