Jump to content

Recommended Posts

Posted (edited)

Good evening everyone :)
I am working with Word UDF ( thanks @water! ), and, especially, with the function _Word_DocFindReplace().
The replace does work everywhere in the document, but, it does not work in Headers or Footers.
Am I missing something or am I forced to use the code below? :)
I have already looked in the Help file ( about _Word_DocFindReplace() ), but there are no mentions about replace text in Headers/Footers.

Sub FindAndReplaceFirstStoryOfEachType()

  Dim rngStory As Range
  
  For Each rngStory In ActiveDocument.StoryRanges
    With rngStory.Find
      .Text = "find text"
      .Replacement.Text = "I'm found
      .Wrap = wdFindContinue
      .Execute Replace:=wdReplaceAll
    End With
  Next rngStory

End Sub

Thanks everyone in advance :)


Best Regards.

Edited by FrancescoDiMuro

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

Posted

Correct. You need to specify the StoryRange to process. To do the find/replace operation everywhere you need to do something like:

For $oStoryRange In $oDoc.StoryRanges
    _Word_DocFindReplace($oDoc, "Find", "ReplaceWith", Default, $oStoryRange)
Next

 

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

 

Posted

Thanks :)
Glad the problem could be solved.

I think I will add a section about "story" to the Word wiki ;)

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

 

Posted

@water
I specified the range in the _Word_DocFindReplace() function as you suggested to me, but I find out that the replace has been done only in the first page...
I'm not so practice with Word object, and, English is not by my side when I try to translate "story" in Italian :)
I was looking at this, and so, I don't know if I have to specify to move to the next "story", in order to apply find/replace to all pages.
Can you help me to understand this?
Thank you again :) And...

1 hour ago, water said:

I think I will add a section about "story" to the Word wiki ;)

For everything helps someone, it will always deserves to be a peace of "story" ;)


Best Regards.

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

Posted

Can you provide a sample document and a reproducer script so we can play with it?

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

 

Posted

Sure @water :)
Here you are:

#include <MsgBoxConstants.au3>
#include <Word.au3>
#include <WordConstants.au3>

Global $objWord, _
       $objWordDocument, _
       $strWordDocument = @ScriptDir & "\Word_Document.docx", _
       $objStoryRange

Global $objWord = _Word_Create(False, False)
If @error Then
    ConsoleWrite("Error while creating the Word object. Error: " & @error & @CRLF)
Else
    $objWordDocument = _Word_DocOpen($objWord, $strWordDocument)
    If @error Then
        ConsoleWrite("Error while opening the document and " & $strWordDocument & ". Error: " & @error & @CRLF)
    Else

        For $objStoryRange In $objWordDocument.StoryRanges
            _Word_DocFindReplace($objWordDocument, "ABCD", "Hey!", $WdReplaceAll, $objStoryRange)
            If @error Then Exit
        Next

        _Word_DocSave($objWordDocument)
        If @error Then
            ConsoleWrite("Error while saving the document. Error: " & @error & @CRLF)
        Else
            _Word_DocClose($objWordDocument)
            If @error Then
                ConsoleWrite("Error while closing the document. Error: " & @error & @CRLF)
            Else
                _Word_Quit($objWord)
                If @error Then
                    ConsoleWrite("Error while closing Word application. Error: " & @error & @CRLF)
                Else
                    MsgBox($MB_ICONINFORMATION, "", "Word Document saved correctly.")
                EndIf
            EndIf
        EndIf
    EndIf
EndIf

Word_Document.docx

Thanks again for your help :)


Best Regards.

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

Posted
On 6.7.2018 at 5:24 PM, FrancescoDiMuro said:

I specified the range in the _Word_DocFindReplace() function as you suggested to me, but I find out that the replace has been done only in the first page...

The example document you provided only consists of a single page (I checked on Linux using LibreOffice). Can you please provide an example document that you tested and that didn't work?

BTW: Which version of Word do you run?

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

 

Posted
3 hours ago, FrancescoDiMuro said:

For me, it exits...

I put an error check after the _Word_DocFindReplace(), and the error code was 3, extended was 0...

Sorry i changed document in the way that in header, body and footer a replacestring exists. Without you have to delete line #21, seems that body is the first range in FindReplace order. I use a 2013 Home & Student version.

Posted

@AutoBert

Yes.

The body is the first place where the function does the research, and, if it doesn't find anything, it throws @error = 3 and @extendend = 0.

But, even if I specify the StoryRange to do the find/replace, it seems to not find anything.

So, am I missing something? :)

Thanks for your help.

 

Best Regards.

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

Posted

Here it works the same way as Subz describes. Remove the error check and it does what it is intended to do.
I will check where the @error = 3 comes from.

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

 

Posted

That's kind of a bug :>

_Word_DocFindReplace returns @error = 3 when the Word Find.Execute method returns an error AND when the find/replace was not successful.
This means:
@error = 3 and @extended = 0: Search string was not found and everything else what MS describes as "not successful"
@error = 3 and @extended <> 0: A real error happened and @extended gets set to the COM error (HRESULT).

So check @extended for a real error ;)

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

 

Posted (edited)

@water
Definitely a bug :D

2 hours ago, water said:

@error = 3 and @extended = 0: Search string was not found and everything else what MS describes as "not successful"

Search string was not found in the body of the Document, since the text to find is in the Header of the Document.
By now, I tried to do the replacement of a string in the Document with more than one page, and it works correctly.
But now, I'm trying with the "original" document where to find/replace strings, and it does the replace only in the first sheet of the document...
I don't know what to think...

Thanks :)

EDIT:

This peace of code does the trick, as it is stated in this tutorial: 

Local $objStoryRange

For $objStoryRange In $objWordDocument.StoryRanges
    Do
        _Word_DocFindReplace($objWordDocument, "XNOMEX", $arrResult[1][0], Default, $objStoryRange)
        _Word_DocFindReplace($objWordDocument, "XDESCRIZIONEX", $arrResult[1][1], Default, $objStoryRange)
        $objStoryRange = $objStoryRange.NextStoryRange
    Until IsObj($objStoryRange) = 0 ; I don't know if this statement is correct, since I have to loop until objStoryRange Is Nothing
Next


Best Regards,

Edited by FrancescoDiMuro

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

Posted

Glad you got it working :)

Just found a german document that explains why you need to use the NextStoryRange property.
Will think about how to document this behavior in the wiki ;)

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

 

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
×
×
  • Create New...