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

ShellExecuteEx API

Status
Not open for further replies.

cramd

Programmer
Mar 28, 2001
214
US
I am trying out the "shellexecuteex" but can't get past step 1. My goal is to read a file and print documents without any user intervention. I've been studying some examples on this forum but I must be missing pieces. When trying to run this program, it errors out at the stament "me.hwnd" - according to the error, I need a class module(??) -- I have this code in a standard module. Doing API's is a deeper than I've gone before with VB, but willing to figure this out.

STANDARD MODULE
***

Private Sub Main()
Dim strdocname As String
strdocname = "c:\data\bs-original\bksv1"
Call OpenThisDoc(Me.hwnd, strdocname)

End Sub
****************************************

GLOBAL MODULE
***

Public 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

Public Sub OpenThisDoc(formname As Long, filename As String)
On Error Resume Next
Dim x As Long
x = shellexecute(formname, "Print", filename, 0&, 0&, 3)

End Sub

*****************
Thoughts would be appreciated!
cramd
 
Where does the Me.hwnd come from in the module? You need a window handle (hwnd). They don't come from modules, they come from forms (or any other kind of window).
Greetings,
Rick
 
Maybe not only do I need to read up on the shellexecuteex but also the Me.hwnd....... Since there will not be any user intervention with this program, I have no forms, only using a modmain module. Since I'm not using a form for the Me.hwnd, I'm not sure how to handle this.
Thanks for your thoughts!
cramd
 
I think you can just send a null (a 0, NOT a vbNull) for the hWnd argument.
Greetings,
Rick
 
Rick,
Sorry to be so blind on this, but the statement you would be referring to is:
was using:
Call OpenThisDoc(Me.hwnd, strdocname)

tried this--with not much luck
Call OpenThisDoc(Me.hwnd a 0, strdocname)

cramd

 
No I meant to the ShellExecute API.
Forget about the me.hwnd and just send a 0 to the API.
Greetings,
Rick
 
Hi there

There is a couple of things you have to consider here:

* hwnd is the handle for your vb application window, meaning the one where you are executing the shellexecute function. Shellexecute requires this handle in order to pass any errors that may occurr during the operations performed on your executed file back to your vb app so you can handle them with code instead of having the user getting a nasty error.

* You will have to use another api function like FindWindow() in order to find your vb's window hwnd and pass it then to Shellexecute()

* The example you are using as a base was created in VBA (most likely MS Access). In there you can use Me and its properties since Me is an intrinsic variable that Access uses to designate the active form (the one with the focus).

I hope this helps.


 
As I said in thread222-482951 where I posted the code shown above, this is not error checked.

As LazyMe says, change the first parameter of the Shellexecute function with 0& as it's first parameter

x = shellexecute(0&, "Print", filename, 0&, 0&, 3)

or to keep that routine generic, call it from your program as:

Call OpenThisDoc(0&, strdocname)

(The 0& just declares the value to be a Long)
________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
<ahem>

* hwnd is the ... Shellexecute requires this handle

Not really. The handle is nominally required so that ShellExecute can display any messageboxes (not restricted to error dialogs) in a nominated parent window. It sure as heck isn't for passing messages back that you can handle in code. But you can happily pass 0, which means the desktop will get used.

* You will have to use another api function like FindWindow

Nope. A VB form has a directly retrievable hWnd, which is held in the form's <tada..> hWnd property. But anyway, as discussed above, we don't really need it

* The example...VBA...In there you can use Me

VB Forms also have a Me property.

 
Everyone,
Thanks for all the great posts on this--I'm slowly figuring this out. I'm able to debug this thing without errors, but when I get to this line &quot;x = shellexecute(0&, &quot;Print&quot;, filename, 0&, 0&, 3)&quot; I guess I should expect my file to print--right?? Prior to hitting this statement in debug, x = 0, after this statement processes, x = 2. I'm guessing this is an error because my file isn't printing yet--but I'll be back with more questions until this works!!!
Again, thanks for the comments on this.
Also TIGERBLANCO - thanks for clearing up the &quot;ME&quot; property related to VBA - I work alot with VBA and knowing this helps.
cramd
 
Error return 2 means:
The system cannot find the file specified.
Greetings,
Rick
 
Rick,
I'm still trying this &quot;shell&quot; function.....I found another piece of code and I'm able to debug this to the point that I can understand what is happening. I continually get the file not found error(2)--which I don't understand, the file is there, I've even matched the case for the file name - (which it wouldn't matter upper/lower on this - right?). Anyway, I've posted the code that I am working with - and if you have a chance to run it, I would be interested to see if it works for you.

**********************
Option Explicit

Public Declare Function shellexecute Lib &quot;shell32.dll&quot; Alias _
&quot;ShellExecuteA&quot; (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 Declare Function GetDesktopWindow Lib &quot;user32&quot; () As Long


Const SW_SHOWNORMAL = 1

Const SE_ERR_FNF = 2&
Const SE_ERR_PNF = 3&
Const SE_ERR_ACCESSDENIED = 5&
Const SE_ERR_OOM = 8&
Const SE_ERR_DLLNOTFOUND = 32&
Const SE_ERR_SHARE = 26&
Const SE_ERR_ASSOCINCOMPLETE = 27&
Const SE_ERR_DDETIMEOUT = 28&
Const SE_ERR_DDEFAIL = 29&
Const SE_ERR_DDEBUSY = 30&
Const SE_ERR_NOASSOC = 31&
Const ERROR_BAD_FORMAT = 11&

Function StartDoc(DocName As String) As Long
Dim Scr_hDC As Long
Scr_hDC = GetDesktopWindow()
StartDoc = shellexecute(Scr_hDC, &quot;Print&quot;, DocName, _
&quot;&quot;, &quot;C:\&quot;, 3)
End Function
******
Private Sub Command1_Click()
Dim r As Long, msg As String
r = StartDoc(&quot;C:\Data\BS&quot;)
If r <= 32 Then
'There was an error
Select Case r
Case SE_ERR_FNF
msg = &quot;File not Found&quot;
Case SE_ERR_PNF
msg = &quot;Path not Found&quot;
Case SE_ERR_ACCESSDENIED
msg = &quot;Access Denied&quot;
Case SE_ERR_COM
msg = &quot;Out of memory&quot;
Case SE_ERR_DLLNOTFOUND
msg = &quot;DLL not found&quot;
Case SE_ERR_SHARE
msg = &quot;A Sharing violation occurred&quot;
Case SE_ERR_ASSOCINCOMPLETE
msg = &quot;Incomplete or invalid file association&quot;
Case SE_ERR_DDETIMEOUT
msg = &quot;DDE Time out&quot;
Case SE_ERR_DDEFAIL
msg = &quot;DDE transaction failed&quot;
Case SE_ERR_DDEBUSY
msg = &quot;DDE busy&quot;
Case SE_ERR_NOASSOC
msg = &quot;No association for file extension&quot;
Case ERROR_BAD_FORMAT
msg = &quot;Invalid EXE file or error in EXE&quot;
Case Else
msg = &quot;Unknown error&quot;
End Select
MsgBox msg
End If

End Sub
**********************************************************
Thanks for your time on this!
cramd
 
NEVER MIND......
I've been thinking way too much on this......add &quot;.doc&quot; onto my file name and guess what happens........IT WORKS!!
Thanks to everyone on this topic.
cramd
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top