New-Object -ComObject Word.Application not working

Product: PowerShell Universal
Version: 1.4.6

New-Object -ComObject Word.Application not working in 5.6.0,

Upgraded our server from 4. something to 5.6 and havin issues with creating word Docs

If i use this example code from powershell ISE logged in as the user account that runs PSU it creates the document:

$word = New-Object -ComObject Word.Application
$word.Visible = $True # Set to $true if you want to see Word open$doc = $word.Documents.Add()$selection = $word.Selection

$selection.TypeText(“This is a document created with PowerShell.”)

$filePath = “D:\PSU_Data\Add Users\Welcome Sheets Files\MyPowerShellDocument.docx”
$doc.SaveAs([ref]$filePath)

$doc.Close()
$word.Quit()

This exact Code in a new APP does nothing

New-UDApp -Content {

   New-UDButton -Text "Click Me!"  -onclick {

        \# Create a new Word Application object

  $word = New-Object -ComObject Word.Application

  $word.Visible = $True # Set to $true if you want to see Word open

  \# Add a new document

  $doc = $word.Documents.Add()

  \# Get the selection object (where the cursor is)

  $selection = $word.Selection

  \# Insert text

  $selection.TypeText("This is a document created with PowerShell.")

  \# Save the document

  $filePath = "D:\\PSU_Data\\Add Users\\Welcome Sheets Files\\MyPowerShellDocument.docx"

  $doc.SaveAs(\[ref\]$filePath)

  Show-UDToast -Message "Test"

  \# Close the document and Word application

  $doc.Close()

  $word.Quit()

If i open task manager i can see Word gets opened but none of the other code will run except the toast

I stay away from COM automation like this, I’ve found it to be quite unreliable at times but also resource intensive (since you’re actually running the application in the background), and instead i’ll use PSWriteOffice which load dlls instead and do it a more efficient way.

I’d hazard a guess that when you’re running the script in PSU, if it’s running under a different context e.g service account, there’s your difference in why it may work for you interactively but not in PSU.

There’s likley a popup or something happening in the background which because it’s now running in that context, the ‘visbile’ wont work - since visible would only be visible to that logged in user, not you as a user to PSU.

One way to test, would be to login to your service as the service account/context running the script, and run it, that way you’ll see the application and can maybe figure out why it’s hanging.

You could also try adding $word.DisplayAlerts = 0, but personally, I’d ditch COM all together and go with PSWriteOffice

Hey!

you are 100% correct, last night i tried the PSWriteOffice.

Took out a bunch of crappy word COM code with a find and replace function to make my new users welcome sheets. to something as simple as this.

$date = (Get-Date).ToString(“yyyy-MM-dd”)

$TEMPdoc = Get-OfficeWord -FilePath “d:\PSU_Data\Add Users\Welcome Sheet Template\Template.docx”

# FIND and Replace on the Document

$TEMPdoc.FindAndReplace(“<>”,$date)

$TEMPdoc.FindAndReplace(“<>”,$User.Site)

$TEMPdoc.FindAndReplace(“<>”,$User.Username)

$TEMPdoc.FindAndReplace(“<>”,$User.Password)

$TEMPdoc.FindAndReplace(“<>”,$User.EmailAddress)

$TEMPdoc.FindAndReplace(“<>”,$User.Token)

#Create Save Location

$Username = $User.Username

$FacilityCode = $User.Site

$SaveName = “D:\PSU_Data\Add Users\Welcome Sheets Files\$FacilityCode-$Username.docx”

Save-OfficeWord -Document $TEMPdoc -FilePath $SaveName

Close-OfficeWord -Document $TEMPdoc

Its so much easier and readable. Thank you SIR!