Solved

OPEN A FILE

Posted on 1998-07-04
7
184 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
ID: 1464953
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
ID: 1464954
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
ID: 1464955
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Expert Comment

by:dirtdart
ID: 1464956
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
ID: 1464957
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
ID: 1464958
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
ID: 1464959
Thank you so much, I appreciate it.  It works now!


DAvid
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
MS Access - Capture pressed key onclick 4 43
Hide vba in gp 7 113
Error with a code discussed on this page 5 38
Problem to line 23 72
I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

679 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