Home > Mobile >  Powershell script not working (Misspelling Project)
Powershell script not working (Misspelling Project)

Time:05-28

$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!

CodePudding user response:

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"
  • Related