powershell -- how do I programmatically close an excel dialog box?


I'm trying to figure out how to programmatically close an excel dialog box that pops up when I'm closing a workbook.

# create an instance of excel  
$a = New-Object -comobject Excel.Application  
# make it visible  
$a.Visible = $True  
# open  workbook   
$b = $a.Workbooks.Open("filename.xlsx")  
# There are a number of steps following, then I manually copy some data from the workbook.  
# close the workbook, saving changes  
# this is the point where I see the dialog box I want to close programmatically  


I don't want to save the info on the clipboard, so I want to answer No

Thanks for any assistance

Christian Bahnsen

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
4,734 questions
{count} votes

Accepted answer
  1. Rich Matheisen 36,161 Reputation points

    You can avoid the need to deal with dialog boxes. Try one (or both) of these:

    $a.DisplayAlerts = $False
    $null = [Reflection.Assembly]::LoadWithPartialName(“System.Windows.Forms”)

2 additional answers

Sort by: Most helpful
  1. Ian Xue (Shanghai Wicresoft Co., Ltd.) 18,766 Reputation points Microsoft Vendor


    Please see if this helps

    Best Regards,


    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

  2. 2020-11-25T12:41:07.467+00:00

    @Rich Matheisen

    Thanks, $a.DisplayAlerts = $False worked like a charm

    Thanks also to @Ian Xue (Shanghai Wicresoft Co., Ltd.) for your reply