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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Excel - ShellExecute

Status
Not open for further replies.

pbazsmir

Technical User
Nov 2, 2005
3
0
0
PL
Hi,

I'm using the code below to open different kind of files. But it doesn't work with, for example, Excel files. I'd like to have one universal code for opening files.

Code:
Option Explicit

Private Const SW_SHOWNORMAL = 1

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


Sub OpenFile()

Dim Hwnd As Long
Const FileName = "c:\temp\data.xls"

   On Error GoTo Errorhandler
   ShellExecute Hwnd, vbNullString, FileName, vbNullString, _
   vbNullString, SW_SHOWNORMAL
   Exit Sub
   
Errorhandler:
   'MsgBox "Error: " & Err.Number
   MsgBox "Error occured. I can't open the file" 
   Exit Sub
    
End Sub

What's wrong? I'm using Office 2000, Windows XP Professional, SP2.

Thanks in advance,

pbazsmir
 
pbaszmir,

What application is your VBA running from? When I tested your code, I had no problem opening an Excel file from MS Word but got strange behavior when executing from Excel (hourglass cursor appeared / no observable action / couldn't quit Excel). When I ran your code from Excel but substituted a Word doc file in the call to ShellExecute, it opened in Word properly.


Regards,
Mike
 
sdraper said:
What is the error being returned?
There's no error. Nothing happens! Just like rmikesmith described.

rmikesmith said:
When I tested your code, I had no problem opening an Excel file from MS Word but got strange behavior when executing from Excel (hourglass cursor appeared / no observable action / couldn't quit Excel). When I ran your code from Excel but substituted a Word doc file in the call to ShellExecute, it opened in Word properly.
I have exactly the same results (Excel 2000). I can open *.exe, *.doc, *.txt, *.rtf, etc. but no *.xls. And I don't know why.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top