当前位置:编程学习 > VB >>

求助 :vb打开文件

我用shell语句
Shell "C:\Program Files\Microsoft Office\OFFICE11\WINWORD.exe 路径\文件名.doc" 来打开文件 还有pdf caj格式的 文件也是这么做的 
但是我这台电脑Microsoft Office在上面的路径,如果程序打包后在其他计算机上打开文件的话 不能确定它的 Microsoft Office或查看pdf caj的程序安装在哪个磁盘里
    我想请高手指点怎样写编程语句 能够调动pdf caj Office 的程序安装再哪个路径,再打开文件 ,这样再别的计算机上也能用,我需要代码 很急啊 谢谢各位!
--------------------编程问答-------------------- 先用SHGetSpecialFolderLocation ,ShellExecute,SHGetPathFromIDList打开特殊文件中的\Program Files(代码可在网上搜到)如果仍没有就只好搜索磁盘了 --------------------编程问答--------------------
Private Declare Function FindExecutable Lib "shell32.dll" Alias "FindExecutableA" (ByVal lpFile As String, ByVal lpDirectory As String, ByVal lpResult As String) As Long
    
  Private Sub Command1_Click()
          Dim sPath     As String * 260
            
          Open App.Path & "\A1B1C1D1.doc" For Output As #1
            
          Close #1
            
          FindExecutable App.Path & "\A1B1C1D1.doc", "", sPath
            
          Kill App.Path & "\A1B1C1D1.doc"
            
          Shell sPath, vbNormalFocus
            
            
  End Sub
--------------------编程问答-------------------- 在注册表中可以获取你要的程序的安装路径。
--------------------编程问答-------------------- Shell App.Path & "\文件名.doc", vbNormalFocus
这一句就可以,打开的是和你程序同路径的文件,
文件名.doc也可以换成其它,如*.exe,*.pdf之类的其它具体文件或可执行程序 --------------------编程问答-------------------- 还是用 FindExecutable() 比较简单。
Option Explicit

Private Declare Function FindExecutable Lib "shell32.dll" Alias "FindExecutableA" _
        (ByVal lpFile As String, ByVal lpDirectory As String, ByVal lpResult As String) As Long
    
Private Sub Command1_Click()
    Dim strExePath$, strDocPath$
    strExePath = String$(260, Chr$(0))
    strDocPath = "X:\Temp\中文简历模板.doc"     '你要打开的文档的完整路径
    FindExecutable strDocPath, "", strExePath
    Shell Left$(strExePath, InStr(1, strExePath, Chr$(0)) - 1) & " " & strDocPath, vbNormalFocus
End Sub

--------------------编程问答--------------------
Option Explicit

Private Declare Function FindExecutable Lib "shell32.dll" Alias "FindExecutableA" _
        (ByVal lpFile As String, ByVal lpDirectory As String, ByVal lpResult As String) As Long
    
Private Sub Command1_Click()
    Dim strExePath$, strDocPath$, i%
    strExePath = String$(260, Chr$(0))
    strDocPath = "X:\Temp\中文简历模板.doc"     '你要打开的文档的完整路径
    FindExecutable strDocPath, "", strExePath
    i = InStr(1, strExePath, Chr$(0)) - 1
    If (i = 0) Then
        MsgBox "没找到关联的程序!", 16, "错误"
        Exit Sub
    End If
    Shell Left$(strExePath, i) & " " & strDocPath, 1
End Sub

--------------------编程问答--------------------
Option Explicit
'*************************************************************************
'**模 块 名:ModOpenFile
'**描    述:打开任意类型文件,可以指定命令行,初始目录,显示方式
'**创 建 人:马大哈
'**日    期:2005年04月29日
'**说    明:紫水晶 版权所有2005 - 2006(C) http://www.m5home.com/
'**版    本:V1.0
'*************************************************************************

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
Public Enum ShowStyle
    vbHide = 0
    vbMaximizedFocus = 1
    vbMinimizedFocus = 2
    vbMinimizedNoFocus = 3
    vbNormalFocus = 4
    vbNormalNoFocus = 5
End Enum

Public Function OpenFile(ByVal OpenName As String, Optional ByVal CmdLine As String = vbNullString, Optional ByVal InitDir As String = vbNullString, Optional ByVal msgStyle As ShowStyle = vbNormalFocus)
    ShellExecute 0&, vbNullString, OpenName, CmdLine, InitDir, msgStyle
End Function

别的用户不一定使用同样的程序打开同一类型文件,所以最理想还是直接调用关联的程序来打开.

比如TXT,一般是记事本,但我是UE,你改了我的习惯,会让我觉得你的软件很不人性化.

上面的代码,直接调用OpenFile("xxx.doc")即可,会自动用关联的程序打开. --------------------编程问答-------------------- 我老是修改文件后缀 嘿嘿 比如电影、AV类的 都是改为火星文件 哈哈哈哈

补充:VB ,  网络编程
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,