Calling a Sub or Function contained in a module us

2020-03-01 17:28发布

It is easy to call a function inside a classModule using CallByName How about functions inside standard module?

''#inside class module
''#classModule name: clsExample
  Function classFunc1()
     MsgBox "I'm class module 1"
  End Function
''# 
''#inside standard module
''#Module name: module1
  Function Func1()
     MsgBox "I'm standard module 1"
  End Function
''#
''# The main sub
Sub Main()
''# to call function inside class module
dim clsObj as New clsExample
Call CallByName(clsObj,"ClassFunc1")

''# here's the question... how to call a function inside a standard module
''# how to declare the object "stdObj" in reference to module1?
Call CallByName(stdObj,"Func1") ''# is this correct?

End Sub

标签: vba
5条回答
家丑人穷心不美
2楼-- · 2020-03-01 17:35

Modules in VB6 and VBA are something like static classes, but unfortunately VB doesn't accept Module1 as an object. You can write Module1.Func1 like C.Func1 (C being an instance of some Class1), but this is obviously done by the Compiler, not at runtime.

Idea: Convert the Module1 to a class, Create a "Public Module1 as Module1" in your Startup-module and "Set Module1 = New Module1" in your "Sub Main".

查看更多
别忘想泡老子
3楼-- · 2020-03-01 17:35

Although it is an old question and OP asked for CallByName in a standard module, the correct pieces of advice are scattered through answers and comments, and some may not be that accurate, at least in 2020. As SlowLearner stated, Application.run DOES return a Variant, and in that way both branchs below are equivalent, except by handling errors, as commented around Horowitz's answer:

Dim LoadEnumAndDataFrom as Variant
'FunctionName returns a Variant Array
if fCallByName then
    LoadEnumAndDataFrom = CallByName(ClassObj, "FunctionNameAtClass", VbMethod)
else
    'After moving back function for a standard module
    LoadEnumAndDataFrom = Application.Run("StandardModuleName" & "." & "FunctionNameAtStandard")
endif

I actually just did this above and had no errors at all, tested in Word, Excel and Access, and both return the same Array.

Unfortunately, there is an exception: Outlook's object Model is too protected and it does not have the Run method.

查看更多
男人必须洒脱
4楼-- · 2020-03-01 17:38

I think jtolle's response addressed the question best - the small reference to Application.Run may be the answer. The questioner doesn't want to use simply func1 or Module1.func1 - the reason one would want to use CallByName in the first place is that the desired function.sub name is not known at compile time. In this case, Application.Run does work, e.g.:

Dim ModuleName As String
Dim FuncName As String
Module1Name = "Module1"
FuncName = "func1"
Application.Run ModuleName & "." & FuncName

You can also prepend the Project Name before the ModuleName and add another period ".". Unfortunately, Application.Run does not return any values, so while you can call a function, you won't get its return value.

查看更多
Deceive 欺骗
5楼-- · 2020-03-01 17:42

Unfortunately it is not possible to prepend the ProjectName before the ModuleName and add another period "." In MS Word this throws a runtime error 438. The call is restricted to the use of simply ModuleName.ProcName.

查看更多
We Are One
6楼-- · 2020-03-01 17:56

CallByName works only with class objects.

If your subroutine is in a standard module, you can do this:

Sub Main()
    Module1.Func1
End Sub

If it's a function, then you'll probably want to capture the return value; something like this:

Sub Main()
    Dim var
    var = Module1.Func1
End Sub
查看更多
登录 后发表回答