execute a variable as a function/sub procedure (like eval function in java(script)) 
Author Message
 execute a variable as a function/sub procedure (like eval function in java(script))

Hello

Is it possible in VBA to execute a string/variable if there is a function
with that name?

for example:
Function DisplayMsg(aMsg as string) as string
    MsgBox aMsg
    DisplayMsg = "Yes"
End Sub

execute the function:
dim x as string
dim y as string
x = "DisplayMsg("This is a message")"
y = x

I just realise.... this could be possible when you're using the Word.Basic
procedure, but how long is this function supported by Microsoft in VBA??

Regards,

Marco Dissel



Tue, 04 May 2004 06:02:37 GMT  
 execute a variable as a function/sub procedure (like eval function in java(script))
Hi Marco,

Here's a routine developed by former Word MVP Will Rickards. I've never used
it but perhaps it will be of help to you.

Public Sub EVAL(ByVal strToEvaluate)

Dim comNew As VBIDE.VBComponent

   With ThisDocument.VBProject.VBComponents
      Set comNew = .Add(vbext_ct_StdModule)
      With comNew.CodeModule
         .InsertLines .CountOfLines + 1, "Public Sub EVAL_RUNTIME()" & _
             strToEvaluate & vbcr & "End Sub"
      End With
   End With

   Application.Run "EVAL_RUNTIME"

   ThisDocument.VBProject.VBComponents.Remove comNew

End Sub

--
Bill Coan
Microsoft Word MVP

http://www.wordsite.com

Quote:




Quote:
> Hello

> Is it possible in VBA to execute a string/variable if there is a function
> with that name?

> for example:
> Function DisplayMsg(aMsg as string) as string
>     MsgBox aMsg
>     DisplayMsg = "Yes"
> End Sub

> execute the function:
> dim x as string
> dim y as string
> x = "DisplayMsg("This is a message")"
> y = x

> I just realise.... this could be possible when you're using the Word.Basic
> procedure, but how long is this function supported by Microsoft in VBA??

> Regards,

> Marco Dissel



Tue, 04 May 2004 07:58:52 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Calling a sub or function using a variable through another sub or function

2. Help - sub procedure and a function procedure

3. Help - sub procedure and a function procedure

4. Executing code that is stored in a table, Eval() function

5. Eval & Execute functions

6. Returning sub or function name within a specific sub or function

7. Local function/sub inside function/sub

8. Eval function using a custom typed variable

9. eval function? how to call dynamic function name

10. Does VBScript has a function similar to JScript function eval

11. How can I Call a Sub or Function Procedure with a Variable as Function or Procedure Name. Please Help

12. eval to execute - sub - Please Help.

 

 
Powered by phpBB® Forum Software