在PB中如何打开一个文件(如.txt,.doc),就像在资源管理器中双击打开文件一样?

时间:2022-09-29 14:20:27
我 是 如下 做的,
可以通过API函数来实现。
  在应用程序的Global External Functions中定义:
  Function long ShellExecuteA (ulong hwnd, string lpOperation, string lpFile, & string 
lpParameters, string lpDirectory, long nShowCmd) library “shell32.dll”   调用如下:
  String ls_null
  SetNull (ls_null)
  ShellExecuteA(Handle(Parent), ls_null, “c:\doc\hello.txt”, ls_null, ls_null, 1) 
我是在  w_main窗口中写这段函数的 w_main的窗口属性是 midhelp
怎么没有任何显示??
问什么 不行????

20 个解决方案

#1


run("c:\doc\hello.txt")

#2


用RUN命令就可以实现

#3


run("C:\WINDOWS\NOTEPAD.EXE c:\doc\hello.txt")

#4


总是出现这个问题让我很是不解
string ls_temp,ls_op,ls_file 

integer li_temp 

li_temp=3 
setnull(ls_temp) 
ls_op="open" 
ls_file="C:\jfkh.txt" 
//---name.xls
ls_file="C:\tmp\name.xls" 
//ApplicationHandle
ulong ll_handle
ll_handle = handle(this)

long ll_rc
ll_rc = ShellExecuteA(   ll_handle   ,  "open"   ,   ls_file   ,   ls_temp   ,   ls_temp   ,    0   ) 

If ll_rc <= 32 Then 
Choose Case ll_rc 
Case 2 
messageBox("Error", "File not found", StopSign!, OK!, 1) 
Case 3 
messageBox("Error", "Path not found", StopSign!, OK!, 1) 
Case 4 
messageBox("Error", "Access denied", StopSign!, OK!, 1) 
Case 8 
messageBox("Error", "Out of memory", StopSign!, OK!, 1) 
Case 11 
messageBox("Error", "Invalid EXE file or error in EXE image", StopSign!, OK!, 1) 
Case 26 
messageBox("Error", "Sharing violation occurred", StopSign!, OK!, 1) 
Case 27 
messageBox("Error", "Incomplete or invalid file association", StopSign!, OK!, 1) 
Case 28 
messageBox("Error", "DDE Time out", StopSign!, OK!, 1) 
Case 29 
messageBox("Error", "DDE Transaction failed", StopSign!, OK!, 1) 
Case 30 
messageBox("Error", "DDE busy", StopSign!, OK!, 1) 
Case 31 
messageBox("Error", "No association for file extension", StopSign!, OK!, 1) 
Case 32 
messageBox("Error", "DLL not found", StopSign!, OK!, 1) 
Case Else 
messageBox("Error", "Unknown error", StopSign!, OK!, 1) 
End Choose 
End If
最后通过调试发现  ll_rc =2  ,即就是C:\tmp\name.xls文件不存在,但是这个文件确实是存在的呀!!!
将C:\tmp\name.xls复制到资源管理器的地址栏中是可以打开这个文件的,
但是为什么函数调用总是说“File not found”,是什么 原因呢 !!!

#5


run也 ok
但是为什么我的调用ShellExecuteA总是不对呢 ?????

#6


问题的关键是系统不知道用什么来打开C:\tmp\name.xls文件,将execl.exe加上试试,execl.exe路径要正确,如:
ls_file="C:\Program Files\Microsoft Office\OFFICE11\EXCEL.EXE C:\tmp\name.xls" 

#7


你要这样run
run("C:\programfile\office\excel.EXE(excel路径) e:\pb技术\更改字体.xls" );

#8


测试了一下你的代码,为了保险起见,所有代码一字未动(除了声明因为网页的关系产生了几个乱码而稍做调整),且特意建立了一个 c:\tmp\name.xls 文件!结果很令人吃惊,name.xls 被顺利地打开了,因为之前我估计应该是看不到的,因为你的最后一参数是 0,也就是把窗口以隐藏方式打开,想不到竟然显示了!这可能是 excel 的问题吧,我们就不管它了。

总之,你的代码没有问题,唯一的可能就是,你那个 c:\tmp\name.xls 文件真的不存在!或是,在你的机器上这个文件的确是以隐藏方式打开的,而你却误以为没有打开。

#9


