0

I am running the following script and receiving the following POP UP to select printer, how can I handle it? Disable it or click OK

[1

param(
    [string]$filepath
)

$fileitem = Get-Item $filepath

try {

    $excel = New-Object -ComObject Excel.Application
    $excel.Visible = $false
    $excel.DisplayAlerts = $false

    $wb = $excel.Workbooks.Open($fileitem.FullName)

    # Generate path of save destination PDF file
    $pdfpath = $fileitem.DirectoryName + "\" + $fileitem.BaseName + ".pdf"

    # Save as PDF
    $wb.ExportAsFixedFormat([Microsoft.Office.Interop.Excel.XlFixedFormatType]::xlTypePDF, $pdfpath)

    $wb.Close()

    $excel.Quit()
}
BigBen
  • 46,229
  • 7
  • 24
  • 40
  • You can programmatically click buttons using [UI automation](https://stackoverflow.com/search?q=%5Bpowershell%5D+System.Windows.Automation). Find the window named "printer setup", then find the "Ok" button. Get `InvokePattern` from button and use it to click the button. `System.Windows.Automation` is somewhat deprecated, so it may or may not work. Using the newer UIA3 COM API from PowerShell requires 3rd party wrapper, I've given an example in [this answer](https://stackoverflow.com/questions/73534710/powershell-commands-to-find-and-close-windows-file-explorer-dialogs/73541499#73541499). – zett42 Sep 22 '22 at 17:17

0 Answers0