共用方式為


Command 函數

傳回用來啟動 Microsoft Visual Basic 或透過 Visual Basic 開發之可執行程式的命令列引數部分。 Visual Basic Command 函式可在 Microsoft Access 中使用,但在其他 Microsoft Office 應用程式中則無法使用。

語法

Command

備註

從命令列啟動 Visual Basic 時,/cmd 後面的命令列的任何部分會以命令列引數的形式傳遞給程式。 在下列命令列範例中,cmdlineargs 代表 Command 函數傳回的引數資訊。

VB /cmd cmdlineargs

針對以 Visual Basic 開發並編譯到 .exe 檔案的應用程式,Command 會傳回在命令列的應用程式名稱後出現的任何引數。 例如:

MyApp cmdlineargs

若要了解命令列引數在您使用的應用程式使用者介面中如何變更,請搜尋「命令列引數」說明。

範例

此範例使用 Command 函數以在函數中取得命令列引數,該函數會在包含陣列的 Variant 中傳回它們。 可在 Microsoft Access 中使用,但無法在其他 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
        Else
            '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

另請參閱

支援和意見反應

有關於 Office VBA 或這份文件的問題或意見反應嗎? 如需取得支援服務並提供意見反應的相關指導,請參閱 Office VBA 支援與意見反應