vba 如何使用密码打开工作簿,禁用事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11580569/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 16:59:08  来源:igfitidea点击:

How to open a Workbook with a password, disable events

excelvbams-accesscopy

提问by Adrian

How to open a Workbook with a password, disable events, and then copy a sheet in background to the second file and save. I need in VBA, working in a MS-Access and Excel files

如何使用密码打开工作簿,禁用事件,然后将后台工作表复制到第二个文件并保存。我需要在 VBA 中使用 MS-Access 和 Excel 文件

I have done this until now, is working.

我一直这样做到现在,正在工作。

Private Sub TestFunction()

'strPath = CurrentDb.Properties(0)
'strPath = Left(strPath, Len(strPath) - Len(Dir(strPath, vbNormal))) & "Temp\"
Dim CopyFrom As Object
Dim CopyTo As Object ''Early binding: Workbook
Dim CopyThis As Object
Dim xl As Object ''Early binding: New Excel.Application

Set xl = CreateObject("Excel.Application")
xl.Visible = True
Set CopyFrom = xl.Workbooks.Open("D:\A01.xls")
'CopyFrom.EnableEvents = False
Set CopyThis = CopyFrom.Sheets(1) ''Sheet number 1
Set CopyTo = xl.Workbooks.Open("D:\PM1.xls")
CopyThis.Copy After:=CopyTo.Sheets(CopyTo.Sheets.Count)
CopyFrom.Close
End Sub

This opens the Excel, I enter the pass copy's the sheet to second File.

这将打开 Excel,我将传递副本的工作表输入到第二个文件。

But I need to pass the password in background, delete the sheet and save the second file, all in background. Also I need to delete a sheet, without asking me, like :

但是我需要在后台传递密码,删除工作表并保存第二个文件,全部在后台进行。我还需要删除一张表,而不问我,比如:

CopyTo.Sheets("Sheet1").Delete

Thank you

谢谢

回答by JMax

Password issue

密码问题

Be careful to check wether it is an "open file" password (passwordparameter) or a "modify file" password (WriteResPasswordparameter).

小心检查它是“打开文件”密码(password参数)还是“修改文件”密码(WriteResPassword参数)。

Something like:

就像是:

Sub OpenMyFile()
    Workbooks.Open Filename:="Path", Password:="OpenFile", WriteResPassword:="WriteFile"
End Sub

Delete without alert

无提示删除

For your second question, here is what you can do:

对于您的第二个问题,您可以执行以下操作:

Application.DisplayAlerts=False
CopyTo.Sheets("Sheet1").Delete
Application.DisplayAlerts=True