2017-08-02 61 views
1

我有一個宏,我想在啓動時運行有時候...我知道的奇怪的請求。我知道Application_Startup Sub,但我想知道是否有可能將命令行參數傳遞給它?是否可以將參數傳遞給Outlook中的Application_Startup子?

編輯:我們的真正需求是有時在啓動時根據命令行參數運行宏。我已經嘗試了VBS和Application.Run以及自Outlook 2003以來已棄用的命令行開關/自動運行。

回答

1

您可以使用GetCommandLine函數來檢索當前進程的命令行字符串。要訪問函數只是複製粘貼此API聲明在宏模塊的頂部:

Declare Function GetCommandLineA Lib "Kernel32"() As String 

然後在VBA子,你可以使用下面的代碼:

Dim cmdLineArgs As String 

'Get the commande line string 
cmdLineArgs = GetCommandLineA 
+0

我嘗試這樣做,幾乎打破了我的預期,我做錯了什麼?我寫的代碼採用了命令行參數,然後顯示了一個包含它們的消息框。如果沒有的話,這會破壞嗎? –

+0

在Word中嘗試過...導致崩潰...我讀了一個地方,贏得64位API是不同的...發現一些代碼...下面張貼...在win7 64 – jsotola

0

發現這一點:https://social.msdn.microsoft.com/Forums/en-US/0017d844-3e4a-4115-bc51-cf02ca23db0c/vba-to-fetch-excel-command-line-64-bit?forum=exceldev

發表:https://social.msdn.microsoft.com/profile/andreas%20killer/?ws=usercard-mini

'Note: Declaration is overloaded with LONG! 
#If Win64 Then 
Private Declare PtrSafe Function GetCommandLineL Lib "kernel32" Alias "GetCommandLineA"() As LongPtr 
Private Declare PtrSafe Function lstrcpyL Lib "kernel32" Alias "lstrcpyA" (ByVal lpString1 As String, ByVal lpString2 As LongPtr) As Long 
Private Declare PtrSafe Function lstrlenL Lib "kernel32" Alias "lstrlenA" (ByVal lpString As LongPtr) As Long 
#Else 
Private Declare Function GetCommandLineL Lib "kernel32" Alias "GetCommandLineA"() As Long 
Private Declare Function lstrcpyL Lib "kernel32" Alias "lstrcpyA" (ByVal lpString1 As String, ByVal lpString2 As Long) As Long 
Private Declare Function lstrlenL Lib "kernel32" Alias "lstrlenA" (ByVal lpString As Long) As Long 
#End If 
' 

Function GetCommandLine() As String 

    #If Win64 Then 
    Dim lngPtr As LongPtr 
    #Else 
    Dim lngPtr As Long 
    #End If 

    Dim strReturn As String 
    Dim StringLength As Long 

    lngPtr = GetCommandLineL      ' Get the pointer to the commandline string 
    StringLength = lstrlenL(lngPtr)     ' get the length of the string (not including the terminating null character): 
    strReturn = String$(StringLength + 1, 0)  ' initialize our string so it has enough characters including the null character: 
    lstrcpyL strReturn, lngPtr      ' copy the string we have a pointer to into our new string: 
    GetCommandLine = Left$(strReturn, StringLength) ' now strip off the null character at the end: 

End Function 

Sub getCmdLine() 

    Debug.Print GetCommandLine() 

End Sub 
相關問題