shell - open document

J

Jarle

I want to open a document. Document format may vary.

I try with:

Dim f_name as String
Dim Returnvalue as Variant
f_name ="E:\forlder1\folder2\test.pdf"

ReturnValue = Shell("start " & Chr$(34) & f_name Chr$(34) )

According to a tip on a different web-site this should open the file in the
program it is connected to (here: Acrobat Reader).

I always get error message "file not found", but the file is there.
What is wrong?

rgds
Jarle
 
I

Ivyleaf

I want to open a document. Document format may vary.

I try with:

Dim f_name as String
Dim Returnvalue as Variant
f_name ="E:\forlder1\folder2\test.pdf"

ReturnValue = Shell("start " & Chr$(34) & f_name Chr$(34) )

According to a tip on a different web-site this should open the file in the
program it is connected to (here: Acrobat Reader).

I always get error message "file not found", but the file is there.
What is wrong?

rgds
Jarle

Hi Jarle,

In your path, you have "Forlder1" which I reckon should maybe be
"Folder1"? This may be all that is wrong with your code.

Cheers,
Ivan.
 
J

Jarle

Unfortunately is not that simple.
Folder1/folder2 is just an example. That path that I use is in the program
is different, and I have checked that it is correct. Ehen I paste the
path+filename in the "Run" command in the Start menu (Windows), then the
document opens.
I have also tried to leave out Chr(34) because is seems this gives "".
I have also made sure that I do not have space in pathname or filename.
Still no solution.'

What I want to do is to open a file from VBA code. File might not be office
document. Any suggestion?

Ivyleaf skrev:
 
I

Ivyleaf

Unfortunately is not that simple.
Folder1/folder2 is just an example. That path that I use is in the program
is different, and I have checked that it is correct. Ehen I paste the
path+filename in the "Run" command in the Start menu (Windows), then the
document opens.
I have also tried to leave out Chr(34) because is seems this gives "".
I have also made sure that I do not have space in pathname or filename.
Still no solution.'

What I want to do is to open a file from VBA code. File might not be office
document. Any suggestion?

Ivyleaf skrev:







- Show quoted text -

Hi Jarle,

This doesn't use the Shell function, but it might do what you are
after:

Option Explicit

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

Private Const SW_HIDE As Long = 0
Private Const SW_SHOWNORMAL As Long = 1
Private Const SW_SHOWMAXIMIZED As Long = 3
Private Const SW_SHOWMINIMIZED As Long = 2

Sub OpenFile()
Dim FName As String
Dim RetVal
FName = "E:\Folder 1\Folder 2\Test File.pdf"

ShellExecute Application.hwnd, "open", FName _
', vbNullString, "C:\", SW_SHOWNORMAL

End Sub

I found / pinched / adapted (slightly) the code from here:
http://www.vbforums.com/showthread.php?t=481233

Cheers,
Ivan.
 
P

Peter T

Unfortunately is not that simple.
Folder1/folder2 is just an example. That path that I use is in the program
is different, and I have checked that it is correct. Ehen I paste the
path+filename in the "Run" command in the Start menu (Windows), then the
document opens.
I have also tried to leave out Chr(34) because is seems this gives "".
I have also made sure that I do not have space in pathname or filename.
Still no solution.'

What I want to do is to open a file from VBA code. File might not be office
document. Any suggestion?

Ivyleaf skrev:







- Show quoted text -

Hi Jarle,

This doesn't use the Shell function, but it might do what you are
after:

Option Explicit

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

Private Const SW_HIDE As Long = 0
Private Const SW_SHOWNORMAL As Long = 1
Private Const SW_SHOWMAXIMIZED As Long = 3
Private Const SW_SHOWMINIMIZED As Long = 2

Sub OpenFile()
Dim FName As String
Dim RetVal
FName = "E:\Folder 1\Folder 2\Test File.pdf"

ShellExecute Application.hwnd, "open", FName _
', vbNullString, "C:\", SW_SHOWNORMAL

End Sub

I found / pinched / adapted (slightly) the code from here:
http://www.vbforums.com/showthread.php?t=481233

Cheers,
Ivan.

---------------------------------------------

ShellExecute Application.hwnd, "open", FName _
', vbNullString, "C:\", SW_SHOWNORMAL

For use in Excel 97 & 2000, change
Application.hwnd
to
0&
or use the FindWindow API to get the app's window handle

In later OS may need to change "open" to vbNullString

Regards,
Peter T
 
J

Jarle

This works fine. Thank you!
I had to put in 0& as suggested by Peter T.
Unfortunatelely I still have a problem and a question:

1. If the document to open is an excel workbook. It seems that I reach an
infinite loop or something. The macro keeps running forever. Any Idea why?
How can I check if the document is an excel document. Then I can use the
application.open (?) command for such files.

2. My excel version is 9.0. From which version will I need to use
application.hwnd?
I would like my program to check version (application.version) and then then
use the correct commandline.

Jarle
Peter T skrev:
 
P

Peter T

Jarle said:
This works fine. Thank you!
I had to put in 0& as suggested by Peter T.
Unfortunatelely I still have a problem and a question:

1. If the document to open is an excel workbook. It seems that I reach an
infinite loop or something. The macro keeps running forever. Any Idea why?
How can I check if the document is an excel document. Then I can use the
application.open (?) command for such files.

If Right$(LCase$(sFile), 4) Like ".xl*" Then
Application.Workbooks.Open sFile
End If

first ought test to see if the file is already open

2. My excel version is 9.0. From which version will I need to use
application.hwnd?

It was introduced in v.10 XL2002
I would like my program to check version (application.version) and then then
use the correct commandline.

It's probably OK simply to use 0& as I suggested, which defaults to the
desktop's window, but if you prefer following will work for all versions

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


Dim XLhWin as Long
XLhWin = FindWindow("XLMAIN", Application.Caption)

Regards,
Peter T

PS, watch out for that "open", if vbNulllString works for you stick with
that
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top