Modifying PowerShell script to change the Font type and size ?

Ask your Windows PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
User avatar
ITEngineer
Posts: 90
Joined: Wed Oct 12, 2011 10:52 am

Modifying PowerShell script to change the Font type and size ?

Post by ITEngineer » Tue May 15, 2018 6:27 pm

People,

I need some help in modifying the below Set-Outlook-Signature.ps1 that is from https://github.com/a118n/poweradmin/blo ... nature.ps1
So I can adjust the email font template to below:
Font Type: Consolas
Font Size: 12
Font Colour: Blue

I manually created the .DOCX and then manually set the font typeface to be Consolas.
However the end result is that Outlook always using the font as Calibri (Office default).

FYI this is the location of my .DOCX template: \\domain.com\SYSVOL\EmailSignature\Outlook Signature.docx
and the content is like below:

Code: Select all

AD_DisplayName 
AD_Title	AD_Company
AD_Email
Mobile: AD_MobilePhone  |   Phone: AD_Telephone 
AD_Company • AD_City
Image


This is what I have tried but not working, hence I post it here for some assistance:

Code: Select all

$MSWord.ActiveDocument.FontName = 'Consolas'

Code: Select all

$MSWord.ActiveDocument.Range.Font.Name = 'Consolas'

Code: Select all

$MSWord.ActiveDocument.Selection.Font.Name = 'Consolas'

Code: Select all

$MSWord.ActiveDocument.Text.Selection.Font= 'Consolas'

Code: Select all

$MSWord.ActiveDocument.Text.Selection.Font.Name = 'Consolas'

Code: Select all

$MSWord.ActiveDocument.Selection.Range.Font.Name = 'Consolas'

Code: Select all

$MSWord.ActiveDocument.Selection.Text.Font.Name = 'Consolas'

However, when I create new MS Word document using the below script, it works:

Code: Select all

$MSWord = New-Object -com word.application
$MSWord.Visible = $True
$Document = $MSWord.Documents.Add()
$Document.Content.Text = "Some Text"
$Document.Content.Font.Name = 'Consolas'
$Document.Content.Font.Size = 12
Any help and suggestion to modify the code above would be greatly appreciated.

Thanks in advanced.
/* IT Engineer */

User avatar
jvierra
Posts: 12770
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Modifying PowerShell script to change the Font type and size ?

Post by jvierra » Tue May 15, 2018 7:38 pm

This is the same question posted on the MS forum and the answer for how to do it is still the same.

https://social.technet.microsoft.com/Fo ... powershell

You must crate the bookmarks for the text that you want to replace. Setting the format in the template will remain in Outlook as long as you use a font that is available to Outlook.

As noted in the linked forum, the GitHub method is not the correct way to create an Outlook signature.

I have used the method I posted for many years. It works on all current versions of Outlook. It onlu needs to be run once per user which can be easily done via Group Policy or by distributing a link to the users that they can execute.

User avatar
ITEngineer
Posts: 90
Joined: Wed Oct 12, 2011 10:52 am

Re: Modifying PowerShell script to change the Font type and size ?

Post by ITEngineer » Tue May 15, 2018 7:49 pm

Hi Mr. Vierra,

Would you be able to share the script here please ?
/* IT Engineer */

User avatar
jvierra
Posts: 12770
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Modifying PowerShell script to change the Font type and size ?

Post by jvierra » Tue May 15, 2018 7:55 pm

It is the same script I posted on the MS forum. You just have to define the correct bookmarks that you want and alter the script to set those bookmarks by name. Be sure the book marks covers the whole range of the text to be replaced. This is done in the template by highlighting the text and defining a bookmark name.
Attachments
OutlookSignatureFromTemplate.ps1
(625 Bytes) Downloaded 18 times

User avatar
ITEngineer
Posts: 90
Joined: Wed Oct 12, 2011 10:52 am

Re: Modifying PowerShell script to change the Font type and size ?

Post by ITEngineer » Tue May 15, 2018 8:50 pm

