VB and VBA Users Source Code: Returning command line parameters in Excel
[
Home
|
Contents
|
Search
|
Reply
| Previous | Next ]
VB/VBA Source Code
Returning command line parameters in Excel
By:
Andrew Baker
Email (spam proof):
Email the originator of this post
Date:
Friday, April 13, 2001
Hits:
1380
Category:
VBA (Visual Basic for Applications)
Article:
The following routine demonstrate how to return command line parameters in Excel. Please read the function notes before using this routine: Option Explicit '---API Declare--- Private Declare Function GetCommandLine Lib "kernel32" Alias "GetCommandLineA" () As Long Private Declare Function lstrlen Lib "kernel32" Alias "lstrlenA" (ByVal lpString As Long) As Long Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDst As Any, pSrc As Any, ByVal ByteLen As Long) 'Purpose : Returns the command line 'Inputs : N/A 'Outputs : The command line used to initate Excel 'Author : Andrew Baker (www.vbusers.com) 'Date : 25/11/2000 03:33 'Notes : To return meaningful command line information you must open the workbook ' by shelling it as a parameter for Excel.exe, eg. using the following syntax: ' "D:\Program Files\Microsoft Office\Office\EXCEL.EXE" /MyCommand "d:\command line.xls" ' i.e. The path to Excel, then the command and finally the workbook. ' Note the paths are encapsulated in double quotes. ' NT USERS: YOU MUST OPEN EXCEL BY SHELLING IT USING THE SYNTAX GIVEN ABOVE. ' IF A WORKBOOK IS OPENED BY DOUBLE CLICKING IT IN EXPLORER THIS API CALL WILL CAUSE A GPF. ' 'Revisions : Function CommandLine() As String Dim lRetStr As Long, lLen As Long Static ssCmd As String If Len(ssCmd) = 0 Then 'Get a pointer to a string, which contains the command line lRetStr = GetCommandLine 'Get the length of that string lLen = lstrlen(lRetStr) If lLen > 0 Then 'Create a Buffer ssCmd = Space$(lLen) 'Copy to the Buffer CopyMemory ByVal ssCmd, ByVal lRetStr, lLen End If End If CommandLine = ssCmd End Function 'Demonstration routine 'Place in the "ThisWorkbook" class Sub Auto_Open() MsgBox "Command line: " & CommandLine End Sub 'Run this code to demonstrate how to use the code Sub Test() Shell """" & Application.Path & "\excel.exe"" /TEST! """ & ThisWorkbook.FullName & """", vbNormalFocus End Sub
Terms and Conditions
Support this site
Download a trial version of the Excel Workbook Rebuilder