Pass Argument from VBS to VBA

戏子无情 提交于 2019-11-30 22:39:55

You need to specify parameters in your VBA Sub and use them as you would do if using it from VBA normally.

For example, I tried the following VBScript

dim wd: set wd = GetObject(,"Word.Application")
wd.Visible = true
wd.run "test", "an argument"

and the VBA

Sub Test(t As String)
    MsgBox t
End Sub

which worked successfully, generating a message box.

Addendum to @user69820 answer, if arguments are VBScript variables, they need to be cast as appropriate type before calling the subroutine:

This does not work:

dim argumentVariable
argumentVariable = "an argument"
wd.run "test", argumentVariable

This does:

dim argumentVariable
argumentVariable = "an argument"
wd.run "test", CStr(argumentVariable)

Tested on Excel 2010, Win7SP1 x64

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!