1
- # Enable access to the VBA project object model
1
+ # Enable access to the VBA project object model and configure macro security settings
2
2
3
3
function Enable-VBOM ($App ) {
4
4
Try {
@@ -41,14 +41,58 @@ function Enable-VBOM ($App) {
41
41
Write-Output " Error: None of the registry paths for AccessVBOM were found."
42
42
}
43
43
44
-
45
44
} Catch {
46
45
Write-Output " Failed to enable access to VBA project object model for $App ."
47
46
Write-Output " Error: $ ( $_.Exception.Message ) "
48
47
Write-Output " StackTrace: $ ( $_.Exception.StackTrace ) "
49
48
}
50
49
}
51
50
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
+
52
96
# Get the app name from the argument passed to the script
53
97
$AppName = $args [0 ]
54
98
@@ -58,4 +102,7 @@ if (-not $AppName) {
58
102
}
59
103
60
104
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