Creating Dummy Documents via Office COM Object Model

During a SharePoint project, it’s very useful to have a bunch of dummy documents at your disposal, to test various functions. In SharePoint 2013, there’s a whole bunch of things you will need to test in various environments and browsers, where having a number of unique test documents will be useful. This includes Drag and Drop, Search, Office Web Apps, and any customisations made to document libraries, such as custom Display Templates, Grouping, and Paging. To save me having to create a large number of unique documents I decided to have a play with the Word COM Object Model and PowerShell.

COM Object Model

What is the OM Object Model? The COM Object Model allows you to program against Office Applications, allowing you to automate Office Applications. Using New-Object in PowerShell, it becomes possible to create office documents using PowerShell. You can find the reference on MSDN here: http://msdn.microsoft.com/en-us/library/Microsoft.Office.Interop.Word(v=office.14).aspx

New-DummyDocument

When I started this script I had a few goals which I wanted this to achieve. Each document must:

  1. Be Unique
  2. Have content in​
  3. Vary in size in comparison to other randomly generated content

In the end, I ended up with the PowerShell Function New-DummyDocument, which can be used like so:

1
New-DummyDocument -DocumentName 'Test Document 1' -Path 'c:\documents\' -Headings 2 -Paragraphs 1 -Keyword 'Zebra'

This outputs a document called “Test Document 1.docx” in the “c:\Documents\”. The document has 2 headings, each with 1 paragraph in. The paragraphs are “Lorem Ipsum” text, generated from a free API. Below is the commented script, which you can also download here. If you have any ideas on how to improve it, would do it differently, or would add anything new, leave me a comment, or email me ross@rbradbrook.co.uk

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<#
.Synopsis
Creates a dummy Word Document
.EXAMPLE
New-DummyDocument -DocumentName 'TestDoc1' -Path "c:\test\" -Headings 1 -Paragraphs 1 -Keyword "Zebra"

.EXAMPLE
New-DummyDocument -DocumentName 'Test Doc 1','Test Doc 2','Test Doc 3' -Path "c:\test\" -Headings 1 -Paragraphs 1 -Keyword "Zebra"
#>
function New-DummyDocument
{
Param(
$DocumentName,​
$Path,
[int]$Headings,
[int]$Paragraphs,
[string]$Keyword)

# Loop through all of the documents (Usefull if you need to create lots of documents with different names)
foreach($docname in $DocumentName){
#Opens up Word
$word = New-Object -ComObject Word.Application
#Creates a new document (Same as pressing File > New )
$doc = $word.Documents.Add()
#Put the cursor at the top of the page
$selection = $word.Selection
#Make the text a heading
$selection.Style = "Heading 1"
#Write the name of the document as a heading
$selection.TypeText($docname)
#If a Keyword has been specified, add this into the heading. Useful for testing search
if($Keyword){
$selection.TypeText(" " + $Keyword);
}
#Start a new paragraph
$selection.TypeParagraph()
#Allows the creation of multiple headings. Usefull to test large documents
for($i = 0; $i -lt $Headings; $i++)
{
#Set the style to Heading 2
$selection.Style = "Heading 2"
#Write out heading 2
$selection.TypeText("This is Heading $i")
#Start a new paragraph
$selection.TypeParagraph()
#Get some dummy data, and save as a variable. Uses an API on http://loripsum.net/
$content = Invoke-WebRequest -Uri http://loripsum.net/api/$Pargraphs/plaintext
#Set the style to Normal
$selection.Style = "Normal"
#Processes the text. Turns each paragraph from the API into a paragarph in word
$textArray = $content.Content.Split("`n")
for($j = 0; $j -lt $textArray.Length; $j++)
{
#Ensures the string has content
if($textArray[$j].length -gt 0)
{
#Type the paragraph
$selection.TypeText($textArray[$j])
#Start a new paragraph
$selection.TypeParagraph()
}
}

}
#Tests if the path exists, if not creates it
if((Test-Path $Path) -eq $false){
New-Item $Path -ItemType directory
write-host ($Path + ' Created')
}
#Saves the document.(same as pressing file > save)
$doc.SaveAs([ref]"$Path\$docname.docx")
#Closes word
$word.Quit()
}
}