0
votes

I haven't been able to find a solution yet.

Only solutions using Powershell please. How can a Word *.docx file be converted to a PDF *.pdf file please without using a COM object. This is to run on a virtual server via a Windows Task Schedule with 'run even if user is NOT logged on' option ticked which is why the current COM object solution is not working (if I untick this option it works but not suitable option).

I found a PSWriteWord powershell module to do the search and replace portion of the code (word template to new word file) but it does not appear to support save as PDF. There is also a PSWritePDF module but again it does not appear to have this functionality.

$filePath   = "C:\Temp6"
$origFile   = "$filePath\Template.docx"
$newFile    = "$filePath\ReplacedText.docx"

# Get the original document
$WordDocument = Get-WordDocument -FilePath $origFile

# Search and replace some text
ForEach ($Paragraph in $WordDocument.Paragraphs) {
    $Paragraph.ReplaceText('[given_name]','Jane')
    $Paragraph.ReplaceText('[surname]','Doe')
}

# Save as new document
Save-WordDocument -WordDocument $WordDocument -FilePath $newFile 

# Quick check to confirm Word application is not left open
Get-Process -name WinWord -ErrorAction SilentlyContinue

# How to convert/save as PDF WITHOUT COM Object?

Thank you.

if you are going to use docx to pdf ... you need an app or util that can do the converting. the usual way is to use the COM interface ... and an account that is designed for that.Lee_Dailey
@Lee_Daily - The script I currently have uses a COM object however it needs to run as an admin user on a virtual server in a windows task scheduler when a user is NOT logged on. It wont work if I tick this option and we don't want the user to have to be constantly logged on, hence my post. I have tried the hack suggested by creating a 'desktop' directory in the userprofile folder but that didn't work unfortunately.Shell D
@Tomalak - thanks, I'll have a look, hopefully it works in a windows task schedule with the user logged off (unlike com objects)Shell D
@ShellD As long as Word does not load a user interface, but does the conversion silently when called from the command line, there's a good chance it works.Tomalak