Setting the style in Word file RRS feed

  • Question

  • Hi guys,

    I'm a newer to powershell,today I followed this script to create a word document:

    $Word=New-Object -ComObject Word.Application
    $Word.Visible = $True
    $Selection.Style = 'Title'

    after typing

    $Selection.Style = 'Title'

    there was an error:

    + $Selection.Style = 'Title'
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : OperationStopped: (:) [], COMException
        + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException

    seems like can't assign a string to the Style property,

    can anybody tell me how to change it? My Powershell version :5.1

    Thanks a lot

    • Edited by Sean_82 Wednesday, May 2, 2018 1:02 AM
    Tuesday, May 1, 2018 4:03 PM


  • if you want to use the enums you must use the interop to access them

    $wd.Selection.Style = $wd.ActiveDocument.Styles[[Microsoft.Office.Interop.Word.WdBuiltinStyle]::wdStyleHeading1]


    • Marked as answer by Sean_82 Wednesday, May 2, 2018 5:13 AM
    Wednesday, May 2, 2018 4:57 AM

All replies

  • Please post code correctly in this forum.  Do not post colorized code.  Use the code posting tool provided.


    Tuesday, May 1, 2018 4:42 PM
  • What is "Title" supposed to be doing?  Are you trying to set the current style to a pre-defined style?


    Tuesday, May 1, 2018 4:46 PM
  • https://msdn.microsoft.com/en-us/vba/word-vba/articles/style-object-word

    Note that your exception is a Word exception and not a PowerShell exception.  It refers to the Word session you are accessing.


    • Edited by jrv Tuesday, May 1, 2018 5:53 PM
    Tuesday, May 1, 2018 5:51 PM
  • Hi jrv,thanks for your reply,

    I thought maybe there is a built-in function that can convert string to enum's int value :) cause many article used 

    $Selection.Style = 'Title'
    to set the style or the color of text input,and that seems correct for their script? 

    Wednesday, May 2, 2018 2:31 AM
  • The "Style" property is an object and can only be assigned to a style object.

    As per the article I linked above.

    $wd = New-Object -ComObject Word.Application
    $wd.Visible = $true
    $doc = $wd.Documents.Add()
    $wd.Selection.Style = $wd.ActiveDocument.Styles('Title')


    • Edited by jrv Wednesday, May 2, 2018 2:43 AM
    Wednesday, May 2, 2018 2:42 AM
  • Hi jrv,thanks for your reply,

    I changed this line to:

    $wd.Selection.Style = $wd.ActiveDocument.Styles('wdStyleHeading1')

    got same error,

    but when I used 

    $wd.Selection.Style = $wd.ActiveDocument.Styles(1)

    everything goes fine,

    so the Styles function can only accept INT value as the parameter? 

    following is the script provided in that article 

    Set myStyle = ActiveDocument.Styles.Add(Name:="Bolded", _ 
    myStyle.Font.Bold = True 
    Selection.Range.Style = "Bolded"
    I noticed that this script assigned a string value to the Selction.Range.Style, but I still get wrong message :(

    Wednesday, May 2, 2018 4:24 AM
  • You have to use styles that exist.  To test  style just reference its style name.

    $wd.ActiveDocument.Styles('Heading 1')
    $wd.ActiveDocument.Styles('Heading 2')
    $wd.ActiveDocument.Styles('Heading 3')

    To list all named styles:

    $wd.ActiveDocument.Styles | select NameLocal

    Remember that this is not VBA and you are not writing an ADD-IN with VSTO. It is just "Word Interop" and the Word object model.


    Wednesday, May 2, 2018 4:50 AM
  • if you want to use the enums you must use the interop to access them

    $wd.Selection.Style = $wd.ActiveDocument.Styles[[Microsoft.Office.Interop.Word.WdBuiltinStyle]::wdStyleHeading1]


    • Marked as answer by Sean_82 Wednesday, May 2, 2018 5:13 AM
    Wednesday, May 2, 2018 4:57 AM
  • This is a safer and easier way to build a document.

    $p = $doc.Paragraphs.Add()
    $p.Style = $doc.Styles('Title')
    $p.Range.Text = 'Hello'
    $p.Alignment = 1  # center 

    Word documents are not "typed" but are assembled from page, paragraph and other objects that combine into a "Story"


    Wednesday, May 2, 2018 5:07 AM
  • Hi jrv,

    You are right~ I think I confused the Word object model and VB data type.

    Thank you ~  

    Wednesday, May 2, 2018 5:21 AM
  • Not VB.  VBA.  In Office VBA is able to look up these items as key words in the language implementation.  C#, VB and PowerShell cannot do that.  Even in C# we have to use the type before the enum identifier:  wdBultinStyle.wdStyleHeader1.  In VBA this is not needed for any field as the VBA parser will look up the correct enum and display the available values.

    Office is COM and PowerShell is Net.  Net support COM but does not look up enums for Office since COM properties are not strongly type.  Most are just declared as Variant.


    Wednesday, May 2, 2018 5:37 AM