FUNCTION ulong ShellExecute( ulong hwnd, ref string lpOperation, ref string lpFile, ref string lpParameters, ref string lpDirectory, ulong nShowCmd ) LIBRARY "shell32.dll" ALIAS FOR "ShellExecuteA" 

ll_rc = ShellExecute( ll_handle , ls_op, ls_file, ls_temp, ls_temp, 1 )

#10


run("C:\programfile\office\excel.EXE(excel路径) e:\pb技术\更改字体.xls" );
也不行

#11


"C:\program file\office\"是execl.exe的路径,你需根据你的计算机中execl的安装位置更改,用搜索文件(execl.exe)进行搜索,
搜到后,查看其属性,得到其路径.

#12


是不是我的pb环境的问题,现在run("C:\WINDOWS\NOTEPAD.EXE c:\doc\hello.txt")
根本没有任何反应,怎么回事???
我在w_main窗口的 open时间中写入的上面代码,没反应

#13


怎么回事?

#14


integer i 
i = run("C:\WINDOWS\NOTEPAD.EXE c:\doc\hello.txt") 

看i的值,1成功,-1失败

#15


我调试 后 发现:i=-1,
怎么回事??

#16


建议一步步来,用排除法:

1. Run("NOTEPAD.EXE") 看能否打开,若不能,说明系统有问题!杀毒或重装!

2. Run("NOTEPAD.EXE c:\boot.ini") 看能否打开,若不能,处理方式同上!

3. FUNCTION ulong ShellExecute( ulong hwnd, string lpOperation, string lpFile, string lpParameters, string lpDirectory, ulong nShowCmd ) LIBRARY "shell32.dll" ALIAS FOR "ShellExecuteA" 

string ls_null

SetNull(ls_null)
ShellExecute(0, "open", "c:\boot.ini", ls_null, ls_null, 1)

看能否打开,若不能,重装 pb 再试,还不能,处理方式同上!

#17


应该不会出现这样的问题,是不是你路径设置得不对?

#18


分别检查文件是否存在
boolean b_exist
b_exist = FileExists("C:\WINDOWS\NOTEPAD.EXE")

b_exist = FileExists("c:\doc\hello.txt")
如果文件都存在(b_exist = true),那可能就是系统有问题了

#19


為什麼不用OLE控件了,不是單純多了嗎,不管是WORD,TXT,PPT或是AVI,JPG,PDF...,在PB範例中有詳細的解釋的,
很多問題去看範例就知如何作了

#20


在dos提示符行中,运行 NOTEPAD.EXE c:\boot.ini
没问题,
但是在程序中 Run("NOTEPAD.EXE c:\boot.ini") 却不能打开,
看来是我的系统的问题了!!!

PB环境我重装一下吧!!

#1


run("c:\doc\hello.txt")

#2


用RUN命令就可以实现

#3


run("C:\WINDOWS\NOTEPAD.EXE c:\doc\hello.txt")

#4


总是出现这个问题让我很是不解
string ls_temp,ls_op,ls_file 

integer li_temp 

li_temp=3 
setnull(ls_temp) 
ls_op="open" 
ls_file="C:\jfkh.txt" 
//---name.xls
ls_file="C:\tmp\name.xls" 
//ApplicationHandle
ulong ll_handle
ll_handle = handle(this)

long ll_rc
ll_rc = ShellExecuteA(   ll_handle   ,  "open"   ,   ls_file   ,   ls_temp   ,   ls_temp   ,    0   ) 

If ll_rc <= 32 Then 
Choose Case ll_rc 
Case 2 
messageBox("Error", "File not found", StopSign!, OK!, 1) 
Case 3 
messageBox("Error", "Path not found", StopSign!, OK!, 1) 
Case 4 
messageBox("Error", "Access denied", StopSign!, OK!, 1) 
Case 8 
messageBox("Error", "Out of memory", StopSign!, OK!, 1) 
Case 11 
messageBox("Error", "Invalid EXE file or error in EXE image", StopSign!, OK!, 1) 
Case 26 
messageBox("Error", "Sharing violation occurred", StopSign!, OK!, 1) 
Case 27 
messageBox("Error", "Incomplete or invalid file association", StopSign!, OK!, 1) 
Case 28 
messageBox("Error", "DDE Time out", StopSign!, OK!, 1) 
Case 29 
messageBox("Error", "DDE Transaction failed", StopSign!, OK!, 1) 
Case 30 
messageBox("Error", "DDE busy", StopSign!, OK!, 1) 
Case 31 
messageBox("Error", "No association for file extension", StopSign!, OK!, 1) 
Case 32 
messageBox("Error", "DLL not found", StopSign!, OK!, 1) 
Case Else 
messageBox("Error", "Unknown error", StopSign!, OK!, 1) 
End Choose 
End If
最后通过调试发现  ll_rc =2  ,即就是C:\tmp\name.xls文件不存在,但是这个文件确实是存在的呀!!!
将C:\tmp\name.xls复制到资源管理器的地址栏中是可以打开这个文件的,
但是为什么函数调用总是说“File not found”,是什么 原因呢 !!!

