Hello, I have a challenging problem. In general, I have thousands of PowerPoint documents, that I need to open and answer a popup message regarding repairing the file (PowerPoint found a problem with content in XXXXX. PowerPoint can attempt
to repair the presentation). The specific Office PowerPoint 2016 message is attached below. I would like to automate this via powershell scripting.
I've tried different methods for opening, sending keys, and closing the PowerPoint - all without success. Specifically, I've tried the following:
1) Opening the document via Com - failed result as COM can't open it due to this message
2) Opening the document via command-line (eg powerpnt.exe c:\doc.ppt) and then use DLLImport for the user32.dll to grab the window, set it's focus, and then use sendkeys to answer the message box - failed result as when I bring the windows to the foreground
and use sendkeys, it's the sendkeys don't go to the window. In troubleshooting the issues, it looks like it brings the app to the foreground, but the keyboard focus is not on it. In fact, when I mouse click on it - after the script runs - I see
the focus for the keyboard goes to the app.
I'm guessing that I need to grab the window and set it to the foreground, as well as set the keyboard to focus on this window, but that still doesn't work.
I"m looking for help to get the keyboard focus to the window, or another creative way to get this problem solved! My very simple POC script is below.
thx
Simple script***********************
# Helper functions for building the class
$script:nativeMethods = @();
function Register-NativeMethod([string]$dll, [string]$methodSignature)
{
$script:nativeMethods += [PSCustomObject]@{ Dll = $dll; Signature = $methodSignature; }
}
function Add-NativeMethods()
{
$nativeMethodsCode = $script:nativeMethods | % { "
[DllImport(`"$($_.Dll)`")]
public static extern $($_.Signature);
" }
Add-Type @"
using System;
using System.Runtime.InteropServices;
public static class NativeMethods {
$nativeMethodsCode
}
"@
}
# Add methods here
Register-NativeMethod "user32.dll" "bool SetForegroundWindow(IntPtr hWnd)"
Register-NativeMethod "user32.dll" "bool ShowWindowAsync(IntPtr hWnd, int nCmdShow)"
Register-NativeMethod "user32.dll" "bool SetActiveWindow(IntPtr hWnd, int nCmdShow)"
Register-NativeMethod "user32.dll" "bool GetFocus(IntPtr hWnd, int nCmdShow)"
# This builds the class and registers them (you can only do this one-per-session, as the type cannot be unloaded?)
Add-NativeMethods
# (the Out-Null is just to throw away the return value)
# [NativeMethods]::SetForegroundWindow((Get-Process -name notepad).MainWindowHandle) | Out-Null
# [NativeMethods]::ShowWindowAsync((Get-Process -name notepad).MainWindowHandle, 2) | Out-Null
# [NativeMethods]::SetActiveWindow((Get-Process -name notepad).MainWindowHandle, 2) | Out-Null
![]()