Как получить параметры командной строки WinWord.exe внутри макроса AutoExe c? - PullRequest
1 голос
/ 21 апреля 2020

Внутри макроса Word AutoExec я хотел бы работать с некоторыми функциями. Для этого мне нужен идентификатор, заданный параметром командной строки при запуске WinWord.exe.

Пример: "C:\Program Files\Microsoft Office\root\Office16\WINWORD.EXE" /MyParam:5

Мне нужно это 5

Как я могу получить это?

1 Ответ

0 голосов
/ 29 апреля 2020

VBA изначально не предоставляет такую ​​информацию. Для получения аргументов командной строки необходимо использовать API Windows.

В следующем коде показано, как использовать API GetCommandLineA и lstrcpynA для извлечения полной командной строки. Затем он анализируется для выбора каждого отдельного аргумента.

Declare Function GetCommandLineA Lib "kernel32" () As Long
Declare Function lstrcpynA Lib "kernel32" ( _
   ByVal pDestination As String, ByVal pSource As Long, _
   ByVal iMaxLength As Integer) As Long

Function GetCmdLineInfo() As String

   ' Pointer to the command line
   ' Which will be passed as pSource to lstrcpynA
   Dim ptrCmdLine As Long
   ' Will hold the command line after the call to lstrcpynA
   ' Pointer to the destination; before being passed...
   ' must first be initialized with enough characters to hold that string
   Dim strCmdLine As String

   ' Get the pointer to the command line string
   ptrCmdLine = GetCommandLineA

   ' Fill the string with enough zeros to make sure there are enough
   ' characters available for the command string (which will replace the content).
   ' 300 is an arbitrary number, more might be necessary.
   strCmdLine = String$(300, vbNullChar)

   ' Copy from the pointer to a VBA-style string
   lstrcpynA strCmdLine, pCmdLine, Len(strCmdLine)

   ' Remove the extra vbNullChar characters at the end of the command line
   strCmdLine = left(strCmdLine, InStr(1, strCmdLine, _
      vbNullChar) - 1)

   GetCmdLineInfo = strCmdLine
End Function

Function GetCmdLineArgs(strCmdLine As String) As String
    Dim lExePos As Long, lSpaceAfterExe As Long
    Dim strArgString As String

    'Get the end of the path to the exe file...
    lExePos = InStr(LCase(strCmdLine), ".exe")
    strArgString = Mid(strCmdLine, lExePos + 4)

    'Move beyond any quote characters and spaces after '.exe'
    'The first argument may be the path to a file or
    'an argument beginning with a forward slash, so get it all.
    lSpaceAfterExe = InStr(strArgString, " ")
    If lSpaceAfterExe > 0 Then
        strArgString = Mid(strArgString, lSpaceAfterExe + 1)
    Else
        strArgString = "No args"
    End If
    GetCmdLineArgs = Trim(strArgString)
End Function

Sub TestCmdLineargs()
    Dim strCmdLine As String
    Dim strCmdArgs

    strCmdLine = GetCmdLineInfo
    strCmdArgs = GetCmdLineArgs(strCmdLine)
    'Debug.Print Len(strCmdLine), strCmdLine, strCmdArgs

   'Extract the individual args to an array
    Dim strArgChar As String
    Dim lFirstArgPos As Long, lNextArgPos As Long
    Dim argsList() As String
    Dim strArgString As String
    Dim argsCounter As Long

    strArgChar = " /"
    argsCounter = 0

    lFirstArgPos = InStr(strCmdArgs, strArgChar)
    'If the first argument is a file path, store that in the array
    If left(strCmdArgs, 1) <> "/" Then
        ReDim Preserve argsList(argsCounter)
        strArgString = Trim(left(strCmdArgs, lFirstArgPos - 2))
        argsList(argsCounter) = strArgString
        argsCounter = argsCounter + 1
    End If

    'Get the rest of the args, that start with a /
    Do While lFirstArgPos > 0
        ReDim Preserve argsList(argsCounter)
        strArgString = Mid(strCmdArgs, lFirstArgPos + 1)
        lNextArgPos = InStr(lFirstArgPos + 2, strCmdArgs, strArgChar)

        'If lNextArgPos is not greater than 0, then there are no more args
        If lNextArgPos <= 0 Then
            argsList(argsCounter) = strArgString
            Exit Do
        Else
            strArgString = Mid(strCmdArgs, lFirstArgPos + 1, lNextArgPos - lFirstArgPos)
            argsList(argsCounter) = strArgString
            argsCounter = argsCounter + 1
            lFirstArgPos = lNextArgPos
        End If
    Loop

    Dim i As Long

    For i = LBound(argsList) To UBound(argsList)
        Debug.Print argsList(i)
    Next
End Sub
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...