请教VB高手,麻烦帮我解决一下!

dengnansky 2008-07-27 04:54:31
我想用VB做一个查询系统,功能是在任意路径下找到该word或excel文件后打开该文件,程序代码如下:但打开文件的功能还是无法实现,请高手指点!

rivate Sub Command1_Click()
Dim SearchedPath As String
Dim SearchedFile As String

SearchedPath = "C:\"
SearchedFile = InputBox("输入要搜索的文件名" + Chr(13) + Chr(10) + "例如*.doc abc.*")
'FirstSearch = True
AllSearch SearchedPath, SearchedFile
Text1.Text = SearchedPath
End Sub


Private Sub Command2_Click()
'调用Word文档
FileName = "" '这里还没调试出来!
OLE1.SourceDoc = FileName
OLE1.Action = 1
OLE1.Action = 7
End Sub

Private Sub Command3_Click()
Unload Me
End Sub

添加模块代码为
Public Const MAX_PATH = 260
Public Const FILE_ATTRIBUTE_DIRECTORY = &H10

Public Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type

Public Type WIN32_FIND_DATA
dwFileAttributes As Long
ftCreationTime As FILETIME
ftLastAccessTime As FILETIME
ftLastWriteTime As FILETIME
nFileSizeHigh As Long
nFileSizeLow As Long
dwReserved0 As Long
dwReserved1 As Long
cFileName As String * MAX_PATH
cAlternate As String * 14
End Type

Public Declare Function FindFirstFile Lib "kernel32" Alias _
"FindFirstFileA" (ByVal lpFileName As String, _
lpFindFileData As WIN32_FIND_DATA) As Long
Public Declare Function FindNextFile Lib "kernel32" Alias _
"FindNextFileA" (ByVal hFindFile As Long, _
lpFindFileData As WIN32_FIND_DATA) As Long
Public Declare Function FindClose Lib "kernel32" (ByVal _
hFindFile As Long) As Long
Public Declare Function SetCurrentDirectory Lib "kernel32" Alias _
"SetCurrentDirectoryA" (ByVal lpPathName As String) As Long

Sub AllSearch(sPath As String, sFile As String)
Dim xf As WIN32_FIND_DATA
Dim ff As WIN32_FIND_DATA
Dim findhandle As Long
Dim lFindFile As Long
Dim astr As String
Dim bstr As String

lFindFile = FindFirstFile(sPath + "\" + sFile, ff)
If lFindFile > 0 Then
Do
Form1.List1.AddItem ff.cFileName
Loop Until (FindNextFile(lFindFile, ff) = 0)
FindClose lFindFile
End If

astr = sPath + "\" + "*.*"
findhandle = FindFirstFile(astr, xf)
DoEvents
Do
If xf.dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY Then
If Asc(xf.cFileName) <> Asc(".") Then
bstr = sPath + "\" + Left$(xf.cFileName, InStr(xf.cFileName, Chr(0)) - 1)
AllSearch bstr, sFile
End If
End If
xf.cFileName = ""
Loop Until (FindNextFile(findhandle, xf) = 0)
FindClose findfile
End Sub
...全文
89 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
dnizoy 2008-07-27
  • 打赏
  • 举报
回复
意见同楼上
weberli 2008-07-27
  • 打赏
  • 举报
回复
个人意见,没必要用到这么多API函数,在引用里面直接应用excel9.0,和word,然后一个commanddilog对话框显示打开你选择的excel或word文件,网上搜索一下,很多例子

7,762

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