当我尝试保护 VBProject 并将工作簿作为附件通过电子邮件发送时,我收到了一个自动化错误。我在这里想念什么?
这是保护 VB 项目的代码:
Sub ProtectVBProject(WB As Workbook, ByVal strPassWord As String)
Dim vbProj As Object
Set vbProj = WB.VBProject
'Is it already locked!
If vbProj.Protection = 1 Then Exit Sub
Set Application.VBE.ActiveVBProject = vbProj
'SendKeys to set the project password
SendKeys "+{TAB}{RIGHT}%V{+}{TAB}" & strPassWord & "{TAB}" & strPassWord & "~"
Application.VBE.CommandBars(1).FindControl(ID:=2578, recursive:=True).Execute
'Close and Save
WB.Close True
End Sub
这是调用 ProtectVBProject 以保护 VB 项目并附加工作簿并将其邮寄的代码
TempFilePath = Environ$("temp") & "\"
TempFileName = "Email Test " & Sourcewb.Name & " " _
& Format(Now, "dd-mmm-yy h-mm-ss")
Set OutApp = CreateObject("Outlook.Application")
OutApp.Session.Logon
Set OutMail = OutApp.CreateItem(0)
With Destwb
.SaveAs TempFilePath & TempFileName & FileExtStr, _
FileFormat:=FileFormatNum
On Error Resume Next
With OutMail
.To = "aab2323@example.com"
.CC = ""
.BCC = ""
.Subject = "Test Subject"
Call ProtectVBProject.ProtectVBProject(Destwb, "pa$$w0rd!")
.Attachments.Add Destwb.FullName
.Send
End With
On Error GoTo 0
.Close SaveChanges:=False
Destwb.Close SaveChanges:=False
End With
Kill TempFilePath & TempFileName & FileExtStr
Set OutMail = Nothing
Set OutApp = Nothing