Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Shell for opening .xls or .txt 1

Status
Not open for further replies.

Tommyhat

Programmer
Sep 10, 2004
96
CA
I need to open an excel file before i read from it or i'll get an error so i figured i'd just work that into the code.

Dim var As Variant
var = Shell("C:\rip.xls", vbMaximizedFocus)

But with everything but .exe files, it gives an invalid argument error, is there some way i can run an excel file like this?
 
UPDATE!

I can open excel files like this:

Dim var As Variant
Dim filename As String
filename = "C:\Documents and Settings\tgirard\Desktop\Andrew\testing data\yarr.xls"
var = Shell("C:\Program Files\Microsoft Office\Office\EXCEL.exe " & filename, vbMaximizedFocus)

BUT the spaces in "documents and settings" make it search for C:\Documents.xls, and.xls, Settings\tgirard\Desktop\Andrew\testing.xls and data\yarr.xls instead of the full file path.
 
You could just use ShellExecute:

Private Declare Function ShellExecute Lib "shell32.dll" _
Alias "ShellExecuteA" (ByVal hWnd As Long, ByVal lpOperation _
As String, ByVal lpFile As String, ByVal lpParameters As String, _
ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long

Private Sub Command1_Click()
Dim res As Long
Dim filename As String
filename = "C:\Old Drive\Test.xls"
res = ShellExecute(hWnd, vbNullString, filename, vbNullString, vbNullString, vbNormalFocus)
If res = 31 Then
MsgBox "This file is not associated with a program!", vbCritical, "File Association Error"
Exit Sub
End If
End Sub

Swi
 
Thanks dood!
Do i have to include any references or declairations for this?
 
Nope. It will open you text files are well. ShellExecute opens the file with its default program thus the error checking if the file is not associated with a program.

Swi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top