Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 135
Default Open a file in the same instance with ShellExecute

Hi everyone,

If someone knows if it's possible to use "ShellExecute"
(not workbooks.open) to open a file in the same instance
of the application where the macro is ? If yes, How to do it ?


'API
Private Declare Function ShellExecute Lib "shell32.dll" Alias _
"ShellExecuteA" (ByVal hwnd As Long, ByVal lpszOp As _
String, ByVal lpszFile As String, ByVal lpszParams As String, _
ByVal lpszDir As String, ByVal FsShowCmd As Long) As Long

Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Long

'-----------------------------------------
Sub ShellOpenFile()

Const SW_SHOWNORMAL = 1
Dim hwnd As Long
Dim File As String

File = "C:\Workbook2.xls"

hwnd = FindWindow(vbNullString, Application.Caption)
ShellExecute hwnd, "open", File, vbNullString, vbNullString, SW_SHOWDEFAULT

End Sub
'-----------------------------------------

Thanks for your collaboration and your help.



  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,600
Default Open a file in the same instance with ShellExecute

Try sending the desktop window 0

ShellExecute 0&, "open", Chr(34) & File & Chr(34), _
vbNullString, vbNullString, vbNormalFocus

May be worth bracketing the file with quotes in case of spaces as above.

If multiple excel instances are open ensure yours is the active window,
doesn't guarantee it won't open in an arbitrary instance but it seems to
help. But surely, why not simply use workbooks.open.

Regards,
Peter T

"MichDenis" wrote in message
...
Hi everyone,

If someone knows if it's possible to use "ShellExecute"
(not workbooks.open) to open a file in the same instance
of the application where the macro is ? If yes, How to do it ?


'API
Private Declare Function ShellExecute Lib "shell32.dll" Alias _
"ShellExecuteA" (ByVal hwnd As Long, ByVal lpszOp As _
String, ByVal lpszFile As String, ByVal lpszParams As String, _
ByVal lpszDir As String, ByVal FsShowCmd As Long) As Long

Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Long

'-----------------------------------------
Sub ShellOpenFile()

Const SW_SHOWNORMAL = 1
Dim hwnd As Long
Dim File As String

File = "C:\Workbook2.xls"

hwnd = FindWindow(vbNullString, Application.Caption)
ShellExecute hwnd, "open", File, vbNullString, vbNullString,

SW_SHOWDEFAULT

End Sub
'-----------------------------------------

Thanks for your collaboration and your help.





  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 135
Default Open a file in the same instance with ShellExecute

Thanks for your response.... but it's not working !

My goal was to be able to open a .DQY file using a vba procedure.

The submitted "sub" do the job but open the desired file in another instance of Excel.

If someone knows a way of doing it within tne same instance, I will be pleased to know it.

Thanks for your collaboration.



'API
Private Declare Function ShellExecute Lib "shell32.dll" Alias _
"ShellExecuteA" (ByVal hwnd As Long, ByVal lpszOp As _
String, ByVal lpszFile As String, ByVal lpszParams As String, _
ByVal lpszDir As String, ByVal FsShowCmd As Long) As Long

Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Long

'-----------------------------------------
Sub ShellOpenFile()

Const SW_SHOWNORMAL = 1
Dim hwnd As Long
Dim File As String

File = "C:\Workbook2.xls"

hwnd = FindWindow(vbNullString, Application.Caption)
ShellExecute hwnd, "open", File, vbNullString, vbNullString, SW_SHOWDEFAULT

End Sub
'-----------------------------------------


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Open new instance of excel every time I click file VIPGeorge Excel Discussion (Misc queries) 5 August 16th 12 07:57 PM
How to open a new instance of EXCEL and .xls file Launchnet Excel Worksheet Functions 10 June 19th 07 03:45 PM
How to open a new instance of Word and its .doc file Launchnet Excel Worksheet Functions 3 June 18th 07 06:20 PM
ShellExecute hangs Excel when opening XLS file [email protected] Excel Programming 5 January 3rd 07 06:35 AM
I would like to open a new instance of Excel each time I double-click on a xls file Mark Excel Worksheet Functions 4 September 1st 05 02:29 PM


All times are GMT +1. The time now is 05:59 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"