0
$app = New - Object - ComObject 'Word.Application'
$app.visible = $true
$doc = $app.Documents.Add(
}
$doc.Content.text = 'Here's an exmple of mispeled txt."
$selection = $app.Selection
$report = @(
}
foreach($word in ($doc.Words | Select - ExpandProperty Text)) {
    if (!($app.CheckSpelling($word))) {
        $result = New - Object - TypeName psobject - Property @(Mispelled = $word)
        $sug = $app.GetSpellingSuggestions($word) | Select - ExpandProperty name
        if ($sug) {
            $report += New - Object psobject - Property @ {
                Misspelled = $word;
                Suggestions = $sug
            }
        } else {
            $report += New - Object - TypeName psobject - Property @ {
                Misspelled $word; =
                Suggestions "No Suggestion"
            }
        }
    }
}
$Sreport | Select - Property Misspelled, Suggestions

Hello. I found this script online, from which I'm trying to output a list of misspelled English words together with their spelling recommendations, using MS Word's spelling corrections via Powershell. I don't have coding knowledge, so I don't know why isn't the script working. Could someone help me out? Thanks!

1 Answers1

0

This works, i think! You didnt mention the output you wanted so hope this is ok.

You were on the right track but had some syntax and bracket issues

$app = New-Object -ComObject 'Word.Application'
$app.visible = $false
$doc = $app.Documents.Add()

$doc.Content.text = Get-Content "C:\Temp\TestFile.txt" # This will get the text of the file specified
$selection = $app.Selection
$report = @() # an empty array which we will fill with spelling mistakes

foreach($word in ($doc.Words | Select -ExpandProperty Text)) { # loop through each word in the text
    # if the word is misspelled then process it
    if (!($app.CheckSpelling($word))) {
        
        # get list of correction suggestions for this word
        $suggestion = $app.GetSpellingSuggestions($word) | Select -ExpandProperty name 
        
        # if there are suggestions then add it to the report
        if ($suggestion) {  
            $report += New-Object psobject -Property @{
                Misspelled = $word;
                Suggestions = $suggestion
            }
        } 
        # else if there are no suggestions then add an entry saying just that
        else { 
            $report += New-Object -TypeName psobject -Property @{
                Misspelled = $word;
                Suggestions = "No Suggestion"
            }
        }
    }
}

# push the results to a file 
$report | Out-File "C:\Temp\results.txt"
Otter
  • 1,086
  • 7
  • 18
  • Thank you @Otter. I ran your version. Do you know what should I do now since I got a Type E 'Library Not Registered'? I googled about this error and I only found people talking about Outlook and Excel but not MS Word. "Unable to cast COM object of type 'Microsoft.Office.Interop.Word.ApplicationClass' to interface type 'Microsoft.Office.Interop.Word._Application'. This operation failed because the QueryInterface call on the COM component for the interface failed due to the following error: Library not registered. (Exception from HRESULT: 0x8002801D (TYPE_E_LIBNOTREGISTERED))." – fifafular92 May 28 '22 at 11:13
  • In addition to the above error, I also got 2 additional errors: 1. You cannot call a method on a null-valued expression. At line:3 char:1 + $doc = $app.Documents.Add() 2.The property 'text' cannot be found on this object. Verify that the property exists and can be set. At line:5 char:1 + $doc.Content.text = "Here's an exmple of mispeled txt." – fifafular92 May 28 '22 at 11:17
  • 1
    That error (HRESULT: 0x8002801D) means there is something wrong with your Office (Word) installation. You could try [repairing MS Office](https://support.microsoft.com/en-us/office/repair-an-office-application-7821d4b6-7c1d-4205-aa0e-a6b40c5bb88b) and after that start Word. Then close Word and try @Otters code again – Theo May 28 '22 at 11:37
  • Thank you both, I'm repairing it now as I'm typing. Will update here. – fifafular92 May 28 '22 at 14:03
  • Thank you so much!!! It worked after repairing! Really appreciate it guys! P.S. If you would be kind enough to help me understand this script, can you guys help explain it in layman's terms on what each line does from the "foreach" line downwards? In addition to that, for this line saying '$doc.Content.text = "Here's an exmple of mispeled txt."' can it be replaced with a command like Get-Content to get text from a Notepad file? And lastly, for the $report line at the end, can it be replaced with a report writeup in the format of Notepad file too? – fifafular92 May 28 '22 at 14:13
  • @fifafular92 I have added some comments to help explain everything and i have included reading text from a file located at `C:\Temp\TestFile.txt`, you can change that line to point to any text file. And writing results to a file on the last line – Otter May 28 '22 at 14:39
  • @Otter Again, thank you so so much. As a last question, for me to automate this as much as possible, is there any command I can insert (after the last line which creates the Notepad output file for me) that instructs the Powershell window to close once its done its job? – fifafular92 May 28 '22 at 15:11