vba 通过 Powershell 启用宏

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

Enable Macros via Powershell

excelvbapowershell

提问by bordeltabernacle

I have a Powershell script that creates an Excel File, and within that runs a macro to embed an OLEObject. This is going to be used by field engineers and therefore on different computers. To run the script, the macro settings need to be set to "Enable all macros..." and "Trust access to the VBA project object model." Is there any way this can be incorporated into the Powershell script? Ideally to be activated and then deactivated at the end. I suspect this is something that can't be automated, and will need to be done manually to run the script. Any suggestions would be much appreciated.

我有一个创建 Excel 文件的 Powershell 脚本,并在其中运行一个宏来嵌入 OLEObject。这将由现场工程师使用,因此在不同的计算机上。要运行脚本,需要将宏设置设置为“启用所有宏...”和“信任对 VBA 项目对象模型的访问”。有什么方法可以将其合并到 Powershell 脚本中吗?理想情况下是激活然后在最后停用。我怀疑这是无法自动化的,需要手动完成才能运行脚本。任何建议将不胜感激。

回答by expirat001

The run method can be used to run either a macro or a function.

run 方法可用于运行宏或函数。

$app = $excel.Application
$app.Run("Macro_1")
$app.Run("Macro_2")
$app.Run("Macro_3")

To enable/disable via Regedit :

通过 Regedit 启用/禁用:

Registry Key to change: 
[HKEY_CURRENT_USER\Software\Microsoft\Office.0\Excel\Security]
"VBAWarnings"=dword:00000001

Registry Option Values:
 ?1 = Enable all macros (not recommended; potentially dangerous code can run)
 ?2 = Disable all macros except digitally signed macros
 ?3 = Disable all macros with notification
 ?4 = Disable all macros without notification