我有一个项目,我必须在一个文件夹中搜索超过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