@@ -108,22 +108,69 @@ if ($officeAppName -eq "Excel") {
108
108
$doc = $officeApp.Presentations.Open ($outputFilePath )
109
109
} elseif ($officeAppName -eq " Access" ) {
110
110
try {
111
- if ($officeApp.CurrentDb -ne $null ) {
112
- $officeApp.CloseCurrentDatabase ()
111
+ # Check if the file exists and is accessible
112
+ if (-not (Test-Path $outputFilePath )) {
113
+ Write-Host " 🔴 Error: Access database file not found: $outputFilePath "
114
+ exit 1
113
115
}
114
116
115
- # Set default database properties
116
- $officeApp.DefaultOpenExclusive = $false # Initially try shared mode
117
+ # Close any existing database
118
+ try {
119
+ if ($null -ne $officeApp.CurrentDb ) {
120
+ $officeApp.CloseCurrentDatabase ()
121
+ Start-Sleep - Seconds 1
122
+ }
123
+ } catch {
124
+ # Ignore errors when checking CurrentDb - it may throw if no database is open
125
+ }
117
126
118
- # First attempt
127
+ # First attempt - normal open
128
+ Write-Host " Attempting to open Access database in shared mode..."
119
129
$doc = $officeApp.OpenCurrentDatabase ($outputFilePath )
120
130
121
- # If we got $null or can't modify VBA project, try exclusive mode
122
- if ($null -eq $doc -or $null -eq $doc.VBProject ) {
123
- $officeApp.CloseCurrentDatabase ()
124
- Start-Sleep - Seconds 1
125
- $officeApp.DefaultOpenExclusive = $true # Try exclusive mode
126
- $doc = $officeApp.OpenCurrentDatabase ($outputFilePath )
131
+ # Check if VBA project is accessible
132
+ $vbaAccessible = $false
133
+ try {
134
+ if ($null -ne $doc -and $null -ne $doc.VBProject ) {
135
+ $vbaAccessible = $true
136
+ Write-Host " VBA Project accessible in shared mode"
137
+ }
138
+ } catch {
139
+ Write-Host " Cannot access VBA Project in shared mode: $ ( $_.Exception.Message ) "
140
+ }
141
+
142
+ # If VBA project isn't accessible, try reopening with different flags
143
+ if (-not $vbaAccessible ) {
144
+ Write-Host " Attempting to reopen database with exclusive access..."
145
+ try {
146
+ $officeApp.CloseCurrentDatabase ()
147
+ Start-Sleep - Seconds 1
148
+
149
+ # Try a different approach for exclusive access
150
+ # Some versions of Access use different methods
151
+
152
+ # For newer Access versions
153
+ $officeApp.OpenCurrentDatabase ($outputFilePath , $true ) # $true = exclusive mode
154
+ $doc = $officeApp.CurrentDb
155
+ } catch {
156
+ Write-Host " Failed with exclusive flag: $ ( $_.Exception.Message ) "
157
+
158
+ # For older Access versions - one last attempt
159
+ try {
160
+ $officeApp.Quit ()
161
+ Start-Sleep - Seconds 2
162
+ $officeApp = New-Object - ComObject " Access.Application"
163
+ $officeApp.Visible = $true
164
+
165
+ # Open in exclusive mode
166
+ # Use a slightly different technique as a last resort
167
+ $officeApp.OpenCurrentDatabase ($outputFilePath , $true )
168
+ $doc = $officeApp.CurrentDb
169
+ } catch {
170
+ Write-Host " 🔴 Error: Failed to open the database after multiple attempts: $ ( $_.Exception.Message ) "
171
+ exit 1
172
+ }
173
+ }
127
174
}
128
175
}
129
176
catch {
0 commit comments