When Word has following limitation with PDF
1. when not set as default App for PDF
2. does not show PDF it exported
3. cannot Open PDF navigated by Recent Items
I am using PowerShell Script to have Word open most recent PDF; I need help how to modify following script to bring Window (opened PDF) to the frontmost - as would be activated if alt-tabbed to the application.
Code:
$PDF = "2023-04-27\To Translate\Staging\Otis Gen2Core with Ultra Drive ACD3 & UD\Screenshot\P1. MS Export.pdf"
$desktop_path = [Environment]::GetFolderPath("Desktop")
$latestPDF = Get-ChildItem -Attributes !Directory *.pdf | Sort-Object -Descending -Property LastWriteTime | select -First 1
$word = New-Object -ComObject word.application
$word.Visible = $true
$fullpath = Join-Path -Path $desktop_path -ChildPath $PDF
$doc = $word.Documents.Open("$latestPDF")
$hwnd = $doc.ActiveWindow.Hwnd
[Void]$User32::SetWindowPos($hwnd, -1, 0, 0, 0, 0, 0x53)
<#
https://superuser.com/questions/1784845/how-to-make-the-word-open-dialog-visible-and-frontmost
https://answers.microsoft.com/en-us/msoffice/forum/all/how-to-show-documents-not-yet-opened-and-saved/1937da76-8b06-4681-82b6-d6042c344754
https://support.microsoft.com/en-us/office/command-line-switches-for-microsoft-office-products-079164cd-4ef5-4178-b235-441737deb3a6
https://answers.microsoft.com/en-us/msoffice/forum/all/for-translating-pdf-how-to-open-pdf-in-recent/0ab3a6dd-53d5-423f-97ae-ea41eb2530a5
<#
<#
$latestfile = Get-ChildItem -Attributes !Directory . | Sort-Object -Descending -Property LastWriteTime | select -First 1
$word.visible = $true
$workbook = $word.open
# $doc = $word.Documents.Open("$desktop_path" + "$PDF") - need to add slash
#>
<#
cmd - Run command with drag and drop onto batch file - Stack Overflow
https://superuser.com/questions/1419800/how-a-running-application-can-be-set-as-active-window-via-powershell
https://stackoverflow.com/questions/51985708/correct-way-to-bring-window-to-foreground
https://www.thecodebuzz.com/powershell-get-most-recent-file-in-directory-folder/#:~:text=PowerShell-%20Get%20the%20most%20recent%20file%20in%20Directory,%7C%20select%20-First%201%20PS%20C%3ATest%3E%20%24latestfile.Name%20TheCodeBuzz.png
https://stackoverflow.com/questions/52074362/how-to-connect-powershell-script-to-an-already-opened-document
https://stackoverflow.com/questions/76275880/how-run-excel-command-lines-from-batch-file
#>