Solved

OPEN A FILE

Posted on 1998-07-04
7
185 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
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

Independent Software Vendors: 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!

Question has a verified solution.

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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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…
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…

752 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