Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VBA Sub optional parameters with default values

I am trying to write a VBA sub that allows optional parameters with default values. I tried the sample code from Microsoft Docs - Optional Parameters (Visual Basic) but it leads to the sub not showing on list of available subs (i.e. from 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

The sub declaration I tried, but couldn't get to appear, was:

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)

For now, I have had to settle for the below with 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)

Is it (still) possible, and if so, how:

  1. to set up the parameter declaration?
  2. to get it to appear in the View Macros list?

Environment:

  • Word 2016 x64
  • Windows 10

All references, including SO answers related to VBA optional paramaters are from 2015.

like image 469
kaanchan Avatar asked Jan 26 '23 02:01

kaanchan


1 Answers

The link you have provided is to a help page for VB Net and Not VBA. VBA and VB .Net are similar but have very different use cases. VBA is the built in scripting language used by Microsoft Office applications. VB Net is a full bore .Net Language which has its roots in VBA but is not used by Office Application unless you write specific VSTO addins or applications.

Optional arguments in VBA work fine. The VBA version of the code sample you provided above is.

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

You could also do us all a bit of a favour and learn to use the line break so that

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)

is written as

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 _
)

You should also be aware that any optional parameters defined with a default can never be missing and that sometimes IsMissing is the better option to use because its not possible to provide a sensible default.

like image 135
freeflow Avatar answered Jan 31 '23 10:01

freeflow