DonationCoder.com Software > Post New Requests Here
Powershell Scripting : VBA adaptation code for locate strings inside word files
4wd:
Works for me with Word 2k3.
--- Code: PowerShell ---<# This form was created using POSHGUI.com a free online gui designer for PowerShell.NAME Thing.ps1 .USAGE powershell path\to\Thing.ps1#> Add-Type -AssemblyName System.Windows.Forms[System.Windows.Forms.Application]::EnableVisualStyles() $Form = New-Object system.Windows.Forms.Form$Form.ClientSize = New-Object System.Drawing.Point(400,224)$Form.text = "Thing"$Form.TopMost = $false $Label1 = New-Object system.Windows.Forms.Label$Label1.text = "Folder"$Label1.AutoSize = $true$Label1.width = 25$Label1.height = 10$Label1.location = New-Object System.Drawing.Point(17,24)$Label1.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10) $Label2 = New-Object system.Windows.Forms.Label$Label2.text = "Find"$Label2.AutoSize = $true$Label2.width = 25$Label2.height = 10$Label2.location = New-Object System.Drawing.Point(17,54)$Label2.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10) $Label3 = New-Object system.Windows.Forms.Label$Label3.text = "Replace"$Label3.AutoSize = $true$Label3.width = 25$Label3.height = 10$Label3.location = New-Object System.Drawing.Point(17,83)$Label3.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10) $TextBox1 = New-Object system.Windows.Forms.TextBox$TextBox1.multiline = $false$TextBox1.width = 257$TextBox1.height = 20$TextBox1.location = New-Object System.Drawing.Point(82,20)$TextBox1.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10) $TextBox2 = New-Object system.Windows.Forms.TextBox$TextBox2.multiline = $false$TextBox2.width = 257$TextBox2.height = 20$TextBox2.location = New-Object System.Drawing.Point(82,48)$TextBox2.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10) $TextBox3 = New-Object system.Windows.Forms.TextBox$TextBox3.multiline = $false$TextBox3.width = 257$TextBox3.height = 20$TextBox3.location = New-Object System.Drawing.Point(82,76)$TextBox3.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10) $Button1 = New-Object system.Windows.Forms.Button$Button1.text = "..."$Button1.width = 34$Button1.height = 18$Button1.location = New-Object System.Drawing.Point(353,20)$Button1.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10) $Button2 = New-Object system.Windows.Forms.Button$Button2.text = "Start"$Button2.width = 60$Button2.height = 30$Button2.location = New-Object System.Drawing.Point(184,122)$Button2.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10) $TextBox4 = New-Object system.Windows.Forms.TextBox$TextBox4.multiline = $false$TextBox4.width = 257$TextBox4.height = 20$TextBox4.location = New-Object System.Drawing.Point(82,174)$TextBox4.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10) $Form.controls.AddRange(@($Label1,$Label2,$Label3,$TextBox1,$TextBox2,$TextBox3,$Button1,$Button2,$TextBox4)) $Button1.Add_Click({ $objForm = New-Object System.Windows.Forms.FolderBrowserDialog $objForm.Description = "Select folder containing files" $objForm.SelectedPath = [System.Environment+SpecialFolder]'MyComputer' $objForm.ShowNewFolderButton = $false $result = $objForm.ShowDialog() if ($result -eq "OK") { $TextBox1.Text = $objForm.SelectedPath } else { $TextBox1.Text = "" }}) $Button2.Add_Click({ if ($TextBox1.Text -ne '' -and $TextBox2.Text -ne '') { Start-ReplaceText $TextBox1.Text $TextBox2.Text $TextBox3.Text }}) function Start-ReplaceText { param ( [Parameter(Mandatory = $true)][string]$srce, [Parameter(Mandatory = $true)][string]$FindText, [Parameter(Mandatory = $true)][string]$ReplaceWith ) $wdAlertsAll = -1 # All message boxes and alerts are displayed; errors are returned to the macro. $wdAlertsMessageBox = -2 # Only message boxes are displayed; errors are trapped and returned to the macro. $wdAlertsNone = 0 # No alerts or message boxes are displayed. If a macro encounters a message box, the default value is chosen and the macro continues. # Code below came from StackOverflow but I've lost the link $objWord = New-Object -comobject Word.Application $objWord.Visible = $false $objWord.DisplayAlerts = $wdAlertsNone # See values above $list = Get-ChildItem "$($srce)\*" -Include *.doc,*.docx -exclude ~*,*.mem.doc -Recurse # Add more to exclude if necessary foreach($item in $list){ $TextBox4.Text = "Doing file: $([System.io.Path]::GetFileName($item))" $objDoc = $objWord.Documents.Open($item.FullName, $true) $objSelection = $objWord.Selection $wdFindContinue = 1 $MatchCase = $False $MatchWholeWord = $true $MatchWildCards = $False $MatchSoundsLike = $False $MatchAllWordForms = $False $Forward = $True $Wrap = $wdFindContinue $Format = $True# $wdReplaceNone = 0 $wdFindContinue = 1 $ReplaceAll = 2 $a = $objSelection.Find.Execute($FindText, $MatchCase, $MatchWholeWord, $MatchWildCards, ` $MatchSoundsLike, $MatchAllWordForms, $Forward, $Wrap, $Format, $ReplaceWith, ` $ReplaceAll) $objDoc.Save() $objDoc.Close() } $objWord.DisplayAlerts = $wdAlertsAll # Turn alerts back on, see values above $objWord.Quit() $TextBox4.Text = 'Finished'} #Write your logic code here [void]$Form.ShowDialog()
Contro:
I will try and comment
Best Regards
:-* :P
Contro:
I will try step by step. Seems other type of code. Not VBA. So I have to learn.
You anticipate that you prefer Powershell.
I will study and learn and comment.
Best Regards
4wd:
You anticipate that you prefer Powershell. -Contro (February 02, 2021, 06:04 PM)
--- End quote ---
I don't anticipate that I prefer PowerShell ...
I only use PowerShell. ;)
Contro:
You anticipate that you prefer Powershell. -Contro (February 02, 2021, 06:04 PM)
--- End quote ---
I don't anticipate that I prefer PowerShell ...
I only use PowerShell. ;)
-4wd (February 02, 2021, 08:47 PM)
--- End quote ---
;D :) :-* :P
Navigation
[0] Message Index
[#] Next page
[*] Previous page
Go to full version