topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • Thursday March 28, 2024, 4:12 am
  • Proudly celebrating 15+ years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: Powershell Scripting : VBA adaptation code for locate strings inside word files  (Read 6435 times)

Contro

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 3,940
    • View Profile
    • Donate to Member
4wd indicates me this wonderful script to search a string in several word files and adapted for searching in doc files too (not only docx).
This script search word files in a folder.
https://www.datanume...iple-word-documents/
But don't do in its subfolders.
How can this be done ?

Additionally I have a problem of using fields in my word files and when a file is opened a window appear offering to update the links.
the idea is say no to that special windows and make the replace of the string only...

I can offer any information about that warning window if needed .
 :-* :P
« Last Edit: February 03, 2021, 01:28 PM by Contro »

Contro

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 3,940
    • View Profile
    • Donate to Member
Re: VBA adaptation code for locate strings inside word files
« Reply #1 on: January 30, 2021, 04:34 AM »
how can I disable with VBA code when opening this window ? :


AsusPortatil - 30_01_2021 , 10_32_03.pngPowershell Scripting : VBA adaptation code for locate strings inside word files

Notes :
1. Can I disable the window in a certain file ?
2. Can I disable the window when opening any file ?
3. Can I disable the window with VBA code ?

Best Regards

I have located in spanish how to deshabilitate the link update with https://mundo-geek.c...ivos-como-manejarlo/
It would be perfect if I can inhabilitate with VBA code before opening and habilitate when finish...
 :-* :P
« Last Edit: January 30, 2021, 06:15 AM by Contro »

Contro

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 3,940
    • View Profile
    • Donate to Member
Re: VBA adaptation code for locate strings inside word files
« Reply #2 on: January 30, 2021, 06:43 AM »
In English
Update automatic links on open

https://www.stigview...1-07/finding/V-17811
https://word.tips.ne...Automatic_Links.html

So is possible even throug the windows registry.
The next step may be how to do this with VBA code.

 :-* :P

Contro

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 3,940
    • View Profile
    • Donate to Member
Re: VBA adaptation code for locate strings inside word files
« Reply #3 on: January 30, 2021, 11:19 AM »
How can I adapt this for searching in a folder and its subfolders ?
https://stackoverflo...subfolders-using-vba
 :-* :P

Contro

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 3,940
    • View Profile
    • Donate to Member
Re: VBA adaptation code for locate strings inside word files
« Reply #4 on: January 31, 2021, 12:27 PM »
Can I do a reg script to replace a string in the registry and execute this reg file from a VBA script ?

Best Regards

4wd

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,641
    • View Profile
    • Donate to Member
Re: VBA adaptation code for locate strings inside word files
« Reply #5 on: February 01, 2021, 11:10 PM »
Works for me with Word 2k3.

Thing.png

