I need help with the problem listed below

Hi all, I am in need of help with the below script, I didn’t write it myself - I wouldn’t know where to start.
When I was given this code it worked perfectly and printed all of the Fonts available on my PC.
Since then I have added more Fonts, but it doesn’t work. The problems are listed below the code.
Thanks for any help. Regards Malcolm.

Clear-Host
$word = New-Object -ComObject “Word.Application”
$word.Visible = $true
$document = $word.Documents.Add()
$sorted = $word.FontNames | sort
for ($i=0;$i -lt $word.FontNames.Count;$i++ ) {
$p = $document.Paragraphs.Add()
$p.Range.Text = $sorted[$i] + " ($i) - "
$p.Range.Font.Name = “Arial”
$p.Range.Font.Size = 20
$f=$p.Range.End
$p.Range.InsertAfter(“ABCDEFGabcdefg1234567890”)
$t=$p.Range.End
$p.Range.InsertParagraphAfter()
$document.Range(ref,[ref]$t).Font.Name = $sorted[$i] }

At C:\Users\malco\OneDrive\Documents\Print_All_Fonts.ps1:2 char:9

  • $word = New-Object -ComObject “Word.Application”
  •     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : ResourceUnavailable: (:slight_smile: [New-Object], COMException
    • FullyQualifiedErrorId : NoCOMClassIdentified,Microsoft.PowerShell.Commands.NewObjectCommand

The property ‘Visible’ cannot be found on this object. Verify that the property exists and can be set.
At C:\Users\malco\OneDrive\Documents\Print_All_Fonts.ps1:3 char:1

  • $word.Visible = $true
  •   + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
      + FullyQualifiedErrorId : PropertyNotFound
    
    

You cannot call a method on a null-valued expression.
At C:\Users\malco\OneDrive\Documents\Print_All_Fonts.ps1:4 char:1

  • $document = $word.Documents.Add()
  •   + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
      + FullyQualifiedErrorId : InvokeMethodOnNull

Hello, welcome back :wave:

Firstly, when posting code and errors, please use the </> button so that your code is formatted properly, easy to read, and easy to test.

All of your errors are due to the first one you receive, which is that PowerShell cannot find the COM class Word.Application - is Word installed on that computer?

I got an error on your script, at the last line.
I refactored it a bit and it works.

Don’t know why you get error on Word.Application COM Object.

$word = New-Object -ComObject “Word.Application”
$word.Visible = $true
$document = $word.Documents.Add()
$sorted = $word.FontNames | sort
for ($i=0;$i -lt $word.FontNames.Count;$i++ ) {
    $p = $document.Paragraphs.Add()
    $p.Range.Text = $sorted[$i] + " ($i) - "
    $p.Range.Font.Name = $sorted[$i]
    $p.Range.Font.Size = 20
    $p.Range.InsertAfter(“ABCDEFGabcdefg1234567890”)
    $p.Range.InsertParagraphAfter()
}

PS: Nice script :smiley:

Hi,

Matt Bloomfield replied to my request for help.

He asked if Word was installed on my PC, I knew office 2007 Pro was installed but when

I checked it was not working correctly, when I get time I will re-install it.

Thanks to you and Matt in taking an interest in the problem.

Regards,

Malcolm.

Just to mention it at least once: Office 2007 does not get updates anymore for more than 4 years!! You should urgently update to a supported version or find a solution independent from an installed office product.

1 Like

Hi Olaf – Thanks for your concern. I have 5 desktops & 7 laptops.

I carry out most of my current work on a Lenovo laptop with Windows 11.

Regards,

Malcolm