Solved

OPEN A FILE

Posted on 1998-07-04
7
183 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

856 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