1
1
function Set-WordTextReplace {
2
2
<#
3
3
. SYNOPSIS
4
- Short description
4
+ Provides ability to search and replace certain words, phrases, or regular expressions in a word file.
5
5
6
6
. DESCRIPTION
7
- Long description
7
+ Provides ability to search and replace certain words, phrases, or regular expressions in a word file.
8
8
9
9
. PARAMETER Paragraph
10
- Parameter description
10
+ Provide paragraph to search for text
11
11
12
12
. PARAMETER SearchValue
13
- Parameter description
13
+ Value to search for
14
14
15
15
. PARAMETER ReplaceValue
16
- Parameter description
16
+ Value to replace with
17
17
18
18
. PARAMETER TrackChanges
19
- Track changes
19
+ Track changes, default is off
20
20
21
21
. PARAMETER RegexOptions
22
- Parameter description
22
+ The regex options to use when searching for the search value. Default is none.
23
23
24
24
. PARAMETER NewFormatting
25
25
The formatting to apply to the text being inserted.
28
28
The formatting that the text must match in order to be replaced.
29
29
30
30
. PARAMETER MatchFormattingOptions
31
- How should formatting be matched?
31
+ How should formatting be matched? ExactMatch (default) or SubsetMatch
32
32
33
33
. PARAMETER escapeRegEx
34
34
True if the oldValue needs to be escaped, otherwise false. If it represents a valid RegEx pattern this should be false.
40
40
Remove empty paragraph
41
41
42
42
. EXAMPLE
43
- An example
43
+ $FilePath = "C:\Users\przemyslaw.klys\OneDrive - Evotec\Desktop\Word.docx"
44
+ $FilePath1 = "C:\Users\przemyslaw.klys\OneDrive - Evotec\Desktop\Word1.docx"
45
+ $doc = Get-WordDocument -FilePath $FilePath
46
+ $word = "Sample"
47
+ $formatObj = New-Object Xceed.Document.NET.Formatting
48
+ $formatObj.FontColor = "Red"
49
+ foreach ($p in $doc.Paragraphs) {
50
+ Set-WordTextReplace -Paragraph $p -SearchValue $word -ReplaceValue $word -NewFormatting $formatObj -Supress $false
51
+ }
52
+ Save-WordDocument -Document $doc -FilePath $FilePath1
44
53
45
54
. NOTES
46
55
General notes
47
56
#>
48
57
[CmdletBinding ()]
49
58
param (
50
- [parameter (ValueFromPipelineByPropertyName , ValueFromPipeline )][Xceed.Document.NET.InsertBeforeOrAfter ] $Paragraph ,
59
+ [parameter (ValueFromPipelineByPropertyName , ValueFromPipeline , Mandatory )][Xceed.Document.NET.InsertBeforeOrAfter ] $Paragraph ,
51
60
[string ] $SearchValue ,
52
- [string ] $ReplaceValue ,
61
+ [alias ( ' NewValue ' )][ string ] $ReplaceValue ,
53
62
[switch ] $TrackChanges ,
54
- [System.Text.RegularExpressions.RegexOptions ] $RegexOptions ,
55
- [Xceed.Document.NET.Formatting ] $NewFormatting ,
56
- [Xceed.Document.NET.Formatting ] $MatchFormatting ,
57
- [Xceed.Document.NET.MatchFormattingOptions ] $MatchFormattingOptions ,
63
+ [System.Text.RegularExpressions.RegexOptions ] $RegexOptions = [ System.Text.RegularExpressions.RegexOptions ]::None ,
64
+ [Xceed.Document.NET.Formatting ] $NewFormatting = [ Xceed.Document.NET.Formatting ]::new() ,
65
+ [Xceed.Document.NET.Formatting ] $MatchFormatting = [ Xceed.Document.NET.Formatting ]::new() ,
66
+ [Xceed.Document.NET.MatchFormattingOptions ] $MatchFormattingOptions = [ Xceed.Document.NET.MatchFormattingOptions ]::ExactMatch ,
58
67
[switch ] $EscapeRegEx ,
59
68
[switch ] $UseRegExSubstitutions ,
60
69
[switch ] $RemoveEmptyParagraph ,
64
73
# void ReplaceText(string findPattern, System.Func[string,string] regexMatchHandler, bool trackChanges, System.Text.RegularExpressions.RegexOptions options, Xceed.Document.NET.Formatting newFormatting, Xceed.Document.NET.Formatting matchFormatting, Xceed.Document.NET.MatchFormattingOptions fo, bool removeEmptyParagraph)
65
74
if ($Paragraph ) {
66
75
$Paragraph = $Paragraph.ReplaceText ($SearchValue , $ReplaceValue , $TrackChanges.IsPresent , $RegexOptions , $NewFormatting , $matchFormatting , $MatchFormattingOptions , $EscapeRegEx.IsPresent , $UseRegExSubstitutions.IsPresent , $RemoveEmptyParagraph.IsPresent )
67
- if ($Supress ) { return } else { return $Paragraph }
76
+ if ($Suppress ) { return } else { return $Paragraph }
68
77
}
69
- # $Paragraph.ReplaceText($SearchValue, $ReplaceValue, $TrackChanges, $RegexOptions, $NewFormatting, $MatchFormatting, $MatchFormattingOptions, $escapeRegEx, $useRegExSubstitutions, $removeEmptyParagraph)
70
78
}
0 commit comments