Executing a function when the name is in a string

G

Gary Varga

I have a function name in a string and would like to
execute the function using the string e.g.

'A number of functions are defined
Function X() as String
X = "Called"
End Function

....

'Get the function name
Dim function_name as String
function_name = "X"

....

'Execute the function
'NOTE: The crux is that I need the equivalent of the line
below
Call function_name

Many thanks,
Gary
 
C

Chirag

In Outlook, you have to use the native VBA CallByName() function to call the
macro if it resides in ThisOutlookSession:

VBA.CallByName ThisOutlookSession, function_name, VbMethod

If the macro resides in some other module, call it from the command button.
Use the following macro in that case:

---
Sub CallMacro(ByVal MacroName As String)
Dim CBP As CommandBarPopup
Dim CBB As CommandBarButton

Set CBP =
Application.ActiveExplorer.CommandBars.ActiveMenuBar.Controls(1)
Set CBB = CBP.Controls.Add(Type:=msoControlButton, _
Temporary:=True)
CBB.OnAction = MacroName
CBB.Execute
CBB.Delete
End Sub
---

- Chirag

PowerShow - View multiple shows simultaneously
http://officerone.tripod.com/powershow/powershow.html
 
C

Chris Jensen [MSFT]

Hello Gary,

If you are using the word "function" in the sense of an Excel function,
such as the name of one of the many functions in the Excel Solver add-in,
you can replace one function's name with another using the =SUBSTITUTE(...)
function. Check the Excel Help topic on Substitute.

If you are using the word "function" in the sense of a macro you need to
use the Select...Case VBA method. An example would be

Select "Function_Name"
Case "MyFunction1"
Result = MyFunction1(argument1, argument2, ...)
Case "MyFunction2"
Result = MyFunction2(argument_a, argument_b,...)
...
MsgBox"Result is " & Result.
--------------------
From: "Gary Varga" <[email protected]>
Subject: Executing a function when the name is in a string
Date: Mon, 4 Aug 2003 02:10:35 -0700
I have a function name in a string and would like to
execute the function using the string e.g.

'A number of functions are defined
Function X() as String
X = "Called"
End Function

...

'Get the function name
Dim function_name as String
function_name = "X"

...

'Execute the function
'NOTE: The crux is that I need the equivalent of the line
below
Call function_name

Many thanks,
Gary

Regards,
Chris Jensen[MSFT]

This posting is provided “AS IS” with no warranties, and confers no rights.

“Microsoft Security Announcement: Have you installed the patch for
Microsoft Security Bulletin MS03-026?  If not Microsoft strongly advises
you to review the information at the following link regarding Microsoft
Security Bulletin MS03-026
http://www.microsoft.com/security/security_bulletins/ms03-026.asp and/or to
visit Windows Update at http://windowsupdate.microsoft.com to install the
patch. Running the SCAN program from the Windows Update site will help to
insure you are current with all security patches, not just MS03-026.”
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top