开发者

VBA cannot find my macro when it has parameters

开发者 https://www.devze.com 2023-02-14 19:36 出处:网络
I am trying to write a macro that will be attached to a series of buttons in an Office 2010 backstage tab.Depending on the button clicked the Macro should be called with different parameters.

I am trying to write a macro that will be attached to a series of buttons in an Office 2010 backstage tab. Depending on the button clicked the Macro should be called with different parameters.

The issue I am having is that if the Macro is defined as having parameters then VBA will display the "Macros" dialog box, with no Macros listed. Removing the parameters from the declaration will allow the macro to run, but it needs the Macros to make sense.

The VBA being used is below:

Sub NewDocs(docType As String, docTemplate As S开发者_如何学Ctring)
   Dim sMyShellCommand As String
   sMyShellCommand = "C:\NewDocs.exe " & docType & docTemplate
   WordBasic.Shell (sMyShellCommand)
End Sub

Any ideas


If I've got your question right....

Because there is no place in the Macros dialog box for entering parameters, the macros with parameters are simply not shown.

If you want to make them visible in the dialog box, you can enumerate those functions you need (and I hope it is not a big number).

For example,

Sub NewDocs1
  Dim docType As String
  Dim docTemplate As String

  docType = "the type you want"
  docTemplate = "the template you want"

  NewDocs docType, docTemplate
End Sub

In addition, as you said in the question, you wanted the macro to run when buttons were pressed. Then there is no need to make the macro visible in the dialog box (which saves your labor). Simply associate it with the button with correct parameters.


You can pass arguments from the Macro dialog. For example, if you have this macro

Sub myMacro(n As Long)
    MsgBox n
End Sub

To run it, enter

mymacro 1000

... and press the Run button.


You can't call functions or subs with parameters from UI components, just subs without parameters. The best solution is to create a parameter free sub for each button you need to associate with, and call the parameterized sub or function from inside each sub

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号