|
本帖最后由 lzqlaj 于 2013-9-19 17:23 编辑
最近给同事用VBA做了个卫生检查汇总的工作簿,为了在装有excel的机子上不设置安全性直接运行工作簿,上网查了一下,要么下载“打开工具”,然后改工作簿的名称;要么每次打开一个,而且不公开源码或者有各种限制。自己搜索资料,写了一个,只能打开xls文件,可以在源码中改成xlsm形式,实现用vb一次打开多个含有宏的xls文件。
用法,“excel打开钥匙”(可以改名)复制到xls文件夹,双击即可,如果只有一个xls文件,无提示自动打开;如果多个会提示是否打开。打开多余10个的xls文件会变慢,电脑死机本人不负责。
vb源码
Dim sSave As String, Ret As Long, r As Long, rtn As Long, kk As Long
Dim fol, fso, fil, fils, s, f, fldr
Private Sub Command1_Click()
Unload Me
For Each fil In fils
s = s & fil.Name
aa = midstr & "\" & fil.Name
If UCase(Right(aa, 3)) = "XLS" Then
songname = aa
i = InStrRev(songname, "\")
If i > 0 Then
bb = Mid(songname, i + 1)
Dim ObjExcel As Object
Set ObjExcel = CreateObject("Excel.Application")
ObjExcel.Workbooks.Open App.Path & songname
ObjExcel.Visible = True
Set ObjExcel = Nothing
End If
End If
Next
End
End Sub
Private Sub Command2_Click()
Unload Me
End Sub
Private Sub Form_Load()
Set fso = CreateObject("Scripting.FileSystemObject")
Set fldr = fso.GetFolder(App.Path)
Set fils = fldr.Files
kk = 0
For Each fil In fils
s = s & fil.Name
aa = midstr & "\" & fil.Name
If UCase(Right(aa, 3)) = "XLS" Then
songname = aa
i = InStrRev(songname, "\")
If i > 0 Then
bb = Mid(songname, i + 1)
kk = kk + 1
End If
End If
Next
If Str(kk) > 1 Then
Label1.Caption = "“" & App.Path & "”有" & Str(kk) & "个.xls文件,这些.xls文件都将被打开!!!," & Chr(13) & "点击“确定”打开,点击“取消”退出。"
Else: Command1_Click
End If
End Sub
|
|