Calling a Sub or Function contained in a module using “CallByName” in VB/VBA

前端 未结 5 876
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-04 18:25

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

\'\'#inside class module
\'\'#classModule nam         


        
5条回答
  •  旧巷少年郎
    2021-01-04 18:59

    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.

提交回复
热议问题