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

Shell execute 3

Status
Not open for further replies.

Imbriani

Programmer
Jan 9, 2004
195
US
Hi all,

I have the following code in my module:

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

Under the click event of a button I have:

ShellExecute &0, "Open", txtpdffile.Text, vbNullString, "", SW_SHOWNORMAL

In txtpdffile, I"ve typed the name of a file I want to open in Acrobat. Zemp and johnwm helped me with this a while back. This code won't run and I'm stumped. When I click the button Vb finds an error with the .Text part of the textbox name, with SW_SHOWNORMAL and changes the code to:

ShellExecute &O0, "Open", txtpdffile.Text, vbNullString, "", SW_SHOWNORMAL

Any ideas, anyone?
 
This seems to work for me without any problem:

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()
ShellExecute &O0, "Open", txtpdffile.Text, vbNullString, "", 1
End Sub

Swi
 
I've just done this myself. For some reason, VB changed the zero to 2 zeros (no O's as your text above).

Also, if SW_SHOWNORMAL is not declared, as it isn't by default in VB, you need to declare it or replace it with the value :

(Taken directly from windows.pas, delphi unit.)

SW_HIDE = 0;
SW_SHOWNORMAL = 1;
SW_NORMAL = 1;
SW_SHOWMINIMIZED = 2;
SW_SHOWMAXIMIZED = 3;
SW_MAXIMIZE = 3;
SW_SHOWNOACTIVATE = 4;
SW_SHOW = 5;
SW_MINIMIZE = 6;
SW_SHOWMINNOACTIVE = 7;
SW_SHOWNA = 8;
SW_RESTORE = 9;
SW_SHOWDEFAULT = 10;
SW_MAX = 10;


Also, if you've declared the function in a module (as I assume you have done), you need to declare it as public or your forms cannot use it.

I've just had it working with your original code and the above comments, so have fun!

John.

mf_of_john.gif
 
Thanks for your help.

I have the Private Declare in a class module. When I try to change it to public, I get the error message that the code is not allowable in a public declare.

This might be a stupid question, but do I just put the code starting with ShellExecute under the button click? Is there anything I need to put there?
 
Put the following code under 'Option Explicit' at the top of the code:

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


Then you should be able to call it as such:

Private Sub Command1_Click()
ShellExecute &O0, "Open", txtpdffile.Text, vbNullString, "", 1
End Sub

Swi
 
This is what I get when I've done all that:

Compile error:

Method of data member not found

It stops at the .Text part of txtpdffile.Text:

Private Sub cmdOpenDoc_Click()

ShellExecute &O0, "Open", txtpdffile.Text, vbNullString, "", 1

End Sub
 
Create a new project and place the textfield and button on the form.

Add a module and add the declaration to it (ensure that this is a public declaration, or create a public function in the same module that runs the private one.)

Add the onClick code....

ShellExecute 0,"OPEN",txtpdffile.Text,"","",1


That's it!

John.

mf_of_john.gif
 
If it complains about the .text property then check the spelling of txtpdffile.Text, and check that it's on the same form as the Command1 button

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'If we're supposed to work in Hex, why have we only got A fingers?'
 
Goto the properties page of the textbox txtpdffile and remove the 0 from the Index property or whatever number may be there.

"Two strings walk into a bar. The first string says to the bartender: 'Bartender, I'll have a beer. u.5n$x5t?*&4ru!2[sACC~ErJ'. The second string says: 'Pardon my friend, he isn't NULL terminated'."
 
>This is what I get...
>...Method of data member not found


1) Make sure that Option Explicit is set
2) Ensure that you don't have more than one (potentially in scope) variable with the same name but of a different type...
 
I'm guessing he's referencing a control array.

"Two strings walk into a bar. The first string says to the bartender: 'Bartender, I'll have a beer. u.5n$x5t?*&4ru!2[sACC~ErJ'. The second string says: 'Pardon my friend, he isn't NULL terminated'."
 
This may not apply, but just to cover all bases...if this is in a class module, you should precede your txtpdffile.Text with the form name, such as frmMain.txtpdfile.text.
 
It works! Thanks to you all for your advice. I checked all that you said and what I did was copy the Private Sub statement from the class module and paste it into the Option Explicit of the General Declarations of the form code itself. I thought I'd already done this, but I guess not. Is there a way to put this in the class module as public?

Another question. At the same spot of the form when data is being entered, I want to open a common dialog box, choose a file and have the name of the file put into the txtpdffile control. I know to do Form1.Commondialog1.ShowOpen to get the file name, but what do I do to get it to shove the name into the control?

 
txtpdffile.text = Commondialog1.filename

"Two strings walk into a bar. The first string says to the bartender: 'Bartender, I'll have a beer. u.5n$x5t?*&4ru!2[sACC~ErJ'. The second string says: 'Pardon my friend, he isn't NULL terminated'."
 
All of your help has been absolutely wonderful! AS a newbie, I've been struggling with these two things for weeks now. Thanks again.

Kathy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top