Code: PowerShell [Select]
  1. <# This form was created using POSHGUI.com  a free online gui designer for PowerShell
  2. .NAME
  3.     Thing.ps1
  4.  
  5. .USAGE
  6.     powershell path\to\Thing.ps1
  7. #>
  8.  
  9. Add-Type -AssemblyName System.Windows.Forms
  10. [System.Windows.Forms.Application]::EnableVisualStyles()
  11.  
  12. $Form                            = New-Object system.Windows.Forms.Form
  13. $Form.ClientSize                 = New-Object System.Drawing.Point(400,224)
  14. $Form.text                       = "Thing"
  15. $Form.TopMost                    = $false
  16.  
  17. $Label1                          = New-Object system.Windows.Forms.Label
  18. $Label1.text                     = "Folder"
  19. $Label1.AutoSize                 = $true
  20. $Label1.width                    = 25
  21. $Label1.height                   = 10
  22. $Label1.location                 = New-Object System.Drawing.Point(17,24)
  23. $Label1.Font                     = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
  24.  
  25. $Label2                          = New-Object system.Windows.Forms.Label
  26. $Label2.text                     = "Find"
  27. $Label2.AutoSize                 = $true
  28. $Label2.width                    = 25
  29. $Label2.height                   = 10
  30. $Label2.location                 = New-Object System.Drawing.Point(17,54)
  31. $Label2.Font                     = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
  32.  
  33. $Label3                          = New-Object system.Windows.Forms.Label
  34. $Label3.text                     = "Replace"
  35. $Label3.AutoSize                 = $true
  36. $Label3.width                    = 25
  37. $Label3.height                   = 10
  38. $Label3.location                 = New-Object System.Drawing.Point(17,83)
  39. $Label3.Font                     = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
  40.  
  41. $TextBox1                        = New-Object system.Windows.Forms.TextBox
  42. $TextBox1.multiline              = $false
  43. $TextBox1.width                  = 257
  44. $TextBox1.height                 = 20
  45. $TextBox1.location               = New-Object System.Drawing.Point(82,20)
  46. $TextBox1.Font                   = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
  47.  
  48. $TextBox2                        = New-Object system.Windows.Forms.TextBox
  49. $TextBox2.multiline              = $false
  50. $TextBox2.width                  = 257
  51. $TextBox2.height                 = 20
  52. $TextBox2.location               = New-Object System.Drawing.Point(82,48)
  53. $TextBox2.Font                   = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
  54.  
  55. $TextBox3                        = New-Object system.Windows.Forms.TextBox
  56. $TextBox3.multiline              = $false
  57. $TextBox3.width                  = 257
  58. $TextBox3.height                 = 20
  59. $TextBox3.location               = New-Object System.Drawing.Point(82,76)
  60. $TextBox3.Font                   = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
  61.  
  62. $Button1                         = New-Object system.Windows.Forms.Button
  63. $Button1.text                    = "..."
  64. $Button1.width                   = 34
  65. $Button1.height                  = 18
  66. $Button1.location                = New-Object System.Drawing.Point(353,20)
  67. $Button1.Font                    = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
  68.  
  69. $Button2                         = New-Object system.Windows.Forms.Button
  70. $Button2.text                    = "Start"
  71. $Button2.width                   = 60
  72. $Button2.height                  = 30
  73. $Button2.location                = New-Object System.Drawing.Point(184,122)
  74. $Button2.Font                    = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
  75.  
  76. $TextBox4                        = New-Object system.Windows.Forms.TextBox
  77. $TextBox4.multiline              = $false
  78. $TextBox4.width                  = 257
  79. $TextBox4.height                 = 20
  80. $TextBox4.location               = New-Object System.Drawing.Point(82,174)
  81. $TextBox4.Font                   = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
  82.  
  83. $Form.controls.AddRange(@($Label1,$Label2,$Label3,$TextBox1,$TextBox2,$TextBox3,$Button1,$Button2,$TextBox4))
  84.  
  85. $Button1.Add_Click({
  86.   $objForm = New-Object System.Windows.Forms.FolderBrowserDialog
  87.   $objForm.Description = "Select folder containing files"
  88.   $objForm.SelectedPath = [System.Environment+SpecialFolder]'MyComputer'
  89.   $objForm.ShowNewFolderButton = $false
  90.   $result = $objForm.ShowDialog()
  91.   if ($result -eq "OK") {
  92.     $TextBox1.Text = $objForm.SelectedPath
  93.   } else {
  94.     $TextBox1.Text = ""
  95.   }
  96. })
  97.  
  98. $Button2.Add_Click({
  99.   if ($TextBox1.Text -ne '' -and $TextBox2.Text -ne '') {
  100.     Start-ReplaceText $TextBox1.Text $TextBox2.Text $TextBox3.Text
  101.   }
  102. })
  103.  
  104.  
  105. function Start-ReplaceText {
  106.   param (
  107.     [Parameter(Mandatory = $true)][string]$srce,
  108.     [Parameter(Mandatory = $true)][string]$FindText,
  109.     [Parameter(Mandatory = $true)][string]$ReplaceWith
  110.   )
  111.  
  112.   $wdAlertsAll = -1         # All message boxes and alerts are displayed; errors are returned to the macro.
  113.   $wdAlertsMessageBox = -2  # Only message boxes are displayed; errors are trapped and returned to the macro.
  114.   $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.
  115.  
  116.   # Code below came from StackOverflow but I've lost the link
  117.   $objWord = New-Object -comobject Word.Application
  118.   $objWord.Visible = $false
  119.   $objWord.DisplayAlerts = $wdAlertsNone  # See values above
  120.  
  121.   $list = Get-ChildItem "$($srce)\*" -Include *.doc,*.docx -exclude ~*,*.mem.doc -Recurse # Add more to exclude if necessary
  122.   foreach($item in $list){
  123.     $TextBox4.Text = "Doing file: $([System.io.Path]::GetFileName($item))"
  124.     $objDoc = $objWord.Documents.Open($item.FullName, $true)
  125.     $objSelection = $objWord.Selection
  126.     $wdFindContinue = 1
  127.     $MatchCase = $False
  128.     $MatchWholeWord = $true
  129.     $MatchWildCards = $False
  130.     $MatchSoundsLike = $False
  131.     $MatchAllWordForms = $False
  132.     $Forward = $True
  133.     $Wrap = $wdFindContinue
  134.     $Format = $True
  135. #    $wdReplaceNone = 0
  136.     $wdFindContinue = 1
  137.     $ReplaceAll = 2
  138.  
  139.     $a = $objSelection.Find.Execute($FindText, $MatchCase, $MatchWholeWord, $MatchWildCards, `
  140.       $MatchSoundsLike, $MatchAllWordForms, $Forward, $Wrap, $Format, $ReplaceWith, `
  141.       $ReplaceAll)
  142.  
  143.     $objDoc.Save()
  144.     $objDoc.Close()
  145.   }
  146.  
  147.   $objWord.DisplayAlerts = $wdAlertsAll  # Turn alerts back on, see values above
  148.   $objWord.Quit()
  149.   $TextBox4.Text = 'Finished'
  150. }
  151.  
  152.  
  153. #Write your logic code here
  154.  
  155. [void]$Form.ShowDialog()
