Modifying PowerShell script to change the Font type and size ?

Ask your 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.
This topic is 5 years and 10 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
ITEngineer
Posts: 216
Last visit: Thu Mar 23, 2023 5:45 pm
Has voted: 4 times

Modifying PowerShell script to change the Font type and size ?

Post by ITEngineer »

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 */
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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

Post by jvierra »

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: 216
Last visit: Thu Mar 23, 2023 5:45 pm
Has voted: 4 times

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

Post by ITEngineer »

Hi Mr. Vierra,

Would you be able to share the script here please ?
/* IT Engineer */
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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

Post by jvierra »

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 219 times
User avatar
ITEngineer
Posts: 216
Last visit: Thu Mar 23, 2023 5:45 pm
Has voted: 4 times

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

Post by ITEngineer »

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 */
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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

Post by jvierra »

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 9992 times
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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

Post by jvierra »

Here
sigtest.png
sigtest.png (47.9 KiB) Viewed 9980 times
is an example of the signature created with a template and the code above:
User avatar
ITEngineer
Posts: 216
Last visit: Thu Mar 23, 2023 5:45 pm
Has voted: 4 times

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

Post by ITEngineer »

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. $WordDoc = New-Object -ComObject Word.Application
  3. $doc = $WordDoc.Documents.Add('\\PRDFS01-VM\EmailSignature\Default Outlook Signature.dotx')
  4.  
  5. $props=@(
  6.     'company',
  7.     'l',
  8.     'displayname',
  9.     'mail',
  10.     'mobile',
  11.     'postalCode',
  12.     'streetAddress',
  13.     'st',
  14.     'title',
  15.     'telephonenumber'
  16. )
  17. $UserName = $env:username
  18. $searcher=[adsisearcher]"samAccountName=$($env:username)"
  19. $searcher.PropertiesToLoad.AddRange($props)
  20. $ADUser = $searcher.FindOne()
  21.  
  22. $ADDisplayName = $ADUser.Properties['displayname']
  23. $ADEmailAddress = $ADUser.Properties['mail']
  24. $ADTitle = $ADUser.Properties['title']
  25. $ADTelePhoneNumber = $ADUser.Properties['telephonenumber']
  26. $ADMobilePhone = $ADUser.Properties['mobile']
  27. $ADStreetAddress = $ADUser.Properties['streetAddress']
  28. $ADCity = $ADUser.Properties['l']
  29. $ADState = $ADUser.Properties['st']
  30. $ADPostal = $ADUser.Properties['postalCode']
  31.  
  32. If ($ADCity -ne '') {    
  33.     switch ( $ADPostal ) {
  34.         1234 { $ReplaceText = 'City A'         }
  35.         5678 { $ReplaceText = 'City B'         }
  36.         9999 { $ReplaceText = 'Outside'         }
  37.         default {$ReplaceText = ''}
  38.     }
  39.     $ReplaceText
  40.    }
  41.    Else {
  42.     $ReplaceText = 'Something wrong in the AD Attributes'
  43.    }
  44.  
  45. # set all bookmark texts by name
  46. $doc.Bookmarks['ST_DisplayName'].Range.Text = $ADDisplayName
  47. $doc.Bookmarks['ST_Title'].Range.Text = $ADTitle
  48. $doc.Bookmarks['ST_City'].Range.Text = $ReplaceText
  49. $doc.Bookmarks['ST_Telephone'].Range.Text = $ADTelePhoneNumber
  50. $doc.Bookmarks['ST_MobilePhone'].Range.Text = $ADMobilePhone
  51.  
  52. # generate signatures in Outlook
  53. $WordDoc.EmailOptions.EmailSignature.EmailSignatureEntries.Add('Default Outlook Signature',$doc.Range())
  54. $WordDoc.EmailOptions.EmailSignature.NewMessageSignature = 'Default Outlook Signature'
  55. $WordDoc.EmailOptions.EmailSignature.ReplyMessageSignature = 'Default Outlook Signature'
  56.  
  57. # abort document and close/quit
  58. $doc.Saved = $true
  59. $doc.Close()
  60. $WordDoc.Quit()
  61.  
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: 216
Last visit: Thu Mar 23, 2023 5:45 pm
Has voted: 4 times

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

Post by ITEngineer »

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 */
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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

Post by jvierra »

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.
This topic is 5 years and 10 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked