VBA 如何确定正在使用的应用程序

VBA how to determine which application is being used

我在 MS Access 和 MS Excel 中使用了一个 VBA 函数。当在 MS Excel 中使用时,我使用 Application.Volatile 但是当它被放置或用于 MS Access 时,它不会编译。有没有一种方法可以使这条线在放置在 MS Access 中时可以互换,而不必删除它?

谢谢, 弗雷德

您可以询问应用的名称:

If Application.Name = "Microsoft Access" then
   'Do Nothing ......Or whatever you need to do.

ElseIf Application.Name = "Microsoft Excel" then
    Application.Run "Application.Volatile"
End If

来自帮助。

Visual Basic for Applications 参考

CallByName 函数

执行一个对象的方法,或设置或returns一个对象的属性。

语法

CallByName(object, procname, calltype,[args()])

CallByName 函数语法具有这些命名参数:

Part Description 
object Required; Variant (Object). The name of the object on which the function will be executed. 
procname Required; Variant (String). A string expression containing the name of a property or method of the object. 
calltype Required; Constant. A constant of type vbCallType representing the type of procedure being called. 
args() Optional: Variant (Array). 

备注

CallByName 函数用于获取或设置 属性,或使用字符串名称在 运行 时调用方法。

在下面的例子中,第一行使用CallByName设置文本框的MousePointer属性,第二行获取MousePointer的值属性,第三行调用Move方法移动文本框:

CallByName Text1, "MousePointer", vbLet, vbCrosshair
Result = CallByName (Text1, "MousePointer", vbGet)
CallByName Text1, "Move", vbMethod, 100, 100

在此处将反馈发送至 MSDN.Look 以获取 MSDN 在线资源。