jvierra wrote:
Tue May 15, 2018 7:55 pm
It is the same script I posted on the MS forum. You just have to define the correct bookmarks that you want and alter the script to set those bookmarks by name. Be sure the book marks covers the whole range of the text to be replaced. This is done in the template by highlighting the text and defining a bookmark name.
OK, and then do I need to edit the NormalEmail.dotm file to have image, disclaimer and table layout like the below screenshot ?

Image

Because creating the HTML script using the PowerShell you've attached is quite cumbersome :o
/* IT Engineer */

User avatar
jvierra
Posts: 12770
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Modifying PowerShell script to change the Font type and size ?

Post by jvierra » Tue May 15, 2018 9:07 pm

The HTML is created automatically when it is assigned as an Outlook signature.

Word creates a signature from a document by assigning the range and calling Outlook to generate three files. Html, text and RTF. If you look in the signatures folder after the code runs you will see those files plus the required control files used by Outlook to manage and use the signatures.

The issue with many people trying to create signatures is that they are working from documentation that has been obsolete and unnecessary since Outlook 2000. The GitHub script is a variation on a very old pre-2000 VBScript method. OL2000 and later added the signature generation commands to Word. Outlook assigns the signature based on the email body type.

Here is what is generated:
OLSigs.png
OLSigs.png (21.72 KiB) Viewed 721 times

User avatar
jvierra
Posts: 12770
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Modifying PowerShell script to change the Font type and size ?

Post by jvierra » Wed May 16, 2018 12:22 am

Here
sigtest.png
sigtest.png (47.9 KiB) Viewed 709 times
is an example of the signature created with a template and the code above:

User avatar
ITEngineer
Posts: 90
Joined: Wed Oct 12, 2011 10:52 am

Re: Modifying PowerShell script to change the Font type and size ?

Post by ITEngineer » Thu May 17, 2018 12:20 am

Wow, that's cool.
So how do you layout and place the Bookmark?

This is my updated code for the bookmark:
  1. # create word document
  2.  
  3. $WordDoc = New-Object -ComObject Word.Application
  4.  
  5. $doc = $WordDoc.Documents.Add('\\PRDFS01-VM\EmailSignature\Default Outlook Signature.dotx')
  6.  
  7.  
  8.  
  9. $props=@(
  10.  
  11.     'company',
  12.  
  13.     'l',
  14.  
  15.     'displayname',
  16.  
  17.     'mail',
  18.  
  19.     'mobile',
  20.  
  21.     'postalCode',
  22.  
  23.     'streetAddress',
  24.  
  25.     'st',
  26.  
  27.     'title',
  28.  
  29.     'telephonenumber'
  30.  
  31. )
  32.  
  33. $UserName = $env:username
  34.  
  35. $searcher=[adsisearcher]"samAccountName=$($env:username)"
  36.  
  37. $searcher.PropertiesToLoad.AddRange($props)
  38.  
  39. $ADUser = $searcher.FindOne()
  40.  
  41.  
  42.  
  43. $ADDisplayName = $ADUser.Properties['displayname']
  44.  
  45. $ADEmailAddress = $ADUser.Properties['mail']
  46.  
  47. $ADTitle = $ADUser.Properties['title']
  48.  
  49. $ADTelePhoneNumber = $ADUser.Properties['telephonenumber']
  50.  
  51. $ADMobilePhone = $ADUser.Properties['mobile']
  52.  
  53. $ADStreetAddress = $ADUser.Properties['streetAddress']
  54.  
  55. $ADCity = $ADUser.Properties['l']
  56.  
  57. $ADState = $ADUser.Properties['st']
  58.  
  59. $ADPostal = $ADUser.Properties['postalCode']
  60.  
  61.  
  62.  
  63. If ($ADCity -ne '') {    
  64.  
  65.     switch ( $ADPostal ) {
  66.  
  67.         1234 { $ReplaceText = 'City A'         }
  68.  
  69.         5678 { $ReplaceText = 'City B'         }
  70.  
  71.         9999 { $ReplaceText = 'Outside'         }
  72.  
  73.         default {$ReplaceText = ''}
  74.  
  75.     }
  76.  
  77.     $ReplaceText
  78.  
  79.    }
  80.  
  81.    Else {
  82.  
  83.     $ReplaceText = 'Something wrong in the AD Attributes'
  84.  
  85.    }
  86.  
  87.  
  88.  
  89. # set all bookmark texts by name
  90.  
  91. $doc.Bookmarks['ST_DisplayName'].Range.Text = $ADDisplayName
  92.  
  93. $doc.Bookmarks['ST_Title'].Range.Text = $ADTitle
  94.  
  95. $doc.Bookmarks['ST_City'].Range.Text = $ReplaceText
  96.  
  97. $doc.Bookmarks['ST_Telephone'].Range.Text = $ADTelePhoneNumber
  98.  
  99. $doc.Bookmarks['ST_MobilePhone'].Range.Text = $ADMobilePhone
  100.  
  101.  
  102.  
  103. # generate signatures in Outlook
  104.  
  105. $WordDoc.EmailOptions.EmailSignature.EmailSignatureEntries.Add('Default Outlook Signature',$doc.Range())
  106.  
  107. $WordDoc.EmailOptions.EmailSignature.NewMessageSignature = 'Default Outlook Signature'
  108.  
  109. $WordDoc.EmailOptions.EmailSignature.ReplyMessageSignature = 'Default Outlook Signature'
  110.  
  111.  
  112.  
  113. # abort document and close/quit
  114.  
  115. $doc.Saved = $true
  116.  
  117. $doc.Close()
  118.  
  119. $WordDoc.Quit()
  120.  
  121.  
