Solved

OPEN A FILE

Posted on 1998-07-04
7
179 Views
Last Modified: 2011-09-20
Is there a way by writing code, (without invoking ole), similiar to the shell cmd. to open a file?  I have searched and searched, it seems like it is an easy enough request, any ideas?


0
Comment
Question by:SPECIAL
  • 3
  • 3
7 Comments
 
LVL 1

Expert Comment

by:OeilNoir
Comment Utility
What do you mean exactly by openining a file? do you mean to open a .doc file and that word will automaticaly open with it? or do you mean to open a file using the open statement like:
open "c:\test.tst" for output as #1

?


0
 
LVL 5

Accepted Solution

by:
dirtdart earned 50 total points
Comment Utility
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

hwnd is the hWnd of the parent form (can be left null).  lpOperation should be "open" (with the quotes) to open a file and its associated program.  lpFile is the complete path and filename.  lpParameters is any command line parameters that the program needs.  lpDirectory is the default starting directory for the program, nShowCmd is how to show the window when it is started (usually SW_SHOW or SW_SHOWNORMAL).
0
 

Author Comment

by:SPECIAL
Comment Utility
I appreciate your help.  I am a bit confused on how I am to carry this out.  Here's what I did, I have error messages.

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()

Set lpOperation = "OPEN"
Set nShowCmd = SW_SHOW
lpDirectory = "C:\WINDOWS\DESKTOP"


Set lpFile = "C:\WINDOWS\DESKTOP\DX.TXT"


'hwnd is the hWnd of the parent form (can be left null). 
' lpOperation should be "open" (with the quotes) to open a file and its associated program. 
' lpFile is the complete path and filename. 
  'lpParameters is any command line parameters that the program needs. 
  'lpDirectory is the default starting directory for the program,
'nShowCmd is how to show the window when it is started (usually SW_SHOW or SW_SHOWNORMAL).
End Sub

Can You correct this, assume the file i want to open is C:\book1.xls

Thanks for your help



0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 5

Expert Comment

by:dirtdart
Comment Utility
Public Const SW_SHOW = 5
Public Const SW_SHOWNORMAL = 1

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

Dim sFileName As String
Dim e As Long

sFileName = "c:\book1.xls"

e = ShellExecute(0, "open", sFileName, "", "", SW_SHOW)

If e = 0 Then
    Dim msg As String
    msg = CStr(err.Number) & vbCrLf
    msg = CStr(err.Description)
    MsgBox msg
End If

The code above will trap any errors and give the error description and number.  If it doesn't work, post the description and number given by this code, and I'll see if I can figure it out.



0
 

Author Comment

by:SPECIAL
Comment Utility
Hi,

What is happening, I am getting the same error message:

it says, "comments may appear only after end sub, end property, or end function.

This is the problem statement:
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

if I put it in the declarations area, it says invalid outside procedure.

If you need more points, I can Reopen the question, just let me know,

David
0
 
LVL 5

Expert Comment

by:dirtdart
Comment Utility
Nah.  I don't do this for the points anyway (they're nice, but not my reason!).  OK.  The problem isn't with your coding, it's with the placement of your code.  If you are going to use an API call in a VB program, it needs to be placed in a module (along with any constants or types that are going to be used by it).  So cut the code for the declaration and constants from your form, add a module to your program, and paste the code into the module you added.  That should take care of any problems.
0
 

Author Comment

by:SPECIAL
Comment Utility
Thank you so much, I appreciate it.  It works now!


DAvid
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now