Create word document with multiple tables from powershell

21/12/2012 23:28

Scripts in this two files create word document with multiple tables. Each table is for Sharepoint List Instance and fetches several properties. Input parameter is source folder, in which are found all Elements.xml files with List Instance.

 

ExportSiteListsToWord.ps1

#Create Word table for Sharepoint List Instances
function CreateListInstanceTable
{
param([object]$selection,
  [object]$document,
  [string]$listUrl,
  [string]$listTitle,
  [string]$listDescription,
  [string]$listTemplateId,
  [int]$autoFormatNumber)

#table for Lists:
#List URL
#Title
#Description
#List definition


#https://hopf.chem.brandeis.edu/yanglingfa/Qt/COM/wd/html/namespace_word.html
#table object members
#https://msdn.microsoft.com/en-us/library/bb259673(v=office.12).aspx
#list of word constants
#[enum]::GetNames([microsoft.office.interop.word.wdcolor])
#list of styles
#$document.Styles | select NameLocal
#word object model
#https://msdn.microsoft.com/en-us/library/vstudio/kw65a0we.aspx

#cls

$rows=4
$columns=2

$range = $selection.Range
$table=$document.Tables.add($range,$rows,$columns)
$table.Cell(1,1).Range.Font.Bold = $true
$table.Cell(1,2).Range.Font.Bold = $true
$table.Borders.Enable=$true

#static text
$table.cell(1,1).range.text = "Title"
$table.cell(2,1).range.text = "List URL"
$table.cell(3,1).range.text = "Description"
$table.cell(4,1).range.text = "List Definition"

#variable text
$table.cell(1,2).range.text = $listTitle
$table.cell(2,2).range.text = $listUrl
$table.cell(3,2).range.text = $listDescription
$table.cell(4,2).range.text = $listTemplateId

#$table.Autoformat(3)#($autoFormatNumber)

#end table and enter new line
$selection.EndKey(6)
$selection.TypeParagraph()

 }

GetListInstanceFromFile.ps1

#We want to get list definition attributes for technical design

# Go to source folder
# Find elements.xml what contains list definition
# Load it as xml file
# Read attributes
# Write it to console
# Create word document, table and fill it.

#Settings
cls
$ErrorActionPreference="Stop"
#Include function
. .\ExportSiteListsToWord.ps1

#Input variables

#source folder
$sourceFolder="c:\Documents"

#List Templates
$pattern = "

#Find all files with $listDefinitionFiles = Get-ChildItem $sourceFolder -recurse |
             Select-String -pattern $pattern |
             group path |
             select name|
             Where-Object {-not($_.Name -match "\\pkg")}|
             Where-Object {$_.Name -match "Elements.xml"}
Write-Host "'$pattern' found in following files: "             
$listDefinitionFiles
Write-Host ""
Write-Host " =================== List Instances ======================="
Write-Host ""

#create word document
$word = New-Object -ComObject word.application
$word.visible = $true
$document = $word.documents.add()
$selection=$word.Selection

$selection.Style="Heading 1"
$selection.TypeText("Lists")
$selection.TypeParagraph()
$selection.TypeParagraph()

$i=1
foreach($fileName.Name in $listDefinitionFiles)
{
 write-host "File name: "$fileName.Name
 Write-Host ""
 $xmlListDef=[xml](Get-Content -Path $fileName.Name)
 $listDefinition=$xmlListDef.Elements.ListInstance
 
 
 CreateListInstanceTable -document $document -listDescription $listDefinition.Description -listTemplateId $listDefinition.TemplateType -listTitle $listDefinition.Title -listUrl $listDefinition.Url -selection $selection -autoFormatNumber $i
 Write-Host "List Name: "$listDefinition.Title
 Write-Host "List Url: "$listDefinition.Url
 Write-Host "Description: "$listDefinition.Description
 Write-Host "Template ID: "$listDefinition.TemplateType
 Write-Host "---------------------------"
 $i++
}

#save word document
 $filename = "C:\temp\List from PS.docx"
 $document.SaveAs([REF]$filename)
 #$document.Close()

 

Output in word (according to styles)

Lists

 

 

Title

SharePointProject - ListInstance1

List URL

Lists/SharePointProject-ListInstance1

Description

My List Instance

List Definition

10000

 

 

Title

Test List Instance1

List URL

Lists/TestistInstance1

Description

My List Instance

List Definition

10008

 

 

Title

TestInstance

List URL

Lists/TestInstance

Description

Test instance from template 10008

List Definition

10008