« Last Edit: February 04, 2021, 06:25 AM by 4wd »

Contro

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 3,940
    • View Profile
    • Donate to Member
Re: VBA adaptation code for locate strings inside word files
« Reply #6 on: February 02, 2021, 05:42 PM »
I will try and comment
Best Regards
 :-* :P

Contro

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 3,940
    • View Profile
    • Donate to Member
Re: VBA adaptation code for locate strings inside word files
« Reply #7 on: February 02, 2021, 06:04 PM »
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

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,641
    • View Profile
    • Donate to Member
Re: VBA adaptation code for locate strings inside word files
« Reply #8 on: February 02, 2021, 08:47 PM »
You anticipate that you prefer Powershell.

I don't anticipate that I prefer PowerShell ...

I only use PowerShell. ;)

Contro

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 3,940
    • View Profile
    • Donate to Member
Re: VBA adaptation code for locate strings inside word files
« Reply #9 on: February 03, 2021, 05:26 AM »
You anticipate that you prefer Powershell.

I don't anticipate that I prefer PowerShell ...

I only use PowerShell. ;)
;D :)  :-* :P

Contro

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 3,940
    • View Profile
    • Donate to Member
Re: VBA adaptation code for locate strings inside word files
« Reply #10 on: February 03, 2021, 10:54 AM »
I am trying the script and seems go well, but with some files I have problem
I can put screenshots if needed

.eml files are processed if contain in the description .doc ?
doing file : ~$5.proyecto.doc and similar are not needed..
pdf files are not needed

where can I control this in the script ?

I only need to process doc, docx and rtf files initially.
And some of them may be excluded like :
EMERGENCIA.doc
beginning with ~$ ,,,,,, doc
and some others
 :-\

Contro

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 3,940
    • View Profile
    • Donate to Member
Re: VBA adaptation code for locate strings inside word files
« Reply #11 on: February 03, 2021, 11:04 AM »
Initially i am trying in a security copy in the desktop.

Some files give me errors. Some of them are from external programs for calculations and I don't need to process.
Like EMERGENCIA.doc
I put some screenshots with the errors I am obtaining...
AsusPortatil - 03_02_2021 , 16_34_16.pngPowershell Scripting : VBA adaptation code for locate strings inside word files AsusPortatil - 03_02_2021 , 16_43_17.pngPowershell Scripting : VBA adaptation code for locate strings inside word files AsusPortatil - 03_02_2021 , 16_45_26.pngPowershell Scripting : VBA adaptation code for locate strings inside word files AsusPortatil - 03_02_2021 , 16_58_53.pngPowershell Scripting : VBA adaptation code for locate strings inside word files AsusPortatil - 03_02_2021 , 16_58_53.pngPowershell Scripting : VBA adaptation code for locate strings inside word files AsusPortatil - 03_02_2021 , 17_01_03.pngPowershell Scripting : VBA adaptation code for locate strings inside word files

Contro

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 3,940
    • View Profile
    • Donate to Member
