Solved

OPEN A FILE

Posted on 1998-07-04
7
182 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

773 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