JavaScript to call a VBA routine with parameters

后端 未结 1 793
梦谈多话
梦谈多话 2021-01-22 17:34
  1. I need to pass arguments to a Excel VBA code from JavaScript of HTA.

    1. I can successfully call VBA function, but unable to pass string arguments correctly.<
相关标签:
1条回答
  • 2021-01-22 17:50

    I'm thinking you want:

    objWb.Application.Run('testing_excel_web.xls!subTest("' + strName + '")');
    

    This way, the value of the variable strName is concatenated to the command you are attempting to run.

    I know nothing about this calling of a VBA function, so I'm not sure if you need the " around the strName like I provided or not.

    In addition, to be safe, in case your strName value contains ", you should use this:

    objWb.Application.Run('testing_excel_web.xls!subTest("' + strName.replace(/"/g, "\"") + '")');
    

    Hopefully with this, the value of strName could be

    The word "testing" here
    or
    "Here's a quote"
    

    and it will still work.

    The point is that if the string contains ", the Javascript would/could fail. If it would absolutely never contain ", then forget about it. But I think it's needed since any " in the value of strName will break the passing of it as a parameter.

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