0
votes

In MS Outlook, is there a way to automatically replace some words like Google, MSN, Facebook, etc (I have an exhausting list in a CSV file), by the hyperlink that redirects to correct website. So basically when I type google it transforms it to a hyperlink.

My CSV file:

Word, URL 
Facebook, https://facebook.com 
MSN, https://msn.com
Google, https://google.com

What I have so far is a script that add to the object autocorrect entries a word and replaces it by another word not using a CSV but a word document. But I'm not able to replace it by an hyperlink. It causes an error saying that autocorrect entries accept only string format and not object (hyperlink). Reference: Add formatted text to Word autocorrect via PowerShell

When I create manually via outlook an hyperlink and I add this hyperlink to autocorrect and I run the following PowerShell script I can't find this autocorrect entry:

(New-Object -ComObject word.application).AutoCorrect.Entries | where{$_.Value -like "*http*"}

I want to adapt this code coming from Use PowerShell to Add Bulk AutoCorrect Entries to Word

If someone has an idea on how to add a hyperlink to the autocorrect entries, I would be grateful.

Thanks!

1

1 Answers

0
votes

I finally managed how to add autocorrect entries for both word and outlook. I need to create a .docx file with 'X row' and '2 Columns', the first column contain the word that i want an autocorrect like 'google' and the second column the 'google' link.

$objWord = New-Object -Com Word.Application
$filename = 'C:\Users\id097109\Downloads\test3.docx'
$objDocument = $objWord.Documents.Open($filename)
$LETable = $objDocument.Tables.Item(1)
$LETableCols = $LETable.Columns.Count
$LETableRows = $LETable.Rows.Count

$entries = $objWord.AutoCorrect.entries

for($r=1; $r -le $LETableRows; $r++) {
    $replace = $LETable.Cell($r,1).Range.Text
    $replace = $replace.Substring(0,$replace.Length-2)

    $withRange = $LETable.Cell($r,2).Range
    $withRange.End = $withRange.End -1
    # $with = $withRange.Text

    Try { 
        $entries.AddRichText($replace, $withRange) | out-null 
    }
    Catch [system.exception] {
        Write-Host $_.Exception.ToString()
    }
}

$objDocument.Close()
$objWord.Quit()

 [gc]::collect()

 [gc]::WaitForPendingFinalizers()

$rc = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($objWord)

This code allow to modify the file Normal.dotm that contains all the autocorrect to an object link (C:\Users{your user id}\AppData\Roaming\Microsoft\Templates) But then to apply those change to Outlook you have delete the 'NormalEmail.dotm' and the copy/paste 'Normal.dotm' and the rename it to 'NormalEmail.dotm'

This is the script to avoid to do it manually :

$FileName='C:\Users\{your id}\AppData\Roaming\Microsoft\Templates\Normal.dotm'
$SaveTo='C:\Users\{your id}\AppData\Roaming\Microsoft\Templates\NormalEmail.dotm'
Remove-Item –path $SaveTo
$Word = New-Object –ComObject Word.Application
$Document=$Word.Documents.Open($Filename)
$Document.SaveAs($SaveTo)
$Document.Close