標題:
[解決了,改為分享] 用VBA批次幫其他excel 的VB專案設定密碼鎖定
[打印本頁]
作者:
diabo
時間:
2011-2-26 20:55
標題:
[解決了,改為分享] 用VBA批次幫其他excel 的VB專案設定密碼鎖定
本帖最後由 diabo 於 2011-2-28 00:31 編輯
一直無法成功,請大家幫忙看看哪裡有問題?
Sub Lock_VBA()
Dim xlapp As Excel.Application
Dim wbSource As Excel.Workbook
Dim LogFileName As Variant
Dim fname As Variant
Dim VBA_PWD As String, XLS_PWD As String
'取得密碼
VBA_PWD = [c4] 'VBA password
Set xlapp = New Excel.Application
'MultiSelect:=True 表示可複選檔案
LogFileName = Application.GetOpenFilename( _
FileFilter:="Excel檔(*.xls),*.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))
'專案沒有保護
If wbSource.VBProject.Protection = 0 Then
DoEvents
With xlapp
.SendKeys "%{F11}" 'Alt + F11 切換到VBA視窗
.SendKeys "%Te" 'ALT + T 工具(繁體中文是(T))-VBproject屬性(E)
.SendKeys "^{TAB}" '切換到下一頁面 VBproject屬性(E)-保護
.SendKeys "{+}" '{+}/{-}選取/取消「鎖定專案以供檢視」
.SendKeys "{TAB}", False '移到密碼欄位
.SendKeys VBA_PWD & "{TAB}", True '輸入密碼
.SendKeys VBA_PWD '確認密碼
.SendKeys "{TAB}{ENTER}" '按確定鈕(預設值)
.SendKeys "%q" '
End With
'存檔後關閉該檔案
wbSource.Save
wbSource.Close SaveChanges:=True
'MsgBox Dir(fname) & " 專案鎖定完成"
Else
'不存檔後關閉該檔案
wbSource.Close SaveChanges:=False
'MsgBox Dir(fname) & " 專案已經鎖定,請解鎖後再執行"
End If
Next fname
Set wbSource = Nothing xlapp.Quit
End Sub
複製代碼
[attach]4878[/attach]
作者:
kimbal
時間:
2011-2-27 10:41
一直無法成功,請大家幫忙看看哪裡有問題?
diabo 發表於 2011-2-26 20:55
sendkeys需要配合版面顯示快速度,試試加些暫停的代碼
With xlapp
.SendKeys "%{F11}" 'Alt + F11 切換到VBA視窗
Application.Wait (Now + TimeValue("0:00:01"))
.SendKeys "%Te" 'ALT + T 工具(繁體中文是(T))-VBproject屬性(E)
Application.Wait (Now + TimeValue("0:00:01"))
.SendKeys "^{TAB}" '切換到下一頁面 VBproject屬性(E)-保護
Application.Wait (Now + TimeValue("0:00:01"))
.SendKeys "{+}" '{+}/{-}選取/取消「鎖定專案以供檢視」
Application.Wait (Now + TimeValue("0:00:01"))
.SendKeys "{TAB}", False '移到密碼欄位
Application.Wait (Now + TimeValue("0:00:01"))
.SendKeys VBA_PWD & "{TAB}", True '輸入密碼
Application.Wait (Now + TimeValue("0:00:01"))
.SendKeys VBA_PWD '確認密碼
Application.Wait (Now + TimeValue("0:00:01"))
.SendKeys "{TAB}{ENTER}" '按確定鈕(預設值)
Application.Wait (Now + TimeValue("0:00:01"))
.SendKeys "%q" '
End With
複製代碼
作者:
diabo
時間:
2011-2-28 00:30
謝謝 kimbal ,果然是這要命的8秒.......
現在 OK 了....
歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)