#5


run也 ok
但是为什么我的调用ShellExecuteA总是不对呢 ?????

#6


问题的关键是系统不知道用什么来打开C:\tmp\name.xls文件,将execl.exe加上试试,execl.exe路径要正确,如:
ls_file="C:\Program Files\Microsoft Office\OFFICE11\EXCEL.EXE C:\tmp\name.xls" 

#7


你要这样run
run("C:\programfile\office\excel.EXE(excel路径) e:\pb技术\更改字体.xls" );

#8


测试了一下你的代码,为了保险起见,所有代码一字未动(除了声明因为网页的关系产生了几个乱码而稍做调整),且特意建立了一个 c:\tmp\name.xls 文件!结果很令人吃惊,name.xls 被顺利地打开了,因为之前我估计应该是看不到的,因为你的最后一参数是 0,也就是把窗口以隐藏方式打开,想不到竟然显示了!这可能是 excel 的问题吧,我们就不管它了。

总之,你的代码没有问题,唯一的可能就是,你那个 c:\tmp\name.xls 文件真的不存在!或是,在你的机器上这个文件的确是以隐藏方式打开的,而你却误以为没有打开。

#9


FUNCTION ulong ShellExecute( ulong hwnd, ref string lpOperation, ref string lpFile, ref string lpParameters, ref string lpDirectory, ulong nShowCmd ) LIBRARY "shell32.dll" ALIAS FOR "ShellExecuteA" 

ll_rc = ShellExecute( ll_handle , ls_op, ls_file, ls_temp, ls_temp, 1 )

#10


run("C:\programfile\office\excel.EXE(excel路径) e:\pb技术\更改字体.xls" );
也不行

#11


"C:\program file\office\"是execl.exe的路径,你需根据你的计算机中execl的安装位置更改,用搜索文件(execl.exe)进行搜索,
搜到后,查看其属性,得到其路径.

#12


是不是我的pb环境的问题,现在run("C:\WINDOWS\NOTEPAD.EXE c:\doc\hello.txt")
根本没有任何反应,怎么回事???
我在w_main窗口的 open时间中写入的上面代码,没反应

#13


怎么回事?

#14


integer i 
i = run("C:\WINDOWS\NOTEPAD.EXE c:\doc\hello.txt") 

看i的值,1成功,-1失败

#15


我调试 后 发现:i=-1,
怎么回事??

#16


建议一步步来,用排除法:

1. Run("NOTEPAD.EXE") 看能否打开,若不能,说明系统有问题!杀毒或重装!

2. Run("NOTEPAD.EXE c:\boot.ini") 看能否打开,若不能,处理方式同上!

3. FUNCTION ulong ShellExecute( ulong hwnd, string lpOperation, string lpFile, string lpParameters, string lpDirectory, ulong nShowCmd ) LIBRARY "shell32.dll" ALIAS FOR "ShellExecuteA" 

string ls_null

SetNull(ls_null)
ShellExecute(0, "open", "c:\boot.ini", ls_null, ls_null, 1)

看能否打开,若不能,重装 pb 再试,还不能,处理方式同上!

#17


应该不会出现这样的问题,是不是你路径设置得不对?

#18


分别检查文件是否存在
boolean b_exist
b_exist = FileExists("C:\WINDOWS\NOTEPAD.EXE")

b_exist = FileExists("c:\doc\hello.txt")
如果文件都存在(b_exist = true),那可能就是系统有问题了

#19


為什麼不用OLE控件了,不是單純多了嗎,不管是WORD,TXT,PPT或是AVI,JPG,PDF...,在PB範例中有詳細的解釋的,
很多問題去看範例就知如何作了

#20


在dos提示符行中,运行 NOTEPAD.EXE c:\boot.ini
没问题,
但是在程序中 Run("NOTEPAD.EXE c:\boot.ini") 却不能打开,
看来是我的系统的问题了!!!

PB环境我重装一下吧!!

#21