【发布时间】:2020-08-23 17:03:26
【问题描述】:
我正在尝试编写一个允许可选参数具有默认值的 VBA 子程序。我尝试了来自 Microsoft Docs - Optional Parameters (Visual Basic) 的示例代码,但它导致 sub 未显示在可用 sub 列表中(即来自 View Macros)。
Sub notify(ByVal company As String, Optional ByVal office As String = "QJZ")
If office = "QJZ" Then
Debug.WriteLine("office not supplied -- using Headquarters")
office = "Headquarters"
End If
' Insert code to notify headquarters or specified office.
End Sub
我尝试过但无法出现的子声明是:
Sub HighlightByFont( _
Optional ByVal highlightFont As Variant = "", _
Optional ByVal highlightColor As Variant = "", _
Optional ByVal highlightText As Variant = "", _
Optional ByVal matchWildcards As Variant = False, _
Optional ByVal useGUI As Variant = False, _
Optional ByVal highlightBold As Variant = False, _
Optional ByVal highlightItalic As Variant = False)
现在,我不得不通过IsMissing logic满足以下条件:
Sub HighlightByFont( _
Optional ByVal highlightFont As Variant, _
Optional ByVal highlightColor As Variant, _
Optional ByVal highlightText As Variant, _
Optional ByVal matchWildcards As Variant, _
Optional ByVal useGUI As Variant, _
Optional ByVal highlightBold As Variant, _
Optional ByVal highlightItalic As Variant)
是否(仍然†)可能,如果可能,如何:
- 要设置参数声明?
- 让它出现在 View Macros 列表中?
环境:
- Word 2016 x64
- Windows 10
† 所有参考资料,包括与 VBA 可选参数相关的 SO 答案均来自 2015 年。
【问题讨论】:
-
在 View Macros 中只能看到没有参数的 public subs。创建一个没有参数的 sub 将调用它(一旦在 VBA 中正确编写)。
-
我的当前版本有很多参数可以在查看宏列表中看到(每个 OP):Sub HighlightByFont(Optional ByVal highlightFont As Variant, ...)
标签: vba ms-word default-value optional-parameters word-2016