View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
Michael H Michael H is offline
external usenet poster
 
Posts: 2
Default Determine If A File Was Specified When Starting Excel

Leith and NickHK,

Thanks very much for your responses.

Based on the code from the link provided by NickHK, I created the following
function:

Declare Function GetCommandLineA Lib "Kernel32" () As String

Public Function FileParameter() As Boolean

Dim strCmdLine As String

strCmdLine = GetCommandLineA
strCmdLine = Mid(strCmdLine, 1, 255)

If InStr(1, strCmdLine, "/e") < 0 Then
FileParameter = True
Else
FileParameter = False
End If

End Function


This worked, but sometimes when opening a file directly in Excel, it would
crash with a "Microsoft Excel has encountered a problem and needs to close"
error. When I put a stop in the code so as to step through it, the crash
does not occur. I am unable to find the cause of this issue so far.

After a bit more searching for info on "Command Line", I found the following:
http://groups.google.co.uk/group/mic...ca75a8220396ce


I managed to use the code in Post# 6 to accomplish what I needed:

Private Declare Function GetCommandLine Lib "kernel32" Alias
"GetCommandLineA" () As Long

Select Case GetCommandLine()
Case 1385360
'No file specified. Add new workbook.
Application.Workbooks.Add
Case 1385376
'File specified. Do nothing
End Select

I need to do more testing, because I'm not sure yet if there are any other
possible values for the results of GetCommandLine. However, it works well so
far.

Once again, thanks to both of you for the responses, which helped to put me
on the right track.

-Michael