kaa*_*han 6 vba ms-word default-value optional-parameters word-2016
我正在尝试编写一个允许具有默认值的可选参数的 VBA 子程序。我尝试了Microsoft Docs - 可选参数 (Visual Basic)中的示例代码,但它导致子程序未显示在可用子程序列表中(即来自视图宏)。
\n\nSub notify(ByVal company As String, Optional ByVal office As String = "QJZ")\n If office = "QJZ" Then\n Debug.WriteLine("office not supplied -- using Headquarters")\n office = "Headquarters"\n End If\n \' Insert code to notify headquarters or specified office.\nEnd Sub\nRun Code Online (Sandbox Code Playgroud)\n\n我尝试过但无法出现的子声明是:
\n\nSub HighlightByFont( _\n Optional ByVal highlightFont As Variant = "", _\n Optional ByVal highlightColor As Variant = "", _\n Optional ByVal highlightText As Variant = "", _\n Optional ByVal matchWildcards As Variant = False, _\n Optional ByVal useGUI As Variant = False, _\n Optional ByVal highlightBold As Variant = False, _\n Optional ByVal highlightItalic As Variant = False)\nRun Code Online (Sandbox Code Playgroud)\n\n现在,我不得不满足以下IsMissing 逻辑:
\n\nSub HighlightByFont( _\n Optional ByVal highlightFont As Variant, _\n Optional ByVal highlightColor As Variant, _\n Optional ByVal highlightText As Variant, _\n Optional ByVal matchWildcards As Variant, _\n Optional ByVal useGUI As Variant, _\n Optional ByVal highlightBold As Variant, _\n Optional ByVal highlightItalic As Variant)\nRun Code Online (Sandbox Code Playgroud)\n\n是否可能(仍然\xe2\x80\xa0),如果可以,如何:
\n\n环境:
\n\n\xe2\x80\xa0所有参考文献,包括与 VBA 可选参数相关的 SO 答案,均来自 2015 年。
\n您提供的链接是 VB Net 而不是 VBA 的帮助页面。VBA 和 VB .Net 很相似,但用例却截然不同。VBA 是 Microsoft Office 应用程序使用的内置脚本语言。VB Net 是一种完整的 .Net 语言,它源于 VBA,但不被 Office 应用程序使用,除非您编写特定的 VSTO 插件或应用程序。
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
Run Code Online (Sandbox Code Playgroud)
您还可以帮我们大家一个忙,学习使用换行符,以便
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)
Run Code Online (Sandbox Code Playgroud)
写成
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 _
)
Run Code Online (Sandbox Code Playgroud)
您还应该意识到,任何使用默认值定义的可选参数都不会丢失,有时 IsMissing 是更好的选择,因为它不可能提供合理的默认值。
是的,具有默认值的可选参数直到 Word 2016 仍然有效。VBA参考指出:
\n\n\n\n\narglist 参数具有以下语法和部分:
\n\n[ 可选 ] [ ByVal | ByRef ] [ ParamArray ] varname [ ( ) ] [ 作为类型 ] [ = 默认值 ]
\n\n可选 可选。表示不需要参数的关键字。如果使用,arglist 中的所有后续参数也必须是可选的,并使用Optional关键字声明。如果使用ParamArray ,则可选不能用于任何参数。
\n\n默认值可选。任何常量或常量表达式。仅对可选参数有效。如果类型是Object,则显式默认值只能是Nothing。
\n
Sub OptParam_Test_1()\n \'Shows in Macro list\nEnd Sub\n\nSub OptParam_Test_2(param)\n \'Does not show in Macro list\n Debug.Print (param) \'Output for OptParam_Test_2 "hello": hello\nEnd Sub\n\nSub OptParam_Test_3(Optional param)\n \'Shows in Macro list\n Debug.Print (param) \'Output: hello\nEnd Sub\n\nSub OptParam_Test_4(Optional param As String = "hello")\n \'Does not show in Macro list\n Debug.Print (param) \'Output: hello\nEnd Sub\n\nSub OptParam_Test_5(Optional param As Variant = "hello again")\n \'Does not show in Macro list\n Debug.Print (param) \'Output: hello again\nEnd Sub\nRun Code Online (Sandbox Code Playgroud)\n\n使用默认值声明参数时的意外接口行为是子将停止出现在宏列表中。
\n\n\n\n这不应被解释为子系统有问题或无法使用。
\n\n
调用包含默认值参数的子函数的两个选项是:
\n\n Sub OptParam_Test_4(Optional param As String = "hello")\n \'Does not show in Macro list\n Debug.Print (param) \'Output: hello\n End Sub\n\n Sub OptParam_Test_4_()\n \'Shows in Macro list\n OptParam_Test_4\n \'Output: hello\n End Sub\nRun Code Online (Sandbox Code Playgroud)\n\n\n\n1开发人员 - Visual Basic - 查看 - 立即窗口 (Ctrl+G)
\n\n2与该主题相关的多个 SO 帖子的各种评论相反
\n| 归档时间: |
|
| 查看次数: |
12379 次 |
| 最近记录: |