Open a Word-Doc with activated Line-Numbers RRS feed

  • Question

  • Dear scripting-experts,

    I want to print code with Powershell (PHP etc.) including line-numbers. The idea is to rename .php to .txt and print it with Office/Word. That works fine, but how can I open the Word-file so that line-numbers are automatically activated?  I know how to format things like this:

    $WordObj.ActiveDocument.Content.Font.Size = 12
    $WordObj.ActiveDocument.Content.Font.Name = "Verdana"

    What would be a line for activated line-numbers?


    What would be the code to add the full document-path in the header of the document?

    What would be the code to add the content of a variable (used in powershell) to the document-header?

    Goal is to print the working-results from a student-class.




    • Edited by ScriptingHomer Monday, November 17, 2014 8:55 AM
    • Moved by Bill_Stewart Thursday, January 8, 2015 8:42 PM Move to more appropriate forum
    Monday, November 17, 2014 7:52 AM

All replies

  • $wd.ActiveDocument.PageSetup.LineNumbering.Active=-1


    Monday, November 17, 2014 12:10 PM
  • Thanks, this works perfectly, see my script below.

    There is one question remaining: How can I set the document that it shows "Site 1 of 2" or something like that?

    $pfad1 = "The best scripting language in the world!"
    $pfad2 = "    "                                                                                                                           "
    [ref]$SaveOption = "" -as [type]
    $application = New-Object -ComObject word.application
    $application.visible = $true
    $doc = $application.activeDocument
    Remove-Variable -name application

    Monday, November 17, 2014 4:04 PM
  • You do not need to use a script for this.  Everything is built into Word.

    Create a template with all settings you need t oformat the output.  Use the Word commandline options to load the template and then the PS1 file.  THe pront command goes on the commandline.

    You can aslo create a context menu that calls word to print the document.

    PowerShell is the hardest way to do this:


    Monday, November 17, 2014 5:49 PM
  • That sounds good. But then I always have an external Word-doc, right? Or can I merge it all to one ps1-file?
    Monday, November 17, 2014 8:47 PM
  • How would this be correct:


    Is there a doku where I can read a reference? I only find stuff for VBA.

    Monday, November 17, 2014 8:49 PM
  • That sounds good. But then I always have an external Word-doc, right? Or can I merge it all to one ps1-file?

    No - you have a template and apply it to the file you want to format and print.  It does not create a new file.


    Monday, November 17, 2014 8:50 PM
  • How would this be correct:


    Is there a doku where I can read a reference? I only find stuff for VBA.

    You would be better of posting in the Word VBA developer forum or jus tbuild a template and apply it.  With a template you don.t need to understand the technology of Office.

    You can only update pagesetup after you load the document.


    Monday, November 17, 2014 9:08 PM
  • Her is a starter but you really should take some time out to learn how to use the search engines available on the modern Internet.  They will find things faster than you can ask the question.

    This is what I found by just pasting your question into the search box:


    Monday, November 17, 2014 9:15 PM
  • Sorry, but for me it is not simple as you may think. Your link points to a site with VBA-Standards that are not usable in PS for me. An exampel:


    works (as I found out with trial and error). The solution from your link (I found similar resources using google ;-))

    works not:

    ActiveDocument.PageSetup.LineNumbering.Active = True

    The usage of True (that seems to be a VBA but not a PS-Standard) leades to this:

    Die Benennung "True" wurde nicht als Name eines Cmdlet, einer Funktion, einer Skriptdatei oder eines ausführbaren Programms erkannt. Überprüfen Sie die Schreibweise des Na
    mens, oder ob der Pfad korrekt ist (sofern enthalten), und wiederholen Sie den Vorgang.
    Bei C:\Users\Homer\Desktop\psdrucktest3.ps1:15 Zeichen:69
    + $application.ActiveDocument.PageSetup.LineNumbering.RestartMode=True <<<<
        + CategoryInfo          : ObjectNotFound: (True:String) [], CommandNotFoundException
        + FullyQualifiedErrorId : CommandNotFoundException

    However, I am still thankfull for your help.

    Monday, November 17, 2014 9:30 PM
  • You have to learn first PowerShell.  Then you need to learn Office automation and VBA.  All documentation for office is in VBA.  There is not PowerShell documentation.  You also need to understand the more technical side or coding.  It is not easy.  If you are not trained in programming then it may be out-of-reach.


    In VBS we use True and False which would normally default to $true and $false in PowerShell.  With this property that is not the case.  RestartMode is also not a Boolean so I do not know why you think that it would be.

    wdRestartContinuous is an enumeratied constant and exists by default in VBA but not is Powershell. You have to find these things on your own. The Office docs are very scattered and poorly organized.

    We can use Interop. 

    Add-Type -AssemblyName Microsoft.Office.Interop.Word

    Now we get the enums:


    I just use 2.

    You can also use this:

    New-Variable -Name wdTrue -Value -1 -Option Constant -Description 'Office automation TRUE' -Visibility Public -Force -Scope global
    New-Variable -Name wdFalse -Value 0 -Option Constant -Description 'Office automation FALSE' -Visibility Public -Force -Scope global


    • Edited by jrv Monday, November 17, 2014 10:09 PM
    Monday, November 17, 2014 10:08 PM
  • You can also use the Visual Studio documentation for Office Add-Ins,   That documentation is a bit better but requires an understanding of C# or VB.Net.

    I recommend sticking with templates and learning VBA if you are planning on automating office and have zero programming experience.  The Office community site and the VBA developers will get you moving along quickly.


    Monday, November 17, 2014 10:12 PM