none
Beginner questions about PowerShell and Word

    Question

  • Two questions:

     
     
    1) Support forums:
     
    Is this the appropriate forum for what I’m trying to do?
     
    I’m new to PowerShell. I want to use it for some simple automation, of Word macros and Mail Merge processes and the like. I don’t really know what I’m doing, and I don't want to acquire great expertise. I just want to find some examples which I can clone from. Or is PowerShell too complex for this approach?
     
    Googling led me to the website PowerShell.com. I downloaded a useful free e-book, Master PowerShell by Dr Tobias Weltner, and the forum contains a lot of helpful-looking posts but I have not succeeded in joining it and I don’t get any replies from its support email address, info@powershell.com.
     
     
    2) Specific question about PowerShell and Word:
     
    How can I find out how to run Word commands from PowerShell?
     
    I want to write a PowerShell script which edits a Word document. I’m starting from a Word macro and trying to convert its commands to PowerShell. TypeText and TypeParagraph are simple enough, but I can’t get anywhere with MoveDown and its parameters. I can’t find anything on the web which tells me about the commands to edit a Word document in PowerShell. Once I’ve got over this I shall be trying to set up a loop and outputting multiple Word files or PDFs.
     
    This is the script which I have got to work so far. It opens an existing file, makes a change or two, and saves it under a new name.
     
    # Word21.ps1
    $word = New-Object -ComObject "Word.Application"
    $word.Visible = $True
    $filename = 'C:\Users\Seamus\Dropbox\PowerShell\Testing\MOC Collection PDFs design 2016-03-30.docx'
    $doc = $word.documents.open($filename)
    $selection=$word.Selection
    $selection.TypeText("string")
    $selection.TypeParagraph()
    $selection.TypeText("more string")
    $selection.TypeParagraph()
    $doc.SaveAs([ref]"C:\Users\Seamus\Dropbox\PowerShell\Testing\MOC 02.docx")
    $doc.Close()
    $word.Quit()
     
    This is the Word macro which I shall be working from. There are text items which will need to be picked up from an Excel file.
     
    ' Insert two new lines.
        Selection.TypeParagraph
        Selection.TypeParagraph
    ' Select and delete next two old lines.
        Selection.MoveDown Unit:=wdLine, Count:=2, Extend:=wdExtend
        Selection.Delete Unit:=wdCharacter, Count:=1
    ' Move to picture and delete it.
        Selection.MoveDown Unit:=wdLine, Count:=1
        Selection.Delete Unit:=wdCharacter, Count:=1
    ' Go to top left.
        Selection.HomeKey Unit:=wdStory
    ' Type new line 1.
        Selection.TypeText Text:="London frieze"
    ' Replace brackets by quotes.
        Selection.Find.ClearFormatting
        Selection.Find.Replacement.ClearFormatting
        With Selection.Find
            .Text = "["
            .Replacement.Text = """"
            .Forward = True
            .Wrap = wdFindContinue
        End With
        Selection.Find.Execute Replace:=wdReplaceAll
        With Selection.Find
            .Text = "]"
            .Replacement.Text = """"
            .Forward = True
            .Wrap = wdFindContinue
        End With
        Selection.Find.Execute Replace:=wdReplaceAll
    ' Type new line 2.
        Selection.MoveDown Unit:=wdLine, Count:=1
        Selection.TypeText Text:="Rayon" & vbTab & "82 cm high x 180 cm wide" & vbTab & "R 1"
    ' Insert picture.
        Selection.MoveDown Unit:=wdLine, Count:=2
        Selection.InlineShapes.AddPicture FileName:= _
            "C:\Users\Seamus\Dropbox\MOC gallery\MOC 2000 PIX JPEGS\Rayon\MOC R 1.jpg", _
            LinkToFile:=False, SaveWithDocument:=True
    ' Save.
        ActiveDocument.Save
    ' Create PDF.
        ActiveDocument.ExportAsFixedFormat OutputFileName:= _
            "C:\Users\Seamus\Dropbox\MOC PDFs\Rayon\MOC R 1.pdf" _
            , ExportFormat:=wdExportFormatPDF, OpenAfterExport:=False, OptimizeFor:= _
            wdExportOptimizeForPrint, Range:=wdExportAllDocument, From:=1, To:=1, _
            Item:=wdExportDocumentContent, IncludeDocProps:=False, KeepIRM:=False, _
            CreateBookmarks:=wdExportCreateHeadingBookmarks, DocStructureTags:=True, _
            BitmapMissingFonts:=False, UseISO19005_1:=False
    ' Go to top left.
        Selection.HomeKey Unit:=wdStory
    • Moved by Bill_Stewart Wednesday, June 8, 2016 1:02 PM Move to more appropriate forum
    Wednesday, June 8, 2016 10:04 AM

Answers

All replies

  • Just call the word macro from PowerShell.

    $word.Run('MyMacro')


    \_(ツ)_/

    Wednesday, June 8, 2016 1:06 PM
  • You seem serious and eager. However, I'd like you to rethink what you want to accomplish and phrase that in detail.
    You cannot just throw yourself in and expect yourself to be an expert in no time.

    First of all you need to experiment with Powershell in all its glory and failure. Start with a VERY simple example:

    $myName = "Seamus O_Connell"
    Write-Host $myName -ForegroundColor Cyan

    etc ...


    Best regards, George

    Wednesday, June 8, 2016 2:02 PM
  • Thanks for this. It works.

    However, if I understand what's going on, it means that I shall have to put the looping code, which picks up values from an Excel file, into the Visual Basic macro rather the PowerShell script.

    For which the change of Forum seems appropriate.

    Wednesday, June 8, 2016 5:10 PM
  • Thanks for this. It works.

    However, if I understand what's going on, it means that I shall have to put the looping code, which picks up values from an Excel file, into the Visual Basic macro rather the PowerShell script.

    For which the change of Forum seems appropriate.

    It is not possible to decode what you are trying tom ask.  Initially you asked how to execute a Word macro with PowerShell.  Are you asking a new question?

    Perhaps this would be easier if you learned a bit of PowerShell and how to use Word.  Without those to things you will not be able to do this.


    \_(ツ)_/

    Wednesday, June 8, 2016 5:25 PM