Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Sub or Function not defined" when trying to run a VBA script in Outlook

Tags:

vba

outlook

As a first step in creating a VBA script to resize a currently selected image to 100% x 100%, I'm trying to reproduce the example in http://msdn.microsoft.com/en-us/library/ee814736(v=office.14).aspx. The macro is very simple:

Sub Test()
   MsgBox ("Hello world")
End Sub

The VBA script was simply created in "Project1" which opens by default when one presses Alt+F11. However, I keep getting the error "Sub or Function not defined" when trying to run the VBA script (Figures 1 and 2).

How can I make the VBA script 'accessible' to Outlook?

Running the "Test" macro in Microsoft Outlook Figure 1 Running the "Test" macro in Microsoft Outlook

enter image description here Figure 2 "Sub or Function not defined" error, with module tree in the background

like image 451
Kurt Peek Avatar asked May 18 '14 12:05

Kurt Peek


People also ask

How do I enable VBA in Outlook?

Outlook. On the Developer tab, click Visual Basic. On the Tools menu, click Options. On the Other tab, click Advanced Options, and then select the Show Developer tab in the Ribbon check box.

How do I fix subscript out of range error in VBA?

If we run this code using the F5 key or manually, we will get Run time error '9': “Subscript out of Range.” To fix this issue, we need to assign the length of an array by using the “ReDim” word. This code does not give any errors.

What does sub not defined mean?

“Sub or Function not Defined” is a compile error that occurs when VBA cannot find a procedure or other reference by name. A typo is the most common cause of this message.

How do you define a function in VBA?

The most common way to define a function in VBA is by using the Function keyword, followed by a unique function name and it may or may not carry a list of parameters and a statement with End Function keyword, which indicates the end of the function.


2 Answers

I solved the problem by following the instructions on msdn.microsoft.com more closely. There, it is stated that one must create the new macro by selecting Developer -> Macros, typing a new macro name, and clicking "Create". Creating the macro in this way, I was able to run it (see message box below).

enter image description here

like image 138
Kurt Peek Avatar answered Oct 25 '22 12:10

Kurt Peek


I need to add that, if the Module name and the sub name is the same you have such issue. Consider change the Module name to mod_Test instead of "Test" which is the same as the sub.

like image 36
Kangqiao Zhao Avatar answered Oct 25 '22 12:10

Kangqiao Zhao