Excel VBA - 自动输入密码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11326986/
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-08 13:27:06  来源:igfitidea点击:

Excel VBA - Automatically Input Password

excelvbapasswords

提问by iceagle

I want to write an Excel VBA macro to input the password automatically for the user. That way, the user does not need to input the password manually every time they double click on the file. Is this possible? If so, how can I do this?

我想编写一个 Excel VBA 宏来自动为用户输入密码。这样,用户无需在每次双击文件时手动输入密码。这可能吗?如果是这样,我该怎么做?

采纳答案by Siddharth Rout

As mentioned in my comment, the functionality that you seek, in my opinion cannot be done. Let me explain it.

正如我在评论中提到的,您所寻求的功能在我看来是无法完成的。让我解释一下。

To auto run a VBA code in an Excel file you need to place the code either in the Workbook_Open()in the ThisWorkbookcode area or in Auto_Open()in a module.

要在 Excel 文件中自动运行 VBA 代码,您需要将代码放置Workbook_Open()ThisWorkbook代码区域或Auto_Open()模块中。

Now these two Subs execute only after the password has been entered in a password protected file i.e after the workbook has opened. So there is no way this can be run before the password is fed to the password dialog box or before the Workbook is opened.

现在,这两个 Subs 仅在密码被输入到受密码保护的文件中后,即在工作簿打开后才执行。因此,在将密码输入密码对话框或打开工作簿之前,无法运行此操作。

I am sure your boss is a sensible guy and will understand if you can explain it nicely to him :)

我相信你的老板是一个明智的人,如果你能很好地向他解释,他会理解的:)

You might also want to see this link which explains more in details about running the macro automatically.

您可能还想查看此链接,其中详细介绍了有关自动运行宏的详细信息。

Topic: Running a macro when Excel starts

主题:Excel 启动时运行宏

Link: http://office.microsoft.com/en-us/excel-help/running-a-macro-when-excel-starts-HA001034628.aspx

链接http: //office.microsoft.com/en-us/excel-help/running-a-macro-when-excel-starts-HA001034628.aspx

Quote from the above link

从上面的链接引用

If you want to automatically perform certain actions whenever you start Microsoft Excel, you can record or write a macro that will run whenever you open a workbook. There are two ways to do this:

Record a macro and save it using the name Auto_Open. Write the macro as a Microsoft Visual Basic? for Applications (VBA) procedure for the Open event of a workbook.

如果您想在每次启动 Microsoft Excel 时自动执行某些操作,您可以录制或编写一个宏,该宏将在您打开工作簿时运行。有两种方法可以做到这一点:

录制一个宏并使用名称 Auto_Open 保存它。将宏编写为 Microsoft Visual Basic?用于工作簿打开事件的应用程序 (VBA) 过程。

回答by DJ.

There is no command-line parameter to pass in the password. But what you can do is have a "opener" spreadsheet that takes a spreadsheet name and password as parameters and using VBA opens the password-protected spreadsheet.

没有命令行参数可以传递密码。但是您可以做的是拥有一个“opener”电子表格,它将电子表格名称和密码作为参数,并使用 VBA 打开受密码保护的电子表格。

Look at his link:

看他的链接:

https://superuser.com/questions/438842/excel-workbook-desktop-shortcut-with-auto-password

https://superuser.com/questions/438842/excel-workbook-desktop-shortcut-with-auto-password

That only shows you how to get the command-line parameters - once you have those the you can use:

这仅向您展示了如何获取命令行参数 - 一旦您拥有可以使用的参数:

Workbooks.Open "filename", , , , "Password"

回答by Piet93

@iceagle, I do not have enough reputation to "comment" on @(Siddharth Routh), but I regret the comments and the answer for saying it is meaningless/useless/can not be done.
For instance if you have a whole set of excels that you want to alter but are all password protected. If now you run them all via a loop you have to enter the password everytime again (seems stupid when you have 1000+ files no?) A better option: encode the macro of VBS, which has the password in it and can therefor change all the encoded files! Provide the user with this password and now you still have 1000 encoded files only you do not have to enter it everytime.

@iceagle,我没有足够的声誉来“评论”@(Siddharth Routh),但我对评论和回答说它毫无意义/无用/无法完成感到遗憾。
例如,如果您有一整套想要更改但都受密码保护的 excel。如果现在你通过循环运行它们,你必须每次再次输入密码(当你有 1000 多个文件时似乎很愚蠢不是?)更好的选择:编码 VBS 的宏,其中包含密码,因此可以更改所有编码的文件!向用户提供此密码,现在您仍然拥有 1000 个编码文件,只是您不必每次都输入它。

Now for the answer, if people come upon this, is given correctly by @DJ. You can put the password into the command line of the workbook opener like this:

现在的答案是,如果人们遇到这个问题,@DJ 会正确给出。您可以将密码放入工作簿开启器的命令行中,如下所示:

Set wb = Workbooks.Open(Filename:="myfile.xls", Password:="password")

Set wb = Workbooks.Open(Filename:="myfile.xls", Password:="password")

I hope people that come across this do not take the accepted answer as given but look further.

我希望遇到这个问题的人不要把公认的​​答案当作给定的答案,而是看得更远。

Kind regards, Pieter

亲切的问候,彼得