2017-12-11 46 views
3

Excelブックを開くときにコマンドライン引数を取得するコードがあります(64ビット、ただし32ビットコードも#if句の下にあります)。私は、コマンドプロンプトで次のコード行を実行したときにEXCEL 64ビットコマンドラインvbaコード

したがって、たとえば、私は、コマンドライン引数として入力された文字列フェッチすることができると期待しています:。

Excelを起動し、」\ AwajiPush.xlsm 「/p/"kjh%dg.pdf」

私は期待してい

にする(ところで、なぜ「スタート」理由がある、それは.BATバッチファイルで働くだろうとそうです) "。\ AwajiPush.xlsm"と /p/"kjh%dg.pdf " をパラメータとして取り込むことができます。

コードはそれをしません。

なぜ2番目の引数をフェッチしないのですか?

ポインタの仕組みについてはあまりよく分かりませんが、 私はそれを解析できるように、少なくとも両方のパラメータを含む文字列をキャプチャするために使用できるコードがありますか?それ以上のものが含まれていれば、それは問題ありません。一貫している限り、私はいつもそれを解釈することができます。

私はプログラム(MsgBox)にスタブを入れましたが、なぜ2番目のスタブが空白であるのかわかりません。ここで

コードです:

'Put this code in a new module called Parameters 

Option Explicit 

#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 
    Dim strReturn As String 
    #If Win64 Then 
    Dim lngPtr As LongPtr 
    #Else 
    Dim lngPtr As Long 
    #End If 

    Dim StringLength As Long 
    'Get the pointer to the commandline string 
    lngPtr = GetCommandLineL 

    'get the length of the string (not including the terminating null character): 
    StringLength = lstrlenL(lngPtr) 
    MsgBox StringLength 


    'initialize our string so it has enough characters including the null character: 
    strReturn = String$(StringLength + 1, 0) 
    'copy the string we have a pointer to into our new string: 
    MsgBox strReturn 


    lstrcpyL strReturn, lngPtr 
    'now strip off the null character at the end: 
    MsgBox strReturn 


    GetCommandLine = Left$(strReturn, StringLength) 

End Function 

そして

'Put this code in "This Workbook" 

Sub workBook_open() 
    MsgBox Parameters.GetCommandLine 
End Sub 

答えて

0

はここで現在のプロセスからのコマンドラインを取得する機能です:あなたが持っているでしょう

Private Declare PtrSafe Function w_commandline Lib "kernel32.dll" Alias "GetCommandLineW"() As LongPtr 
Private Declare PtrSafe Function w_strlen Lib "kernel32.dll" Alias "lstrlenW" (ByVal lpString As LongPtr) As Long 
Private Declare PtrSafe Sub w_memcpy Lib "kernel32.dll" Alias "RtlMoveMemory" (dst As Any, src As Any, ByVal size As LongPtr) 

Public Function GetCommandLine() As String 
    GetCommandLine = String$(w_strlen(w_commandline()), 0) 
    w_memcpy ByVal StrPtr(GetCommandLine), ByVal w_commandline(), LenB(GetCommandLine) 
End Function 

Sub Test() 
    Debug.Print GetCommandLine() 
End Sub 

注意/eスイッチを使用して、既に起動されているExcelのインスタンスへのリダイレクトを回避します。提供されたパラメータを維持する。 、

Set MyArguments=abcde 
start "xl" excel.exe /e "C:\temp\myfile.xlsm" 

excel.exe /e "C:\temp\myfile.xlsm" /p "myparam" 

またはstartと::たとえば

start "xl" excel.exe /e "C:\temp\myfile.xlsm" /p "myparam" 

しかし、あなたの目標は、環境変数を使用して、バッチからVBAにいくつかの引数を提供することを目的とする場合Excelから引数を取得する:

Debug.Print Environ("MyArguments") ' >> "abcde" ' 
関連する問題