2

Excel VBAマクロのコマンドラインパラメーターにアクセスする必要があり、多くのバリエーションが見つかりましたが、Excel-2010で機能したのは1つだけで、APIは時間の経過とともに変更されたようです。私は「そこに」見つけたこのコードを試しました:

  Dim pCmdLine As Long     ' Pointer to the Comand-line string
  Dim strCmdLine As String ' Command line string
  Dim CmdLine As String    ' Command line string

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

   ' Fill the string with zeros
   '    (300 characters for command line seems to be enough)
   strCmdLine = String$(300, vbNullChar)

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

   ' At this point we got the string,
   '     now skip the rest of it filled with 0 characters.
   CmdLine = Left(strCmdLine, InStr(1, strCmdLine, vbNullChar) - 1)

  MsgBox "Length of the command line = " & Len(CmdLine) '' Debug
  MsgBox "Command Line:: " & CmdLine '' Debug 

これをスプレッドシートのAuto_openマクロに入れました。この呼び出しを試してみると:

start excel TestMacro.xlsm /e/abcd/xyz

それは一般的に機能しているようで、マクロは次のように報告します。

Command line = " C:/.../excel.exe TestMacro.xlsm"

したがって、呼び出し部分を取得しますが、パラメーターが失われます。

部分的な解決策: 呼び出しを次のように変更すると、次のようになります。

   start excel  /e/abcd/xyz TestMacro.xlsm

これは機能しますが、最後にないファイル名を無視するように解析コードを変更する必要があります。また、この形式では、引用符で囲まれている場合でも、パラメーターに空白を含めることができないようです。システムはそれらをターゲットExcelファイルのファイル名として解釈し、エラーを出すようです。例えば:

start excel /e/abc/"my sheet"/ TestMacro.xlsm

エラーが発生します:

file 'sheet"/.xlsx' not found

誤った起動エラーの後、目的のシートが開き、行全体が機能するようになります。

4

0 に答える 0