Skip to content

Commit f3a0ef7

Browse files
committed
Add Enable-AllMacros
1 parent c58057e commit f3a0ef7

File tree

1 file changed

+50
-3
lines changed

1 file changed

+50
-3
lines changed

scripts/Enable-VBOM.ps1

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Enable access to the VBA project object model
1+
# Enable access to the VBA project object model and configure macro security settings
22

33
function Enable-VBOM ($App) {
44
Try {
@@ -41,14 +41,58 @@ function Enable-VBOM ($App) {
4141
Write-Output "Error: None of the registry paths for AccessVBOM were found."
4242
}
4343

44-
4544
} Catch {
4645
Write-Output "Failed to enable access to VBA project object model for $App."
4746
Write-Output "Error: $($_.Exception.Message)"
4847
Write-Output "StackTrace: $($_.Exception.StackTrace)"
4948
}
5049
}
5150

51+
function Enable-AllMacros ($App) {
52+
Try {
53+
# Check if the application registry key exists
54+
$AppKeyPath = "Registry::HKEY_CLASSES_ROOT\$App.Application\CurVer"
55+
if (-not (Test-Path $AppKeyPath)) {
56+
Write-Output "Error: The registry path '$AppKeyPath' does not exist."
57+
return
58+
}
59+
60+
# Retrieve the current version
61+
$CurVer = Get-ItemProperty -Path $AppKeyPath -ErrorAction Stop
62+
$Version = $CurVer.'(default)'.replace("$App.Application.", "") + ".0"
63+
64+
# Define possible security paths
65+
$SecurityPaths = @(
66+
"HKCU:\Software\Microsoft\Office\$Version\$App\Security",
67+
"HKLM:\Software\Microsoft\Office\$Version\$App\Security"
68+
)
69+
70+
# Check each path
71+
$Found = $false
72+
foreach ($Path in $SecurityPaths) {
73+
if (Test-Path $Path) {
74+
Write-Output "Found security registry path: $Path"
75+
# Set VBAWarnings to 1 (Enable all macros)
76+
Set-ItemProperty -Path $Path -Name VBAWarnings -Value 1 -ErrorAction Stop
77+
Write-Output "Successfully set macro security level to 'Enable all macros' at $Path."
78+
$Found = $true
79+
}
80+
else {
81+
Write-Output "Security registry path not found: $Path"
82+
}
83+
}
84+
85+
if (-not $Found) {
86+
Write-Output "Error: None of the registry paths for macro security settings were found."
87+
}
88+
89+
} Catch {
90+
Write-Output "Failed to modify macro security settings for $App."
91+
Write-Output "Error: $($_.Exception.Message)"
92+
Write-Output "StackTrace: $($_.Exception.StackTrace)"
93+
}
94+
}
95+
5296
# Get the app name from the argument passed to the script
5397
$AppName = $args[0]
5498

@@ -58,4 +102,7 @@ if (-not $AppName) {
58102
}
59103

60104
Write-Output "Enabling VBOM access for $AppName..."
61-
Enable-VBOM $AppName
105+
Enable-VBOM $AppName
106+
107+
Write-Output "Setting macro security to enable all macros for $AppName..."
108+
Enable-AllMacros $AppName

0 commit comments

Comments
 (0)