作者moon52016 (matrix)
看板Visual_Basic
标题[VBA ] 执行阶段错误'50290'
时间Sat Jan 2 00:43:23 2016
各位好
最近第一次使用VBA(之前也没碰过VB),碰上了一些问题:
目的是批量帮excel档案加密,利用sendkeys的做法((就像机器人一样...
(程式码是从网路上修改)
Sub Lock_file()
Dim xlapp As Excel.Application
Dim wbSource As Excel.Workbook
Dim LogFileName As Variant
Dim fname As Variant
Dim x As String, y As String
'取得密码
x = InputBox("请输入保护密码:", "保护密码")
y = InputBox("请输入防写密码:", "防写密码")
Set xlapp = New Excel.Application
'MultiSelect:=True 表示可复选档案
LogFileName = Application.GetOpenFilename( _
FileFilter:="Excel档(*.xlsx),*.txt", _
Title:="请选取档案", MultiSelect:=True)
'判断使用者是否有选取档案,或按取消
If VarType(LogFileName) = vbBoolean Then
Exit Sub
End If
xlapp.Visible = True
For Each fname In LogFileName
Set wbSource = xlapp.Workbooks.Open(CStr(fname))
DoEvents
With xlapp
.SendKeys "{F12}", True
Application.Wait (Now + TimeValue("0:00:02"))
.SendKeys "%L", True
Application.Wait (Now + TimeValue("0:00:01"))
.SendKeys "{DOWN}"
Application.Wait (Now + TimeValue("0:00:01"))
.SendKeys "g", True
Application.Wait (Now + TimeValue("0:00:01"))
.SendKeys x, True
Application.Wait (Now + TimeValue("0:00:01"))
.SendKeys "{TAB}", True
Application.Wait (Now + TimeValue("0:00:01"))
.SendKeys y, True
Application.Wait (Now + TimeValue("0:00:01"))
.SendKeys "{ENTER}", True
Application.Wait (Now + TimeValue("0:00:01"))
.SendKeys x, True
Application.Wait (Now + TimeValue("0:00:01"))
.SendKeys "{ENTER}", True
Application.Wait (Now + TimeValue("0:00:01"))
.SendKeys y, True
Application.Wait (Now + TimeValue("0:00:01"))
.SendKeys "{ENTER}", True
Application.Wait (Now + TimeValue("0:00:01"))
.SendKeys "{ENTER}", True
Application.Wait (Now + TimeValue("0:00:01")) '
End With
Next fname
Set wbSource = Nothing 'xlapp.Quit
End Sub
在执行第一个"按F12"之後就会出错
也测试过了除了第一条按F12以外,其他的都不会有效果
看键盘是"numberlock"亮暗亮暗QQ
已经有在网路上搜寻过,有人是说excel还没准备好,但是还是不知道该怎麽解决
原来的程式码来源:
http://forum.twbts.com/viewthread.php?tid=2723
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 36.225.120.48
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/Visual_Basic/M.1451666606.A.F5B.html