i want to replace a string with an hyperlink
i try with something like this
Update:
$FindText = "[E-mail]"
$email ="asdadasd#asdada.com"
$a=$objSelection.Find.Execute($FindText)
$newaddress = $objSelection.Hyperlinks.Add($objSelection.Range,$email) )
but this insert the email at beginnig of file word don't replace the string "[E-mail]"
Add-Type -AssemblyName "Microsoft.Office.Interop.Word"
$wdunits = "Microsoft.Office.Interop.Word.wdunits" -as [type]
$objWord = New-Object -ComObject Word.Application
$objWord.Visible = $false
$findText = "[E-mail]"
$emailAddress = "someemail#example.com"
$mailTo = "mailto:"+$emailAddress
$objDoc = $objWord.Documents.Open("Path\to\input.docx")
$saveAs = "Path\to\output.docx")
$range = $objDoc.Content
$null = $range.movestart($wdunits::wdword,$range.start)
$objSelection = $objWord.Selection
$matchCase = $false
$matchWholeWord = $true
$matchWildcards = $false
$matchSoundsLike = $false
$matchAllWordForms = $false
$forward = $true
$wrap = 1
$format = $False
$wdReplaceNone = 0
$wdFindContinue = 1
$wdReplaceAll = 2
$wordFound = $range.find.execute($findText,$matchCase,$matchWholeWord,$matchWildCards,$matchSoundsLike,$matchAllWordForms,$forward,$wrap)
if($wordFound)
{
if ($range.style.namelocal -eq "normal")
{
$null = $objDoc.Hyperlinks.Add($range,$mailTo,$null,$null,$emailAddress)
}
}
$objDoc.SaveAs($saveAs)
$objDoc.Close()
$objWord.Quit()
Remove-Variable -Name objWord
[gc]::Collect()
[gc]::WaitForPendingFinalizers()
Kinda ugly, but this script will do what you need. It loads the .docx specified with $objDoc, finds all instances of $findText, and replaces it with a mailto link for $emailAddress and then saves the changes to $saveAs.
Most of this based on a "Hey, Scripting Guy" Article
Related
I'm working on a little side project of listing outlook calendar meetings in the form data grid view.
I can populate columns with Subject and Start Date with no problem. For some reason, I can not populate a column with .DayOfWeekMask value, even though I can access this property via CLI. Any thoughts? Code below
add-type -AssemblyName 'System.Windows.Forms'
Add-Type -AssemblyName 'System.Drawing'
Add-Type -AssemblyName 'PresentationFramework'
$4 =
"BASE64 code"
$today = get-date
$Script:selection = #()
add-type -AssemblyName "Microsoft.Office.Interop.Outlook"
$outlookFolders = "Microsoft.Office.Interop.Outlook.OlDefaultFolders" -as [type]
$outlook = New-Object -ComObject outlook.application
$namespace = $outlook.GetNamespace("MAPI")
$folder = $namespace.GetDefaultFolder($outlookFolders::olFolderCalendar)
$items = $folder.Items
$meetings = $items|?{$_.isRecurring -eq $true -and $_.Subject -notlike "*Canceled*" <#-and $_.GetRecurrencePattern().DayOfWeekMask -eq 18#>}|Select Subject, Start, #{l='Mask';e={$_.GetRecurrencePattern().DayOfWeekMask}}
$bitmap3 = New-Object System.Windows.Media.Imaging.BitmapImage
$bitmap3.BeginInit()
$bitmap3.StreamSource = [System.IO.MemoryStream][System.Convert]::FromBase64String($4)
$bitmap3.EndInit()
$bitmap3.Freeze()
$image3 = [System.Drawing.Bitmap][System.Drawing.Image]::FromStream($bitmap3.StreamSource)
$icon3 = [System.Drawing.Icon]::FromHandle($image3.GetHicon())
$main2 = New-Object System.Windows.Forms.Form
$main2.Icon = $icon3
$main2.ClientSize = '600,600'
$main2.MinimizeBox = $false
$main2.MaximizeBox = $false
$main2.Text = 'Options'
$main2.AutoSize = $true
$grid = New-Object System.Windows.Forms.DataGridView
$grid.Location = '5,5'
$grid.Height = 500
$grid.Width = 390
$grid.ColumnHeadersVisible = $true
$grid.AutoSizeColumnsMode = 'AllCells'
$grid.ColumnCount = 3
$grid.columns[0].Name = 'Name'
$grid.Columns[1].Name = 'Time'
$grid.Columns[2].Name = 'Day Mask'
$grid.MultiSelect = $true
$grid.SelectionMode = 'FullRowSelect'
$meetings|%{$grid.Rows.Add($($_.Subject), $($_.Start)), $($_.Mask)}
$button = [System.Windows.Forms.Button]::new()
$button.Location = [System.Drawing.Point]::new(5,540)
$button.Size = [System.Drawing.Size]::new(100,50)
$button.Text = 'OK'
$button.Add_Click({
$grid.SelectedRows|%{
$Script:selection += [pscustomobject]#{
Name = $grid.Rows[$_.Index].Cells[0].Value
Time = $grid.Rows[$_.Index].Cells[1].Value
}
}
})
$main2.Controls.AddRange(#($grid,$button))
$main2.ShowDialog()
update you datagrid wiht this one
$grid = New-Object System.Windows.Forms.DataGridView
$grid.Location = '5,5'
$grid.Height = 500
$grid.Width = 390
$grid.ColumnHeadersVisible = $true
$grid.AutoSizeColumnsMode = 'AllCells'
$grid.MultiSelect = $true
$grid.SelectionMode = 'FullRowSelect'
$array.AddRange($meetings)
$grid.DataSource=($array)
======= this datasourge work eveytime..
Based on the answer of #Kemal I removed .ColumnCount and subsequent Name properties. Created new ArrayList via $array = [System.Collections.ArrayList]::new() abd then applied Kemal's solution.
In th end the script looks like this.
add-type -AssemblyName 'System.Windows.Forms'
Add-Type -AssemblyName 'System.Drawing'
Add-Type -AssemblyName 'PresentationFramework'
$4 =
"BASE64 CODE"
$today = get-date
$selection =#()
$array = [System.Collections.ArrayList]::new()
add-type -AssemblyName "Microsoft.Office.Interop.Outlook"
$outlookFolders = "Microsoft.Office.Interop.Outlook.OlDefaultFolders" -as [type]
$outlook = New-Object -ComObject outlook.application
$namespace = $outlook.GetNamespace("MAPI")
$folder = $namespace.GetDefaultFolder($outlookFolders::olFolderCalendar)
$items = $folder.Items
$meetings = $items|?{$_.isRecurring -eq $true -and $_.Subject -notlike "*Canceled*" |Select Subject, Start, #{l='Mask';e={$_.GetRecurrencePattern().DayOfWeekMask}}
$bitmap3 = New-Object System.Windows.Media.Imaging.BitmapImage
$bitmap3.BeginInit()
$bitmap3.StreamSource = [System.IO.MemoryStream][System.Convert]::FromBase64String($4)
$bitmap3.EndInit()
$bitmap3.Freeze()
$image3 = [System.Drawing.Bitmap][System.Drawing.Image]::FromStream($bitmap3.StreamSource)
$icon3 = [System.Drawing.Icon]::FromHandle($image3.GetHicon())
$main2 = New-Object System.Windows.Forms.Form
$main2.Icon = $icon3
$main2.ClientSize = '600,600'
$main2.MinimizeBox = $false
$main2.MaximizeBox = $false
$main2.Text = 'Options'
$main2.AutoSize = $true
$grid = New-Object System.Windows.Forms.DataGridView
$grid.Location = '5,5'
$grid.Height = 500
$grid.Width = 390
$grid.ColumnHeadersVisible = $true
$grid.AutoSizeColumnsMode = 'AllCells'
$grid.MultiSelect = $true
$grid.SelectionMode = 'FullRowSelect'
$array.AddRange($meetings)
$grid.DataSource=($array)
$button = [System.Windows.Forms.Button]::new()
$button.Location = [System.Drawing.Point]::new(5,540)
$button.Size = [System.Drawing.Size]::new(100,50)
$button.Text = 'OK'
$button.Add_Click({
$grid.SelectedRows|%{
$Script:selection += [pscustomobject]#{
Name = $grid.Rows[$_.Index].Cells[0].Value
Time = $grid.Rows[$_.Index].Cells[1].Value
DayofWeek = $grid.Rows[$_.Index].Cells[2].Value
}
}
})
$main2.Controls.AddRange(#($grid,$button))
$main2.ShowDialog()
I'm trying to replace multiple strings in a word document using PowerShell, but only one string is replaced when running the code below:
#Includes
Add-Type -AssemblyName System.Windows.Forms
#Functions
#Function to find and replace in a word document
function FindAndReplace($objSelection, $findText,$replaceWith){
$matchCase = $true
$matchWholeWord = $true
$matchWildcards = $false
$matchSoundsLike = $false
$matchAllWordForms = $false
$forward = $true
$wrap = [Microsoft.Office.Interop.Word.WdFindWrap]::wdReplaceAll
$format = $false
$replace = [Microsoft.Office.Interop.Word.WdFindWrap]::wdFindContinue
$objSelection.Find.Execute($findText,$matchCase,$matchWholeWord,$matchWildcards,$matchSoundsLike,$matchAllWordForms,$forward,$wrap,$format,$replaceWith, $replace) > $null
}
$item1 = "Should"
$item2 = "this"
$item3 = "work"
$item4 = "?"
$fileName = "NewFile"
#Opens a file browsers to select a word document
$FileBrowser = New-Object System.Windows.Forms.OpenFileDialog -Property #{
InitialDirectory = [Environment]::GetFolderPath('Desktop')
Filter = 'Documents (*.docx)|*.docx'
}
Write-Host "Select word template file"
$FileBrowser.ShowDialog()
$templateFile = $FileBrowser.FileName
$word = New-Object -comobject Word.Application
$word.Visible = $false
$template = $word.Documents.Open($templateFile)
$selection = $template.ActiveWindow.Selection
FindAndReplace $selection '#ITEM1#' $item1
FindAndReplace $selection '#ITEM2#' $item2
FindAndReplace $selection '#ITEM3#' $item3
FindAndReplace $selection '#ITEM4#' $item4
$fileName = $fileName
$template.SaveAs($fileName)
$word.Quit()
If I comment out FindAndReplace the first one that runs works, but subsequent calls do not.
For example running this as is results in:
Input Output
#ITEM1# Should
#ITEM2# #ITEM2#
#ITEM3# #ITEM3#
#ITEM4# #ITEM4#
I'm not sure what I'm missing, any help would be appreciated
As was suggested it appears that the cursor was not returning to the beginning of the document. I added the following code:
Set-Variable -Name wdGoToLine -Value 3 -Option Constant
Set-Variable -Name wdGoToAbsolute -Value 1 -Option Constant
To the beginning of my script and:
$objSelection.GoTo($wdGoToLine, $wdGoToAbsolute, 1) > $null
as the first line in my FindAndReplace function, and now it works as expected.
There may be a more elegant solution, but this works for me
Using PowerShell, I need to write a script which would remove all hidden text of a Word Document.
Here is what I have so far :
$WordDocument = Get-Item "C:\MyWordDocument.docx"
$word_app = New-Object -ComObject Word.Application
$word_app.Visible = $false
$document = $word_app.Documents.Open($WordDocument.FullName)
$objSelection = $word_app.Selection
$objSelection.Font.Hidden = $True
$FindText = "" # search on formatting only (according to MS doc)
$wdFindContinue = 1
$ReplaceAll = 2
$MatchCase = $False
$MatchWholeWord = $False
$MatchWildcards = $False
$MatchSoundsLike = $False
$MatchAllWordForms = $False
$Forward = $True
$Wrap = $wdFindContinue
$Format = $True # ?
$ReplaceWith = ""
$a = $objSelection.Find.Execute($FindText,$MatchCase,$MatchWholeWord, `
$MatchWildcards,$MatchSoundsLike,$MatchAllWordForms,$Forward,`
$Wrap,$Format,$ReplaceWith,$ReplaceAll)
$document.Save()
$document.Close()
$word_app.Quit()
It does not work, and I cannot figure out why.
Any idea ?
The mistake is where you set the search filter to find hidden text. Instead of $objSelection.Font.Hidden = $True (this actually hides the currently selected text) you need to set the property on the $objSelection.Find object:
$objSelection.Find.Font.Hidden = $True
I've managed to find& edit per one word file. With this code:
$objWord = New-Object -comobject Word.Application
$objWord.Visible = $false
$objDoc = $objWord.Documents.Open("C:\users\stefan\test\New Microsoft Word Document.docx")
$objSelection = $objWord.Selection
$FindText = "that"
$MatchCase = $False
$MatchWholeWord = $true
$MatchWildcards = $False
$MatchSoundsLike = $False
$MatchAllWordForms = $False
$Forward = $True
$Wrap = $wdFindContinue
$Format = $False
$wdReplaceNone = 0
$ReplaceWith = "this"
$wdFindContinue = 1
$a = $objSelection.Find.Execute($FindText,$MatchCase,$MatchWholeWord, `
$MatchWildcards,$MatchSoundsLike,$MatchAllWordForms,$Forward,`
$Wrap,$Format,$ReplaceWith)
$objDoc.Save()
$objWord.Quit()
But I want to do it for whole folder. I've tried to insert something like this:
$objWord = New-Object -comobject Word.Application
$objWord.Visible = $false
$list = Get-ChildItem "c:\users\stefan\test\*.*" -Include *.doc*
foreach($item in $list){
$objDoc = $objWord.Documents.Open($list.FullName,$true)
$objSelection = $objWord.Selection
$FindText = "Sara"
$MatchCase = $False
$MatchWholeWord = $true
$MatchWildcards = $False
$MatchSoundsLike = $False
$MatchAllWordForms = $False
$Forward = $True
$Wrap = $wdFindContinue
$Format = $False
$wdReplaceNone = 0
$ReplaceWith = "AJMOO"
$wdFindContinue = 1
$a = $objSelection.Find.Execute($FindText,$MatchCase,$MatchWholeWord, `
$MatchWildcards,$MatchSoundsLike,$MatchAllWordForms,$Forward,`
$Wrap,$Format,$ReplaceWith)
$objDoc.Save()
$objWord.Quit()
}
Also, it changes only one item that is found, but I want all items in the file.
Thanks.
Multiple Replacements
Also, it changes only one item that is found, but I want all items in the file
This is because you have not set the scope of the replacement to be all items. The is from the next argument you did not specify in your Execute method call. Set a variable called $wdReplaceAll and set it to 2. Then you adjust your call to add that variable.
$a = $objSelection.Find.Execute($FindText,$MatchCase,$MatchWholeWord, `
$MatchWildcards,$MatchSoundsLike,$MatchAllWordForms,$Forward,`
$Wrap,$Format,$ReplaceWith,$wdReplaceAll)
That fixes that issue when run against one file.
Multiple Files
But I want to do it for whole folder
The partial issue with that is your are not properly querying the folder for files. -Include is finicky and work when partnered with -Recurse however you are treating it like a -Filter anyway so adjust as such.
$list = Get-ChildItem "c:\users\stefan\test\" -filter "*.doc*"
Next, when you are looping you are not using the current iteration but the whole collection when calling .open()
$objDoc = $objWord.Documents.Open($list.FullName,$true)
Should instead be
$objDoc = $objWord.Documents.Open($item.FullName,$true)
as per your loop definition.
Now you need to be sure you close each document before you quit the application. Right now you are quitting word inside the loop.
foreach($item in $list){
#.... Stuff and things happens here.
$objDoc.Save()
$objDoc.Close()
}
$objWord.Quit()
Variable declaration and calls
Right now you set $wrap to the value of the variable $wdFindContinue. When that is first called $wdFindContinue is null as it is not set to a few lines later in the code.
$Wrap = $wdFindContinue
#...
$wdFindContinue = 1
Switch the order of these lines or just set $wrap directly to 1. I am unsure of the implications of this being incorrect.
Thankfully to #Matt I've solved my code.
Here is a correct version which works:
$objWord = New-Object -comobject Word.Application
$objWord.Visible = $false
$list = Get-ChildItem "c:\users\stefan\test\*.*" -Include *.doc*
foreach($item in $list){
$objDoc = $objWord.Documents.Open($item.FullName,$true)
$objSelection = $objWord.Selection
$wdFindContinue = 1
$FindText = "Sara"
$MatchCase = $False
$MatchWholeWord = $true
$MatchWildcards = $False
$MatchSoundsLike = $False
$MatchAllWordForms = $False
$Forward = $True
$Wrap = $wdFindContinue
$Format = $False
$wdReplaceNone = 0
$ReplaceWith = "AJMOO"
$wdFindContinue = 1
$ReplaceAll = 2
$a = $objSelection.Find.Execute($FindText,$MatchCase,$MatchWholeWord, `
$MatchWildcards,$MatchSoundsLike,$MatchAllWordForms,$Forward,`
$Wrap,$Format,$ReplaceWith,$ReplaceAll)
$objDoc.Save()
$objDoc.Close()
}
$objWord.Quit()
I am attempting to take a large document, search for a "^m" (page break) and create a new text file for each page break I find.
Using:
$SearchText = "^m"
$word = new-object -ComObject "word.application"
$path = "C:\Users\me\Documents\Test.doc"
$doc = $word.documents.open("$path")
$doc.content.find.execute("$SearchText")
I am able to find text, but how do I save the text before the page break into a new file? In VBScript, I would just do a readline and save it to a buffer, but powershell is much different.
EDIT:
$text = $word.Selection.MoveUntil (cset:="^m")
returns an error:
Missing ')' in method call.
I think my solution is kinda stupid, but here is my own solution (please help me find a better one):
Param(
[string]$file
)
#$file = "C:\scripts\docSplit\test.docx"
$word = New-Object -ComObject "word.application"
$doc=$word.documents.open($file)
$txtPageBreak = "<!--PAGE BREAK--!>"
$fileInfo = Get-ChildItem $file
$folder = $fileInfo.directoryName
$fileName = $fileInfo.name
$newFileName = $fileName.replace(".", "")
#$findtext = "^m"
#$replaceText = $txtPageBreak
function Replace-Word ([string]$Document,[string]$FindText,[string]$ReplaceText) {
#Variables used to Match And Replace
$ReplaceAll = 2
$FindContinue = 1
$MatchCase = $False
$MatchWholeWord = $True
$MatchWildcards = $False
$MatchSoundsLike = $False
$MatchAllWordForms = $False
$Forward = $True
$Wrap = $FindContinue
$Format = $False
$Selection = $Word.Selection
$Selection.Find.Execute(
$FindText,
$MatchCase,
$MatchWholeWord,
$MatchWildcards,
$MatchSoundsLike,
$MatchAllWordForms,
$Forward,
$Wrap,
$Format,
$ReplaceText,
$ReplaceAll
)
$newFileName = "$folder\$newFileName.txt"
$Doc.saveAs([ref]"$newFileName",[ref]2)
$doc.close()
}
Replace-Word($file, "^m", $txtPageBreak)
$word.quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($word)
Remove-Variable word
#begin txt file manipulation
#add end of file marker
$eof = "`n<!--END OF FILE!-->"
Add-Content $newfileName $eof
$masterTextFile = Get-Content $newFileName
$buffer = ""
foreach($line in $masterTextFile){
if($line.compareto($eof) -eq 0){
#end of file, save buffer to new file, be done
}
else {
$found = $line.CompareTo($txtPageBreak)
if ($found -eq 1) {
$buffer = "$buffer $line `n"
}
else {
#save the buffer to a new file (still have to write this part)
}
}
}