OWSUG.ca

Welcome to Ottawa's Windows Server User Group Community!
Welcome to OWSUG.ca Sign in | Join | Help
in Search

PowerShell and Word

Last post 12-01-2007, 11:07 PM by KirkAMunro. 1 replies.
Sort Posts: Previous Next
  •  12-01-2007, 2:08 PM 518

    PowerShell and Word

    Do anyone have an Sample code to create a new Word Doc and create tables within the doc?

  •  12-01-2007, 11:07 PM 519 in reply to 518

    Re: PowerShell and Word

    Here's a very-lightly-tested script that I just threw together in the last 20-25 minutes.  It simply gets the services on your system and outputs them to a Word document in a table within that document.  It then saves the file to C:\services.doc and closes down Word.  This is just a rough example.  It could likely be better written.

    # Gather our data that we want to write in a Word document
    $services = @(Get-Service)

    # Launch instance of Microsoft Word
    $msWord = New-Object -Com Word.Application

    # Create new document
    $wordDoc = $msWord.Documents.Add('Normal.dot',$false,0,$true)

    # Make word visible (optional)
    #$msWord.Visible = $true

    # Activate the new document
    $wordDoc.Activate()

    # Create a new table large enough to hold the data we have
    $docTable = $wordDoc.Tables.Add($wordDoc.Application.Selection.Range,$services.Count + 1,3)

    # Insert the column headers into the table
    $columnHeaders = @('Status','Name','DisplayName')
    for ($columnIndex = 1; $columnIndex -le 3; $columnIndex++) {
       
    $cell = $docTable.Cell(1,$columnIndex).Range
       
    $cell.Font.Name="Arial"
       
    $cell.Font.Bold=$true
       
    $cell.InsertAfter($columnHeaders[$columnIndex - 1])
    }

    # Load the data into the table
    for ($rowIndex = 2; $rowIndex -le ($services.Count + 1); $rowIndex++) {
       
    $dataIndex = $rowIndex - 2
       
    $rowData = @($services[$dataIndex].Status, $services[$dataIndex].Name, $services[$dataIndex].DisplayName)
       
    for ($columnIndex = 1; $columnIndex -le 3; $columnIndex++) {
           
    $cell = $docTable.Cell($rowIndex,$columnIndex).Range
           
    $cell.Font.Name="Arial"
           
    $cell.InsertAfter($rowData[$columnIndex - 1])
        }
    }

    # Save the document to disk and close it
    $filename = 'C:\services.doc'
    $wordDoc.SaveAs([REF]$filename)
    $wordDoc.Close()

    # Exit our instance of word
    $msWord.Application.Quit()

    $services = @(Get-Service)

    # Launch instance of Microsoft Word
    $msWord = New-Object -Com Word.Application

    # Create new document
    $wordDoc = $msWord.Documents.Add('Normal.dot',$false,0,$true)

    # Make word visible (optional)
    #$msWord.Visible = $true

    # Activate the new document
    $wordDoc.Activate()

    # Create a new table large enough to hold the data we have
    $docTable = $wordDoc.Tables.Add($wordDoc.Application.Selection.Range,$services.Count + 1,3)

    # Insert the column headers into the table
    $columnHeaders = @('Status','Name','DisplayName')
    for ($columnIndex = 1; $columnIndex -le 3; $columnIndex++) {
       
    $cell = $docTable.Cell(1,$columnIndex).Range
       
    $cell.Font.Name="Arial"
       
    $cell.Font.Bold=$true
       
    $cell.InsertAfter($columnHeaders[$columnIndex - 1])
    }

    # Load the data into the table
    for ($rowIndex = 2; $rowIndex -le ($services.Count + 1); $rowIndex++) {
       
    $dataIndex = $rowIndex - 2
       
    $rowData = @($services[$dataIndex].Status, $services[$dataIndex].Name, $services[$dataIndex].DisplayName)
       
    for ($columnIndex = 1; $columnIndex -le 3; $columnIndex++) {
           
    $cell = $docTable.Cell($rowIndex,$columnIndex).Range
           
    $cell.Font.Name="Arial"
           
    $cell.InsertAfter($rowData[$columnIndex - 1])
        }
    }

    # Save the document to disk and close it
    $filename = 'C:\services.doc'
    $wordDoc.SaveAs([REF]$filename)
    $wordDoc.Close()

    # Exit our instance of word
    $msWord.Application.Quit()

    $services = @(Get-Service)

    # Launch instance of Microsoft Word
    $msWord = New-Object -Com Word.Application

    # Create new document
    $wordDoc = $msWord.Documents.Add('Normal.dot',$false,0,$true)

    # Make word visible (optional)
    #$msWord.Visible = $true

    # Activate the new document
    $wordDoc.Activate()

    # Create a new table large enough to hold the data we have
    $docTable = $wordDoc.Tables.Add($wordDoc.Application.Selection.Range,$services.Count + 1,3)

    # Insert the column headers into the table
    $columnHeaders = @('Status','Name','DisplayName')
    for ($columnIndex = 1; $columnIndex -le 3; $columnIndex++) {
       
    $cell = $docTable.Cell(1,$columnIndex).Range
       
    $cell.Font.Name="Arial"
       
    $cell.Font.Bold=$true
       
    $cell.InsertAfter($columnHeaders[$columnIndex - 1])
    }

    # Load the data into the table
    for ($rowIndex = 2; $rowIndex -le ($services.Count + 1); $rowIndex++) {
       
    $dataIndex = $rowIndex - 2
       
    $rowData = @($services[$dataIndex].Status, $services[$dataIndex].Name, $services[$dataIndex].DisplayName)
       
    for ($columnIndex = 1; $columnIndex -le 3; $columnIndex++) {
           
    $cell = $docTable.Cell($rowIndex,$columnIndex).Range
           
    $cell.Font.Name="Arial"
           
    $cell.InsertAfter($rowData[$columnIndex - 1])
        }
    }

    # Save the document to disk and close it
    $filename = 'C:\services.doc'
    $wordDoc.SaveAs([REF]$filename)
    $wordDoc.Close()

    # Exit our instance of word
    $msWord.Application.Quit()

    --
    Kirk Munro
    Poshoholic
    http://poshoholic.com

View as RSS news feed in XML
Powered by Community Server, by Telligent Systems