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!

macro on vb 2

Status
Not open for further replies.

Hala73

Programmer
Sep 6, 2003
13
US
hi,
here's the code I used to make vb 6.0 open a file at 'keypress'. When I run it I get an error message saying 'Invalid procedure call or argument', with the Shell command highlighted.

here's the code:
Option Explicit
Private sAppPath As String
Private Sub Command1_KeyPress(KeyAscii As Integer)
Shell(sAppPath, vbMinimizedFocus) As Double
End Sub
Private Sub Form_Load()
sAppPath = "C:\Documents and Settings\chaoui.1\Desktop\apt.doc"
End Sub

When I use the alternative shell syntax:
Shell ()As Double

I get the error prompt "statement invalid outside Type block".

Any suggestions?

Thanks
Hala




 
The Shell Function returns a value that indicates whether or not the function was performed successfull. Therefore the correct syntax should be either:

Dim ReturnValue
ReturnValue = Shell((sAppPath, vbMinimizedFocus)


or

Call Shell((sAppPath, vbMinimizedFocus)

In the latter case the function is (hopefully) executed but the returnvalue is lost to the program.

_________________________________
In theory, there is no difference between theory and practice. In practice, there is. [attributed to Yogi Berra]
 
Thank you rvbasic,

I tried what you suggested, and used a different path too(because it was saying the argument is 'invalid').
I now get another error prompt:
run-time error '53'.

I'd appreciate any suggestions.
Here's my most recent code:

Option Explicit
Private sAppPath As String
Private Sub Command1_KeyPress(KeyAscii As Integer)
Call Shell(sAppPath, vbMinimizedFocus)
End Sub
Private Sub Form_Load()
sAppPath = "C:\Documents and Settings\All Users\Start Menu\Programs\Microsoft Word"
End Sub
 

Hi:

Your sAppPath should include the name of an executable file, such as "winword.exe", at the end.

Cassie
 
thanks for the help, it works!

now I need help with 2 other things:
1. I need my program to find the location of an application (since it varies from one computer to the other).

2. I need to be able to type things once I open an application.
I tried the 'selection.typetext' command but it didn't work.

Thanks
Hala
 
hi,

I tried the following code, to have the program find the path of the application I am trying to open with my macro, but an error prompt keeps saying 'object required'.
any suggestions?

the code:

Dim fso, newpath
Set fso = CreateObject("Scripting.FileSystemObject")
newpath = fso.AbsolutePathName(MSNMSGR.EXE)
Print newpath 'to check if it's working

Thanks
Hala

 
Try newpath = fso.GetAbsolutePathName("MSNMSGR.EXE")


_________________________________
In theory, there is no difference between theory and practice. In practice, there is. [attributed to Yogi Berra]
 
It looks like you need the Shellexecute API, which can open or print a file using the file's default application. In a Bas module add the following declaration (it's all on one line):

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


In your code:
Dim X As Long
Dim filename as String
filename = "C:\Fred.doc" 'your full filename and path
X = ShellExecute(Me.Hwnd, "Open", FileName, 0&, 0&, 3)

A forum search on ShellExecute will bring up other examples. For further details see:


________________________________________________________________
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.'
 
hi,
thanks for the tips!
I already know how to open a file (messenger.exe) using shell, and after typing in the file location. What I am trying to do here is not having to type in the location: I want the program to find it.
So far I used this code to find the path, but it returns the wrong one:

Dim fso
Dim newpath As String
Set fso = CreateObject("Scripting.FileSystemObject")
newpath = fso.GetAbsolutePathName("MSNMSGR.EXE")
Print newpath 'here I just get the path of the vb
'executable.

any suggestions are welcome!

thanks
Hala


 
Do you mean open a file or run an application? You asked how to open a file now you want to find an application - I'm lost now!

________________________________________________________________
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.'
 
hi,
I didn't think it made a difference in my current problem to specify if it's a file or an application.
In any case, here's what my program can do so far: it opens an application (msn messenger) using 'call shell', if I manually specify the path. What it doesn't do is find that path on its own which is what I want.
The most recent code I came up with returns a path, but it's the wrong one (it returns the one of the vb executable itself). Come to think of it I should start another thread with a more specific title.

here's the code that return the wrong path.
Dim fso
Dim newpath As String
Set fso = CreateObject("Scripting.FileSystemObject")
newpath = fso.GetAbsolutePathName("MSNMSGR.EXE")
Print newpath 'to check if it's working

Thanks
Hala
 
There are two API functions that return the path/filename of an executable providing it has at least one file extension registered to it. Search here and Google for:

FindExecutable
AssocQueryString

You should get lots of hits. AssocQueryString is easier but requires IE5 or later.

Paul Bent
Northwind IT Systems
 
I finally figured out the code I've been posting questions about!!

What I did is look for the path of the .exe file based on its location in the registry.
It's a portable code that can find the msn 6.0 application on its own and open it!

Option Explicit
Private sAppPath1 As String, sAppPath2 As String
Private Sub Command1_KeyPress(KeyAscii As Integer)
'Call Shell(sAppPath1, vbMinimizedFocus)
Call Shell(sAppPath, vbMinimizedFocus)
End Sub
Private Sub Form_Load()
Dim objWSH As Object
Set objWSH = CreateObject("WScript.Shell")
Dim y As String
y = objWSH.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Components\6378366C4007D1E45ACB03110040FE5C\C838BEBA7A1AD5C47B1EB83441066020")
Print y
sAppPath = y
End Sub

Thanks for the help with some of it!
Hala
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top