Pass Argument from VBS to VBA

后端 未结 2 1735
别跟我提以往
别跟我提以往 2020-12-18 04:24

I try to call a VBA subroutine from VBS with passing a string variable from VBS to VBA, but can\'t find the appropiate syntax:

\'VBS:
\'---------------------         


        
相关标签:
2条回答
  • 2020-12-18 05:01

    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.

    0 讨论(0)
  • 2020-12-18 05:04

    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

    0 讨论(0)
提交回复
热议问题