Command 函数Command function

返回用于启动 Microsoft Visual Basic 或使用 Visual Basic 开发的可执行程序的命令行参数部分。Returns the argument portion of the command line used to launch Microsoft Visual Basic or an executable program developed with Visual Basic. Visual Basic Command 函数在 Microsoft Office 应用程序中不可用。The Visual Basic Command function is not available in Microsoft Office applications.




从命令行启动 Visual Basic 时,后面/cmd的命令行中的任何部分都将作为命令行参数传递给该程序。When Visual Basic is launched from the command line, any portion of the command line that follows /cmd is passed to the program as the command-line argument. 在下面的命令行示例中cmdlineargs ,表示命令函数返回的参数信息。In the following command line example, cmdlineargs represents the argument information returned by the Command function.

VB /cmd cmdlineargs

对于用 Visual Basic 开发的和编译为 .exe 文件的应用程序, Command 返回出现在命令行中应用程序名称后的任何参数。For applications developed with Visual Basic and compiled to an .exe file, Command returns any arguments that appear after the name of the application on the command line. 例如:For example:

MyApp cmdlineargs

若要了解如何在您使用的应用程序的用户界面中更改命令行参数,请搜索 "帮助" 中的 "命令行参数"。To find how command line arguments can be changed in the user interface of the application you are using, search Help for "command line arguments."


此示例使用 Command 函数获取一个函数的命令行参数,以便在包含数组的 Variant 中返回这些参数。This example uses the Command function to get the command line arguments in a function that returns them in a Variant containing an array. 在 Microsoft Office 中不可用。Not available in Microsoft Office.

Function GetCommandLine(Optional MaxArgs)
    'Declare variables.
    Dim C, CmdLine, CmdLnLen, InArg, I, NumArgs
    'See if MaxArgs was provided.
    If IsMissing(MaxArgs) Then MaxArgs = 10
    'Make array of the correct size.
    ReDim ArgArray(MaxArgs)
    NumArgs = 0: InArg = False
    'Get command line arguments.
    CmdLine = Command()
    CmdLnLen = Len(CmdLine)
    'Go thru command line one character
    'at a time.
    For I = 1 To CmdLnLen
        C = Mid(CmdLine, I, 1)
        'Test for space or tab.
        If (C <> " " And C <> vbTab) Then
            'Neither space nor tab.
            'Test if already in argument.
            If Not InArg Then
            'New argument begins.
            'Test for too many arguments.
                If NumArgs = MaxArgs Then Exit For
                NumArgs = NumArgs + 1
                InArg = True
            End If
            'Concatenate character to current argument.
            ArgArray(NumArgs) = ArgArray(NumArgs) & C
            'Found a space or tab.
            'Set InArg flag to False.
            InArg = False
        End If
    Next I
    'Resize array just enough to hold arguments.
    ReDim Preserve ArgArray(NumArgs)
    'Return Array in Function name.
    GetCommandLine = ArgArray()
End Function

另请参阅See also

支持和反馈Support and feedback

有关于 Office VBA 或本文档的疑问或反馈?Have questions or feedback about Office VBA or this documentation? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.