【问题标题】:VBA Sub optional parameters with default values具有默认值的 VBA Sub 可选参数
【发布时间】: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)

是否(仍然)可能,如果可能,如何:

  1. 要设置参数声明?
  2. 让它出现在 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


【解决方案1】:

可选值参数声明

是的,具有默认值的可选参数直到 Word 2016 仍然有效。VBA reference 声明:

arglist 参数具有以下语法和部分:

[ 可选] [ ByVal | ByRef ] [ ParamArray ] varname [ ( ) ] [ As type ] [ = defaultvalue ]

可选可选。 Keyword 表示不需要参数。如果使用,则 arglist 中的所有后续参数也必须是可选的,并使用 Optional 关键字声明。如果使用了ParamArray,则可选不能用于任何参数。

默认值 可选。任何常量或常量表达式。仅对可选参数有效。如果类型是Object,则显式默认值只能是Nothing

Sub OptParam_Test_1()
  'Shows in Macro list
End Sub

Sub OptParam_Test_2(param)
  'Does not show in Macro list
    Debug.Print (param) 'Output for OptParam_Test_2 "hello": hello
End Sub

Sub OptParam_Test_3(Optional param)
  'Shows in Macro list
    Debug.Print (param) 'Output: hello
End Sub

Sub OptParam_Test_4(Optional param As String = "hello")
  'Does not show in Macro list
    Debug.Print (param) 'Output: hello
End Sub

Sub OptParam_Test_5(Optional param As Variant = "hello again")
  'Does not show in Macro list
    Debug.Print (param) 'Output: hello again
End Sub

使用默认值声明参数时的意外界面行为是子将停止出现在宏列表中

这不应被解释为子有问题或无法使用。

  • 可以理解,这似乎是一个设计决定,需要从另一个子或即时窗口调用带有参数的子1,因此不会显示在宏列表中
  • 从逻辑上讲,只有/所有可选参数(并且没有默认值在宏列表中显示2 自调用以来,根据定义,不需要提供显式参数
  • 不一致,带有可选参数提供的默认值显示在宏列表中

调用选项

调用包含默认值参数的 subs 的两个选项是:

  1. 声明一个无参数调用子程序,它调用相关子程序。 此调用子程序将出现在宏列表中。
    Sub OptParam_Test_4(Optional param As String = "hello")
      'Does not show in Macro list
        Debug.Print (param) 'Output: hello
    End Sub

    Sub OptParam_Test_4_()
      'Shows in Macro list
      OptParam_Test_4
      'Output: hello
    End Sub

  1. 使用即时窗口1调用有问题的子

1开发者 - Visual Basic - 视图 - 即时窗口 (Ctrl+G)

2在与此主题相关的多个 SO 帖子上与各种 cmet 相反

【讨论】:

    【解决方案2】:

    您提供的链接是指向 VB Net 而不是 VBA 的帮助页面。 VBA 和 VB .Net 很相似,但有非常不同的用例。 VBA 是 Microsoft Office 应用程序使用的内置脚本语言。 VB Net 是一种完整的 .Net 语言,它起源于 VBA,但除非您编写特定的 VSTO 插件或应用程序,否则 Office 应用程序不会使用它。

    VBA 中的可选参数可以正常工作。您在上面提供的代码示例的 VBA 版本是。

    Sub notify(ByVal company As String, Optional ByVal office As String = "QJZ")
        If office = "QJZ" Then
            Debug.print "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)
    

    写成

    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 是更好的选择,因为它无法提供合理的默认值。

    【讨论】:

    • 我已经用换行符更新了示例代码(这是一个很好的提示,谢谢。)看起来 VB.net 和 VBA 代码几乎相同,除了 Debug.WriteLine 替换为 Debug .print
    猜你喜欢
    • 1970-01-01
    • 2013-04-25
    • 2021-09-24
    • 2016-06-21
    • 2021-07-05
    • 1970-01-01
    • 1970-01-01
    • 2014-12-18
    • 1970-01-01
    相关资源
    最近更新 更多