none
How to remove numbering from a new created style based on a numbered style with PowerShell RRS feed

  • Question

  • I am creating a Word template document with PowerShell (Word 2016 build 16.0.6741.2021, 32-bit and PowerShell 5.0.10586.122 running on Windows 10 Pro build 1511). I create a new style based on the "Heading 1" style. The only difference that I would like from "Heading 1" style is to remove the numbering. With the following PowerShell code I'm able to create the new style based on "Heading 1":

    $word = New-Object -ComObject Word.Application;
    $word.Documents.Add() | Out-Null;
    $style = $word.ActiveDocument.Styles.Add("New Style Name");
    $style.BaseStyle = $word.ActiveDocument.Styles.Item('Heading 1');
    $style.NextParagraphStyle = $word.ActiveDocument.Styles.Item('Normal');
    With VBA the following code will remove the numbering from the template:
    Me.Styles.Item("New Style Name").LinkToListTemplate Nothing
    However this doesn't work with PowerShell when using $Null value or any other value I tried as it expects a ListTemplate object:
    $style.LinkToListTemplate $Null;

    How can the numbering removed with PowerShell and the Word.Application ComObject?

    Thanks in advance,
    Dennis van den Akker

    Monday, April 4, 2016 8:07 PM

Answers

  • Hi Dennis,

    Thanks for the detail explanation for this issue. As far as I know, the PowerShell is different with VBA. The "Nothing" in VBA means the default value for that type.

    As a workaround, we can create a ListTemplate object in PowerShell, and use this variable as parameter.  Please let me know whether it works for you:

    $myVar=$Document.ListTemplates.Add($true)
    $style.LinkToListTemplate($myVar)
    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, April 11, 2016 12:10 PM
    Moderator

All replies

  • Hi Dennis,

    I noticed that the issue is code related. And our forum focuses on general discussion for Word IT Pro, I have moved your thread to development forum. Hope you can find the solution here.


    Regards,

    Winnie Liang
    TechNet Community Support


    Please mark the reply as an answer if you find it is helpful.

    If you have feedback for TechNet Support, contact tnmff@microsoft.com.

    Wednesday, April 6, 2016 11:17 AM
  • Hi Fei,

    Thank you for your reply, but I would like to modify a style within the document styles. I don't want to modify the style of a selection within the document.

    As mentioned in the original post it works with VBA, however I want to do the modification with PowerShell (through the COM Object model of Word).

    To rephrase the question. How to get VBA code 'Me.Styles.Item("New Style Name").LinkToListTemplate Nothing' to work within PowerShell and I have tried '$word.ActiveDocument.Styles.Item("New Style Name").LinkToListTemplate $Null' which doesn't work.

    Kind regards,
    Dennis

    Thursday, April 7, 2016 8:14 AM
  • Hi Dennis,

    Thanks for the detail explanation for this issue. As far as I know, the PowerShell is different with VBA. The "Nothing" in VBA means the default value for that type.

    As a workaround, we can create a ListTemplate object in PowerShell, and use this variable as parameter.  Please let me know whether it works for you:

    $myVar=$Document.ListTemplates.Add($true)
    $style.LinkToListTemplate($myVar)
    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, April 11, 2016 12:10 PM
    Moderator