Building custom solutions that extend, automate, and integrate Microsoft 365 apps.
Found a programatic way to remove that confirmation. I hope this helps. In case if you found a better simple way, let me know :)
Clear-Host
$SourcePath = "D:\Worddoc.dotm"
$DestinationPath = "D:\Worddoc_removed.dotm"
if (-not (Test-Path $SourcePath)) {
Write-Host "ERROR: File not found: $SourcePath" -ForegroundColor Red
Read-Host "Press Enter to exit"
exit
}
try {
Write-Host "Opening Word application..." -ForegroundColor Yellow
$Word = New-Object -ComObject Word.Application
$Word.Visible = $false
$Word.DisplayAlerts = 0
# Retrieve PID of the newly created Word instance
$wordProc = Get-Process -Name "WINWORD" | Sort-Object StartTime -Descending | Select-Object -First 1
if (-not $wordProc) {
Write-Host "ERROR: Could not locate WINWORD process." -ForegroundColor Red
return
}
$wordPid = $wordProc.Id
Write-Host "Word process started successfully (PID: $wordPid)" -ForegroundColor Green
Write-Host "Starting UI Automation listener for dialogs..." -ForegroundColor Yellow
$dismissJob = Start-Job -ScriptBlock {
param($targetPid)
Add-Type -AssemblyName UIAutomationClient
Add-Type -AssemblyName UIAutomationTypes
$procCondition = New-Object System.Windows.Automation.PropertyCondition(
[System.Windows.Automation.AutomationElement]::ProcessIdProperty,
$targetPid
)
$maxAttempts = 40
for ($i = 1; $i -le $maxAttempts; $i++) {
$topLevelWindows = [System.Windows.Automation.AutomationElement]::RootElement.FindAll(
[System.Windows.Automation.TreeScope]::Children,
$procCondition
)
$targetButton = $null
foreach ($win in $topLevelWindows) {
$targetButton = $win.FindFirst(
[System.Windows.Automation.TreeScope]::Descendants,
[System.Windows.Automation.AndCondition]::new(
[System.Windows.Automation.PropertyCondition]::new([System.Windows.Automation.AutomationElement]::AutomationIdProperty, "1"),
[System.Windows.Automation.PropertyCondition]::new([System.Windows.Automation.AutomationElement]::ClassNameProperty, "NetUIButton")
)
)
if ($targetButton) { break }
}
if ($targetButton) {
Write-Output "UIA Handler: Found OK button in PID $targetPid"
$invokePattern = $targetButton.GetCurrentPattern([System.Windows.Automation.InvokePattern]::Pattern)
if ($invokePattern) {
$invokePattern.Invoke()
Write-Output "UIA Handler: Clicked OK button successfully."
return
}
}
Start-Sleep -Milliseconds 500
}
Write-Output "UIA Handler: Timeout reached, no dialog detected."
} -ArgumentList $wordPid
Write-Host "Opening document: $SourcePath" -ForegroundColor Yellow
$Doc = $Word.Documents.Open($SourcePath)
$Doc.RemovePersonalInformation = $true
Write-Host "Saving document..." -ForegroundColor Yellow
$Doc.SaveAs([ref]$DestinationPath, [ref]15)
Receive-Job -Job $dismissJob | ForEach-Object {
Write-Host $_ -ForegroundColor Cyan
}
Write-Host "Save completed!" -ForegroundColor Green
$Doc.Close([ref]$false)
Write-Host "Document closed" -ForegroundColor Green
} catch {
Write-Host "ERROR: $_" -ForegroundColor Red
} finally {
if ($Word) {
$Word.Quit()
[System.Runtime.InteropServices.Marshal]::ReleaseComObject($Word) | Out-Null
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()
}
if ($dismissJob) {
Stop-Job $dismissJob -ErrorAction SilentlyContinue
Remove-Job $dismissJob -ErrorAction SilentlyContinue
}
}
Write-Host "Done! File saved to: $DestinationPath" -ForegroundColor Green