-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathHow-To-MSWordFunctions.ps1
110 lines (93 loc) · 4.12 KB
/
How-To-MSWordFunctions.ps1
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#region Below functions from the following link:
#https://blog.pythian.com/automation-powershell-and-word-templates-let-the-technician-do-tech/
#Function to open a word document:
Function OpenWordDoc($Filename){
$Word=NEW-Object –comobject Word.Application
Return $Word.documents.open($Filename)
}
#Function to save a word document as:
Function SaveAsWordDoc($Document, $FileName){
$Document.Saveas([REF]$Filename)
$Document.close()
}
#Function to replace a text tag (<TAG_YOUR_NAME>) with something else
Function ReplaceTag($Document, $FindText, $ReplaceWithText){
$FindReplace=$Document.ActiveWindow.Selection.Find
$matchCase = $false;
$matchWholeWord = $true;
$matchWildCards = $false;
$matchSoundsLike = $false;
$matchAllWordForms = $false;
$forward = $true;
$format = $false;
$matchKashida = $false;
$matchDiacritics = $false;
$matchAlefHamza = $false;
$matchControl = $false;
$read_only = $false;
$visible = $true;
$replace = 2;
$wrap = 1;
$FindReplace.Execute($findText, $matchCase, $matchWholeWord, $matchWildCards, $matchSoundsLike, $matchAllWordForms, $forward, $wrap, $format, $replaceWithText, $replace, $matchKashida ,$matchDiacritics, $matchAlefHamza, $matchControl) | Out-Null
}
#Add an image to a bookmark
Function AddImage($Document, $BookmarkName, $ReplaceWithImage){
$FindReplace=$Document.ActiveWindow
$FindReplace.Selection.GoTo(-1,0,0,$Document.Bookmarks.item("$BookmarkName"))
$FindReplace.Selection.InlineShapes.AddPicture("$replacewithImage")
}
<#Sample using the above functions:
$TemplateFile = “C:\reports\Template_Report.docx”
$FinalFile = “C:\reports\FinalReport.docx”
# Open template file
$Doc=OpenWordDoc -Filename $TemplateFile
# Replace text tags
ReplaceTag –Document $Doc -FindText ‘<client_name>’ -replacewithtext “Pythian”
ReplaceTag –Document $Doc -FindText ‘<server_name>’ -replacewithtext “WINSRV001”
# Add image
AddImage –Document $Doc -BookmarkName ‘img_SomeBookmark’ -ReplaceWithImage “C:\reports\img.png”
# Save FInal Report
SaveAsWordDoc –document $Doc –Filename $FinalFile
#>
#endregion
#endregion End of Functions from link indicated.
<# BELOW : Tried to check if doc called is already opened and trying to use the
opened one ... not very accurrate method, commenting for now.
Error handling is in the content of the $Doc = $MSWord.Documents.Open($Docfile)
result : if $false => nothing is in $Doc, meaning the opening failed.#>
<#
Title1 "Checking for already opened documents"
Try {
$MSWord = [Runtime.Interopservices.Marshal]::GetActiveObject('Word.Application')
$DocCount = $MSWord.Documents.count
write-host "Currently $DocCount Word docs opened... checking if a doc named $DocName already exists..." -BackgroundColor yellow -ForegroundColor red
$CountDocs = 0
Foreach ($Doc in $MSWord.Documents) {
$COuntDocs++
If ($($Doc.Name) -eq $DocNAme) {
Write-Host "A document with the same name is already opened ... please close it first" -ForegroundColor Red -BackgroundColor Yellow
Exit
}
}
Write-Host "Found $CountDocs currently opened, no docs with name $DocName opened. Creating a new Word instance !"
Write-Host "Cleaning variables..."
$null = [System.Runtime.InteropServices.Marshal]::ReleaseComObject([System.__ComObject]$MSword)
[gc]::Collect()
[gc]::WaitForPendingFinalizers()
Remove-Variable MSword
}
Catch {
Write-Host "No Word instance opened. Creating a new Word Instance"
}
#>
#Text FormFields are also referred to as text Bookmarks in Word
#The advantage of BookMarks is that it's quicker to find and to update, and you can sort by name and by position
#in the word document.
#
#$Bookmarks = $Doc.Bookmarks | Sort Start
#Foreach ($BM in $Bookmarks) {
# #$BM.Name | Out-Host
# $BM | fl * | out-host
#}
#
#Using FormFields for now until I get what's the best to use in Word automation...