在VB6的默认程序中打开一个文件 [英] Opening a file in its default program from VB6

查看:1246
本文介绍了在VB6的默认程序中打开一个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过按下命令在我的VB6应用程序上打开一个文件。该文件是另一个程序读取的文件类型。我希望它打开它,读取文件类型的程序也应该打开,因为当它发现文件类型时会自动打开。任何人都可以帮助我这是我在VB6的新生(昨天刚刚得到它)。

解决方案

我在你的需求的细节上丢失了一些,但一般来说,从VB6中启动一个文件,你可以使用 ShellExecute 及其相关的API函数。

我不确定最佳参考是在哪里,但是我想你可以开始 nofollow noreferrer>这里。



编辑:

您接近。改变这个:

pre $私人小组smb3_Click()
ShellExecute hWnd,find,C:\\\\\\\\ \\ my.file,vbNullString,vbNullString,SW_SHOWNORMAL
End Sub

对此:

  Private Sub smb3_Click()
ShellExecute hWnd,open,C:\hi\my.file ,vbNullString,vbNullString,SW_SHOWNORMAL
End Sub

请注意我所做的更改:

ShellExecute的第二个参数应该是打开,以使用与您要打开的文件的扩展名(函数的第三个参数)关联的机器上的程序来打开指定的文件。检查我包括的链接。

这导致你需要检查你的机器(以及你将使用你的程序的机器)。文件扩展名 .file 应该与您想要在您的程序中启动的任何程序相关联。

编辑(9月9日)



好的,让我们看看是否可以使用 ShellExecute 的简单实现来工作。



创建一个新的VB6项目(标准EXE)并向Form1添加一个名为smb1的按钮。
$ b

转到窗体的代码视图并复制并粘贴下面的代码(只有这个代码):

pre code $ Option $ Exp

Private Declare Function ShellExecute Libshell32.dll别名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

$ b $ Private Sub smb3_Click()
Debug.Print ShellExecute(hWnd,open,C:\hi\my.txt,vbNullString,vbNullString,1)
End Sub

确保有一个文本文件被调用my.txt放在你的c:\hi文件夹里。



运行程序并点击按钮。它应该使用您在机器上配置的默认文本文件编辑器程序(例如记事本)打开文本文件。

如果不是,请告诉我你得到了什么错误,并在哪一行发生错误。另外,请检查即时窗口。立即窗口,我相信,只要你开始你的程序,只要你的程序正在运行,将保持可见。如果你的代码到达并且通过 ShellExecute 运行,立即窗口将显示调用该函数返回的代码。这将告诉我们很多关于你的问题。



如果代码有效,我们可以考虑改变程序来处理你需要的文件。 C:\\\\\\\\\。



但是,首先在Windows资源管理器中双击文件时会发生什么?如果文件打开,那么我们知道有一个程序,你的系统与扩展名为.file的文件相关联。如果文件没有打开,则需要将程序与.file扩展名相关联。检查Windows帮助了解更多。一旦我们设置好了,在你的测试程序中,将你的smb3 Click事件中的代码改为:

  Debug.Print ShellExecute(hWnd ,open,C:\hi\my.file,vbNullString,vbNullString,1)



运行这个并点击程序。该文件应该打开。如果没有,告诉我你在哪里得到什么错误,并告诉我在即时窗口中有什么(如果有的话)。

如果程序确实打开,那么你需要在按钮的click事件中声明 ShellExecute 你需要的代码以编程方式打开文件。但是你仍然需要做出一些改变,你应该改变。



你必须做的改变是在按钮的click事件中。我们将删除调试代码,所以你最终得到这个:

  ShellExecute hWnd,open,C:\ hi \ my.file,vbNullString,vbNullString,1 

处理 ShellExecute 的错误处理。 ShellExecute 运行时会返回一个代码。此代码可能表示错误。研究你可以在我上面链接的页面上下载的示例程序。其中,程序员将其调用的结果写入一个名为 result 的变量。如果 result 等于或小于32,则表示出现错误。在示例程序中,错误信息显示在错误消息中,但请考虑您可能想要执行的操作。


I want to open a file on my VB6 application by pressing a command. The file is a filetype read by another program. I want it to open it and the program that reads the filetype should open as well because when it finds that filetype it opens automatically. Can anyone help me with this I'm kind of a newb at VB6 (just got it yesterday).

解决方案

I'm kind of lost on the details of your requirement, but in general to launch a file from within VB6 you can use ShellExecute and its related API functions.

I'm not sure where the best reference is for this, but I suppose you can start here.

Edit:
You're close. Change this:

Private Sub smb3_Click()    
    ShellExecute hWnd, "find", "C:\hi\my.file", vbNullString, vbNullString, SW_SHOWNORMAL    
End Sub 

to this:

Private Sub smb3_Click()    
    ShellExecute hWnd, "open", "C:\hi\my.file", vbNullString, vbNullString, SW_SHOWNORMAL    
End Sub

Note what I changed:
The second argument to ShellExecute should be "open" to open the specified file using the program on your machine that's associated with the extension of the file you're trying to open (the function's third argument). Check the link I included.

Which leads to something else you need to check on your machine (and the machines you'll use your program on). The file extension .file should be associated with whatever program you want to launch with your program.

Edit (Sept 9)

OK, let's see if we get the simplist implementation of ShellExecute to work.

Create a new VB6 project (Standard EXE) and add to Form1 one button named smb1.

Go to the form's Code View and copy and paste this code (and only this code):

Option Explicit

Private 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 smb3_Click()
    Debug.Print ShellExecute(hWnd, "open", "C:\hi\my.txt", vbNullString, vbNullString, 1)
End Sub

Make sure there's a text file called my.txt in your "c:\hi" folder.

Run the program and click the button. It should open the text file with the default text file editor program you have configured on your machine (wuch as Notepad).

If it doesn't please tell me what error you get and on which line the error occurs. Also, check the Immediate Window. The Immediate Window I believe will be visible as soon as you start your program and will remain visible as long as your program is running. If your code reaches and runs past the line with ShellExecute, the Immediate Window will display the code the call to that function returned. This will tell us a lot about your problem.

If the code works, we can think about changing the program to work on the file you need it to work on - on "C:\hi\my.file".

But first what happens when you double click the file in Windows Explorer? If the file opens up then we know there's a program that your system associated with files having the .file extension. If the file does not open you need to associate a program with the .file extension. Check Windows help for more. Once we're set, in your test program change the code in your smb3 Click Event to:

Debug.Print ShellExecute(hWnd, "open", "C:\hi\my.file", vbNullString, vbNullString, 1)

Run this and click the program. The file should open. If not, tell me what error you get and where, and tell me what (if anything) is in the Immediate Window.

If the program does open then you have in the button's click event and your declaration of ShellExecute the code you need to open files programmatically. But there's still a change you must make and changes you should make.

The change you must make is in the button's click event. We'll remove the debugging code so you end up with this:

ShellExecute hWnd, "open", "C:\hi\my.file", vbNullString, vbNullString, 1

The changes you should make involve some basic error handling around the call to ShellExecute. ShellExecute returns a code when it runs. This code could indicate an error. Study the sample program you can download on the page I linked above. In it, the programmer writes the result of his call to a variable called result. If result is equal to or less than 32 this indicates an error. In the sample program the error is displayed in an error message but think about what you might want to do.

这篇关于在VB6的默认程序中打开一个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