I would like to prevent the "Be careful! Your document may contain personal information that cannot be removed by the Document Inspector." dialog from being displayed when saving a document.

AndAge 0 Reputation points
2023-10-19T12:10:26.97+00:00

I am trying to create a program to remove property information from a Word document in order to ensure that no personal or client information is left behind.

I am using C# code with Microsoft.Office.Interop.Word to initiate Word, and I execute RemoveDocumentInformation(WdRemoveDocInfoType) with the following arguments to remove the properties:

  • wdRDIDocumentProperties
  • wdRDIRemovePersonalInformation

We are aiming to support file extensions such as .docx, .docm, .doc, .dotx, .dotm, and .dot.

However, for .docm, .doc, .dot, and .dotm files, the following dialog appears when saving the file:

"Be careful! Your document may contain personal information that cannot be removed by the Document Inspector."

I start Word.Application programmatically with Visible=False to prevent the screen from displaying during the property deletion process.

However, this dialog unexpectedly appears, and since multiple files are being processed, the dialog must be closed many times.

This behavior is not acceptable for users. I have set Word.Application.DisplayAlerts to wdAlertsNone and confirmed that no other dialogs are displayed, but this specific dialog still appears.

I have researched solutions to similar issues in the past and found that one solution involved disabling the "remove personal information from file properties on save" setting in the Trust Center.

This prevented the dialog from appearing for files with certain extensions.

However, this approach has two problems:

  1. Property information remains:

Since it's necessary to disable the personal information deletion setting when saving the file,

the user's name at the time of program execution remains in the property information as the updater.

The program's purpose is to delete properties that may contain personal or business partner information.

  1. Disabling the function of deleting personal information:

Turning off the function of deleting personal information when saving a file causes the information not to be deleted the next time properties are set.

Files that were originally set to allow deletion of personal information will not function as intended.

This program is designed to prevent personal and client information from being left in documents. It is unacceptable for the program to leave behind properties that should normally be deleted. I have considered the following methods to resolve this issue but have not found a solution:

Turning off properties other than Word.Application.DisplayAlerts that correspond to the displayed dialog.

Automatically selecting "OK" for the displayed dialog.

Is it possible to implement the above solutions? Alternatively, is there another effective solution?

Sorry for the automatically translated English text, but thank you in advance.

Microsoft 365 and Office | Development | Other
Developer technologies | C#
Developer technologies | C#

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.

Locked Question. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

1 answer

Sort by: Most helpful
  1. ADDY ad 0 Reputation points
    2026-08-06T11:43:10.16+00:00

    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
    

    Was this answer helpful?

    0 comments No comments