Re: VBA adaptation code for locate strings inside word files
« Reply #12 on: February 03, 2021, 11:25 AM »
I''ll make an experiment to see if process eml files or pdf files creating a folder with this type of files.
When need to depure something I observ microsoft word interaction goes to open the program directly and then I have the window of the program open.

 :-* :P

Contro

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 3,940
    • View Profile
    • Donate to Member
Re: VBA adaptation code for locate strings inside word files
« Reply #13 on: February 03, 2021, 11:43 AM »
I need to exclude these too :
*.MEM.doc
*.PST.doc
*.pry.doc
*.anx.doc
*.CTF.doc
*.rey.doc

I am trying to determine if the supposed eml and pdf intercepted are because or not containing the string .doc in the description ....


Contro

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 3,940
    • View Profile
    • Donate to Member
So :
1. I need a filter to filter what types and names I want to process

2. I would like too an option to manipulate the windows registry , specially if a window appear when opening the word file.

 :-* :P

4wd

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,641
    • View Profile
    • Donate to Member
I am trying to determine if the supposed eml and pdf intercepted are because or not containing the string .doc in the description ....

That's not a description, it's a file name.  Use the Comment metadata for a description.

1. I need a filter to filter what types and names I want to process

See above:

Code: PowerShell [Select]
  1. $list = Get-ChildItem "$($srce)\*" -Include *.doc,*.docx -exclude ~*,*.mem.doc -Recurse # Add more to exclude if necessary

Edit as necessary.


2. I would like too an option to manipulate the windows registry , specially if a window appear when opening the word file.

Code: PowerShell [Select]
  1. ...
  2.   $wdAlertsAll = -1         # All message boxes and alerts are displayed; errors are returned to the macro.
  3.   $wdAlertsMessageBox = -2  # Only message boxes are displayed; errors are trapped and returned to the macro.
  4.   $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.
  5. ...
  6.   $objWord.DisplayAlerts = $wdAlertsNone  # See values above
  7. ...

Might work, might not.

Contro

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 3,940
    • View Profile
    • Donate to Member
I put this :
     
      $list = Get-ChildItem "$($srce)\*" -Include *.doc* -exclude ~*,*.mem.doc,~*,*.rey.doc,~*,*.CTF.doc,~*,*.anx.doc,~*,*.pry.doc,~*,*.PST.doc, -Recurse # Add more to exclude if necessary

I'll put in a few days the window I wish to deactivate and how I do throught the options in microsoft word.
 :-* :P

4wd

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,641
    • View Profile
    • Donate to Member
      $list = Get-ChildItem "$($srce)\*" -Include *.doc* -exclude ~*,*.mem.doc,~*,*.rey.doc,~*,*.CTF.doc,~*,*.anx.doc,~*,*.pry.doc,~*,*.PST.doc, -Recurse # Add more to exclude if necessary

I'll put in a few days the window I wish to deactivate and how I do throught the options in microsoft word.

Changes were already in original post, you only had to add more to exclude.

reynolds_john

  • Charter Member
  • Joined in 2005
  • ***
  • default avatar
  • Posts: 8
    • View Profile
    • Donate to Member
May I suggest Everything ?

I tested it on Word docx and doc and other files for content and it works brilliantly. You can configure it to just monitor particular folders if you wish, run as a service, etc. etc.
Command line supported as well.
I've used this in environments with massive (20 million)+ files and had great success too.

Best,
--J

Contro

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 3,940
    • View Profile
    • Donate to Member
May I suggest Everything ?

I tested it on Word docx and doc and other files for content and it works brilliantly. You can configure it to just monitor particular folders if you wish, run as a service, etc. etc.
Command line supported as well.
I've used this in environments with massive (20 million)+ files and had great success too.

Best,
--J
Wait for 4wd. He is my guru.
Have you tried to replace strings inside doc and docx files ?
Notepad++ and others also have the ability to search, but not to replace. Or if you replace you get a mess and a headache !!!!!

reynolds_john

  • Charter Member
  • Joined in 2005
  • ***
  • default avatar
  • Posts: 8
    • View Profile
    • Donate to Member
Sorry, I missed the part about replacing words too.

Contro

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 3,940
    • View Profile
    • Donate to Member
Sorry, I missed the part about replacing words too.
I use Everything several years ago !!!!!!!!!
 :-* :P