我有一個項目,我必須在一個文件夾中搜索超過1,000多個excel文件,並查看哪些是受密碼保護的,哪些不是。爲了節省時間,我寫了一個宏來做到這一點,主要內容如下:Excel VBA密碼保護檢查
Sub CheckWbook()
Dim Value As String, a As Single, myfolder as string
With Application.FileDialog(msoFileDialogFolderPicker)
.Show
myfolder = .SelectedItems(1) & "\"
End With
Range("C4") = myfolder
Range("B7:C" & Rows.Count) = ""
a = 0
Value = Dir(myfolder)
Do Until Value = ""
If Value = "." Or Value = ".." Then
Else
If Right(Value, 3) = "xls" Or Right(Value, 4) = "xlsx" Or Right(Value, 4) = "xlsm" Then
On Error Resume Next
Workbooks.Open Filename:=myfolder & Value, Password:="zzzzzzzzzzzz"
If Err.Number > 0 Then
Range("C7").Offset(a, 0).Value = "Yes"
End If
Workbooks(Value).Close False
On Error GoTo 0
Range("B7").Offset(a, 0).Value = Value
a = a + 1
End If
End If
Value = Dir
Loop
End Sub
我遇到的問題是,密碼的彈出仍然存在:它不填寫密碼。任何幫助將不勝感激。 -A
編輯 改變了代碼一點,並獲得過去錯誤消息,但現在我越來越被困在彈出的密碼,即停止從完全工作的宏觀,儘管在錯誤恢復下一個功能。
然後,我遇到了這個代碼,我認爲可以幫助:
Option Explicit
Public Sub ProcessBatch()
Dim strFileName As String
Dim strFilePath As String
Dim oDoc As Document
' Set Directory for Batch Process
strFilePath = "C:\Test\"
' Get Name of First .doc File from Directory
strFileName = Dir$(strFilePath & "*.doc")
While Len(strFileName) <> 0
' Set Error Handler
On Error Resume Next
' Attempt to Open the Document
Set oDoc = Documents.Open(_
FileName:=strFilePath & strFileName, _
PasswordDocument:="?#[email protected]$")
Select Case Err.Number
Case 0
' Document was Successfully Opened
Debug.Print strFileName & " was processed."
Case 5408
' Document is Password-protected and was NOT Opened
Debug.Print strFileName & " is password-protected " & _
"and was NOT processed."
' Clear Error Object and Disable Error Handler
Err.Clear
On Error GoTo 0
' Get Next Document
GoTo GetNextDoc
Case Else
' Another Error Occurred
MsgBox Err.Number & ":" & Err.Description
End Select
' Disable Error Handler
On Error GoTo 0
'-------------------------------------
'-------------------------------------
'---Perform Action on Document Here---
'-------------------------------------
'-------------------------------------
' Close Document
oDoc.Close
' Clear Object Variable
Set oDoc = Nothing
GetNextDoc:
' Get Next Document from Specified Directory
strFileName = Dir$()
Wend
End Sub
但這無法識別沃達柯的文檔。任何想法如何讓它工作?
'Application.DisplayAlerts = False'不起作用,因爲發現由OP編輯。一種方法是將WM_CLOSE消息發送到彈出對話框:在Windows API中使用SendMessage來執行此操作。但不容易設計到您的代碼中。另一種方法*可能是在VBScript中編寫上述代碼,注意保持Excel.Application對象的可見性狀態爲False。我從來沒有嘗試過,所以不能保證彈出對話框不會顯示。 – Bathsheba
這很奇怪,我試過你的宏,它工作得很好。所以問題不在宏觀本身,而在於某些東西。你能更準確地描述,當問題發生時,在哪個迭代中......作爲非相關的提示,我會推薦使用選項Application.ScreenUpdating = False,這可以提高宏的生產力。 – lbeliarl