2017-08-02 125 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 
相关问题