我在Outlook中有一个宏,我想在启动时运行...奇怪的请求我知道。我知道Application_Startup Sub但是我想知道是否可以将命令行参数传递给它?
编辑:我们真正的要求是有时在启动时根据命令行参数运行宏。我已经尝试过VBS和Application.Run以及自outlook 2003以来已弃用的命令行开关/自动运行。
答案 0 :(得分:1)
您可以使用GetCommandLine函数来检索当前进程的命令行字符串。要访问该函数,只需将此API声明粘贴到宏模块的顶部:
Declare Function GetCommandLineA Lib "Kernel32" () As String
然后在VBA子中,您可以使用以下代码:
Dim cmdLineArgs As String
'Get the commande line string
cmdLineArgs = GetCommandLineA
答案 1 :(得分:0)
发布者: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