My email signature in Outlook is still the name of the Bookmark not the AD Attributes value ?

Error:
The requested member of the collection does not exist.
+ $doc.Bookmarks['ST_DisplayName'].Range.Text = $ADDisplayName
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], COMException
+ FullyQualifiedErrorId : System.Runtime.InteropServices.COMException

The requested member of the collection does not exist.
+ $doc.Bookmarks['ST_Title'].Range.Text = $ADTitle
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], COMException
+ FullyQualifiedErrorId : System.Runtime.InteropServices.COMException

The requested member of the collection does not exist.
+ $doc.Bookmarks['ST_City'].Range.Text = $ReplaceText
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], COMException
+ FullyQualifiedErrorId : System.Runtime.InteropServices.COMException

The requested member of the collection does not exist.
+ $doc.Bookmarks['ST_Telephone'].Range.Text = $ADTelePhoneNumber
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], COMException
+ FullyQualifiedErrorId : System.Runtime.InteropServices.COMException

The requested member of the collection does not exist.
+ $doc.Bookmarks['ST_MobilePhone'].Range.Text = $ADMobilePhone
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], COMException
+ FullyQualifiedErrorId : System.Runtime.InteropServices.COMException


Application : Microsoft.Office.Interop.Word.ApplicationClass
Creator : 1297307460
Parent : System.__ComObject
Index : 1
Name : Default Outlook Signature
/* IT Engineer */

User avatar
ITEngineer
Posts: 90
Joined: Wed Oct 12, 2011 10:52 am

Re: Modifying PowerShell script to change the Font type and size ?

Post by ITEngineer » Thu May 17, 2018 12:44 am

This is how it looks like on the MS Word .DOTX when opened with Word2016 as well as on the Outlook 2016:

Image
/* IT Engineer */

User avatar
jvierra
Posts: 12770
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Modifying PowerShell script to change the Font type and size ?

Post by jvierra » Thu May 17, 2018 12:52 am

Please use my code only to experiment. You are making changes that break the code. You will have to try an learn how MSWord works when used from COM.

You have to create the bookmarks in the DOTX file first. Just fix the file and use my code but change the bookmark name. Use the strings provided instead of any other code. Add the extra bookmark assignments.

We cannot do this for you. You have to try to understand what is happening in the code and how it interacts with Word.

Locked