带有默认值的VBA Sub可选参数

问题描述 投票:2回答:2

我正在尝试编写一个VBA子程序,该子程序允许可选参数具有默认值。我尝试了来自Microsoft Docs - Optional Parameters (Visual Basic)的示例代码,但它导致该子项未显示在可用子项列表中(例如,来自“查看宏”)。

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. 要使其显示在“查看宏”列表中?

环境:

  • Word 2016 x64
  • Windows 10

所有参考,包括与VBA可选参数有关的SO答案,均来自2015年。

vba ms-word default-value optional-parameters word-2016
2个回答
3
投票

您提供的链接是VB Net而不是VBA的帮助页面。 VBA和VB .Net相似,但用例却大不相同。 VBA是Microsoft Office应用程序使用的内置脚本语言。 VB Net是一种全面的.Net语言,其源于VBA,但除非编写特定的VSTO加载项或应用程序,否则Office Application不会使用它。

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


0
投票

可选值参数声明

是,具有默认值的可选参数仍然可以在Word 2016之前使用。VBA reference指出:

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

[可选] [ByVal | ByRef] [ParamArray]变量名[()] [作为类型] [=默认值]

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

defaultvalue可选。任何常量或常量表达式。仅对Optional参数有效。如果类型是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

声明具有默认值的参数时的意外接口行为是子将停止出现在“宏”列表中

Note sub examples 1, 3 show. 2, 4, 5 do not.

不应将其解释为潜艇有问题或无法使用。

  • 理解上,似乎是一个设计决策,需要从另一个子目录或“即时”窗口1]中调用带有参数的子目录,因此不显示在“宏”列表中enter image description here
  • 逻辑上,只有/所有可选参数(且没有默认值
  • )的子程序,在宏列表中do show 2,因为根据定义,调用不需要明确的参数待提供
  • Inconsistently,带有可选参数的子项提供的默认值不]在宏列表中显示

    调用选项

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

  1. 声明一个无参数调用子,它调用有问题的子。 此调用子将出现在宏列表中。

    Sub OptParam_Test_4(可选参数As String =“ hello”)'不显示在宏列表中Debug.Print(param)'输出:你好结束子

    Sub OptParam_Test_4_()'显示在宏列表中OptParam_Test_4'输出:你好结束子

    Caller sub shows in Macros list

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

    [1 Developer-Visual Basic-视图-立即窗口(Ctrl + G)

[2与关于与该主题相关的多个SO帖子的各种评论相反

© www.soinside.com 2019 - 2024. All rights reserved.