Jump to content

[Solved] Function _Word_DocFindReplace() can be changed?


Recommended Posts

Hi Experts,

I just want to ask and assistance if _Word_DocFindReplace() can be changed of what you want to replaced and not all characters in "$oFind" will be replaced? Like, I want to find "^$^#^p" in the document and replace only "^p" with delimiter "; ".

#include <Word.au3>
Local $oWord = _Word_Create()
Local $sDocument = @ScriptDir & "\word.docx"
Local $oDoc = _Word_DocOpen($oWord, $sDocument, Default, Default, True)
$oFind = "^$^#^p" ; I want only "^p" to be replaced
$oReplace = "; "
_Word_DocFindReplace($oDoc, $oFind, $oReplace, $wdReplaceOne, 0, True, True)

Thanks in advance Experts,

KS15

Edited by KickStarter15

Programming is "To make it so simple that there are obviously no deficiencies" or "To make it so complicated that there are no obvious deficiencies" by C.A.R. Hoare.

Link to comment
Share on other sites

Why not use

$oFind    = "^$^#^p" ; I want only "^p" to be replaced
$oReplace = "^$^#; "

 

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

@water Thanks, I tried that one but the code will not execute, "^$" and "^#" is not recognized as characters in "$oReplace" but if you change "^$" to "a" and "^#" to "1" then the code will execute replacing "$oFind".

Programming is "To make it so simple that there are obviously no deficiencies" or "To make it so complicated that there are no obvious deficiencies" by C.A.R. Hoare.

Link to comment
Share on other sites

I think the following document is a good reading how to solve your problem:
http://wordmvp.com/FAQs/General/UsingWildcards.htm

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

@water, Thank you for this reading guide on how to handle find and replace in document using wildcards. This is a big help for me in my future projects.^_^ However, after reading the guide, it seems that it is all about replacing the characters to find using wildcards and not replacing the characters being found or was found in _Word_DocFindReplace().

In "$oFind", lets say we use the wildcards from guide, It is still the same concern with this:

On 4/14/2017 at 3:41 PM, KickStarter15 said:

"^$" and "^#" is not recognized as characters in "$oReplace" but if you change "^$" to "a" and "^#" to "1" then the code will execute replacing "$oFind".

The wildcard being searched is not valid when replacing "$oFind" with "$oReplace". Or maybe, I'm just being so mashup with my understanding with the guide.:sweating:

 

Programming is "To make it so simple that there are obviously no deficiencies" or "To make it so complicated that there are no obvious deficiencies" by C.A.R. Hoare.

Link to comment
Share on other sites

Does it make a difference if you set parameter $bMatchWildcards to True? Default si False.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Oops! missed that one.:)

Here it is, I tried having it declared as below but still having the same issue.

Find for numbers "[0-9]" followed by paragraph marks "^13" and replace with "[0-9]; " and the output is "word[0-9]; " from "word1^13".:(

$oFind = "[0-9]^13"
$oReplace = "[0-9]; "
With $oWord.Selection.Find
   .ClearFormatting
   .Replacement.ClearFormatting
;~    expression.Execute(FindText, MatchCase, MatchWholeWord, MatchWildcards, &_
;~ MatchSoundsLike, MatchAllWordForms, Forward, Wrap, Format, ReplaceWith, Replace,&_
;~ MatchKashida, MatchDiacritics, MatchAlefHamza, MatchControl)
   .Execute($oFind, false, false, true, false, false, true, false, false, $oReplace, $wdReplaceAll)
EndWith
MsgBox(0,"","Done!")

I think this is impossible to do right @water?:'( I just need to replace paragraph marks after the number with delimiter "; " but why so hard.

Tried it also this way, the same issue.

$oFind = "[0-9]^13"
$oReplace = "[0-9]; "
; _Word_DocFindReplace($oDoc, FindText, ReplaceText, $iReplace, $vSearchRange, $bMatchCase, $bMatchWholeWord, $bMatchWildcards)
_Word_DocFindReplace($oDoc, $oFind, $oReplace, $wdReplaceOne, False, False, False, True)

 

Edited by KickStarter15

Programming is "To make it so simple that there are obviously no deficiencies" or "To make it so complicated that there are no obvious deficiencies" by C.A.R. Hoare.

Link to comment
Share on other sites

Can't test at the moment but I think it could be something like this:

$oFind = "([0-9])^13"
$oReplace = "\1; "

 

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

@water I admit that I really need to understand the guide you shared to me. Thanks a lot, that simple code do the trick. :D

Thank you so much Water. Now I should read and understand this guide.:lol:

Programming is "To make it so simple that there are obviously no deficiencies" or "To make it so complicated that there are no obvious deficiencies" by C.A.R. Hoare.

Link to comment
Share on other sites

Glad this little piece of code works for you.
I will add have added the link to the wiki for future reference.

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...