Jump to content
FrancescoDiMuro

[Solved] _Word_DocFindReplace() doesn't find and replace in Headers/Footers

Recommended Posts

FrancescoDiMuro
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

Thoughts:

  • I will always thank you for the time you spent for me.
    I'm here to ask, and from your response, I'd like to learn.
    By my knowledge, I can help someone else, and "that someone" could help in turn another, and so on.

/*--------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/

ALWAYS GOOD TO READ:

 

Share this post


Link to post
Share on other sites
water

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

 

  • Thanks 2

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
FrancescoDiMuro

@water
Works like a charm :)

Thanks Sir!
Have a good day :)


Best Regards.


Click here to see my signature:

Spoiler

Thoughts:

  • I will always thank you for the time you spent for me.
    I'm here to ask, and from your response, I'd like to learn.
    By my knowledge, I can help someone else, and "that someone" could help in turn another, and so on.

/*--------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/

ALWAYS GOOD TO READ:

 

Share this post


Link to post
Share on other sites
water

Thanks :)
Glad the problem could be solved.

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

  • Like 1

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
FrancescoDiMuro

@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

Thoughts:

  • I will always thank you for the time you spent for me.
    I'm here to ask, and from your response, I'd like to learn.
    By my knowledge, I can help someone else, and "that someone" could help in turn another, and so on.

/*--------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/

ALWAYS GOOD TO READ:

 

Share this post


Link to post
Share on other sites
FrancescoDiMuro
Posted (edited)

@water Bump.

I'd like to use this thread instead of open a new one :)

 

Best Regards.

Edited by FrancescoDiMuro

Click here to see my signature:

Spoiler

Thoughts:

  • I will always thank you for the time you spent for me.
    I'm here to ask, and from your response, I'd like to learn.
    By my knowledge, I can help someone else, and "that someone" could help in turn another, and so on.

/*--------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/

ALWAYS GOOD TO READ:

 

Share this post


Link to post
Share on other sites
water

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


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
FrancescoDiMuro

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

Thoughts:

  • I will always thank you for the time you spent for me.
    I'm here to ask, and from your response, I'd like to learn.
    By my knowledge, I can help someone else, and "that someone" could help in turn another, and so on.

/*--------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/

ALWAYS GOOD TO READ:

 

Share this post


Link to post
Share on other sites
AutoBert

It works for me without any problem.

Share this post


Link to post
Share on other sites
water
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 (2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
FrancescoDiMuro
Posted (edited)

@AutoBert

For me, it exits...

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

Edited by FrancescoDiMuro

Click here to see my signature:

Spoiler

Thoughts:

  • I will always thank you for the time you spent for me.
    I'm here to ask, and from your response, I'd like to learn.
    By my knowledge, I can help someone else, and "that someone" could help in turn another, and so on.

/*--------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/

ALWAYS GOOD TO READ:

 

Share this post


Link to post
Share on other sites
FrancescoDiMuro
Posted (edited)

@water

Yes, I provided a document with just one page, because it isn't working with one page either! 

By the way, I am working with Microsoft Office 2016.

Thanks Water :)

Edited by FrancescoDiMuro

Click here to see my signature:

Spoiler

Thoughts:

  • I will always thank you for the time you spent for me.
    I'm here to ask, and from your response, I'd like to learn.
    By my knowledge, I can help someone else, and "that someone" could help in turn another, and so on.

/*--------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/

ALWAYS GOOD TO READ:

 

Share this post


Link to post
Share on other sites
AutoBert
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.

Share this post


Link to post
Share on other sites
FrancescoDiMuro

@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

Thoughts:

  • I will always thank you for the time you spent for me.
    I'm here to ask, and from your response, I'd like to learn.
    By my knowledge, I can help someone else, and "that someone" could help in turn another, and so on.

/*--------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/

ALWAYS GOOD TO READ:

 

Share this post


Link to post
Share on other sites
Subz

You just need to remove the following from with the loop:

If @error Then Exit

 

Share this post


Link to post
Share on other sites
water

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 (2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
water

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 (2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
FrancescoDiMuro
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

Thoughts:

  • I will always thank you for the time you spent for me.
    I'm here to ask, and from your response, I'd like to learn.
    By my knowledge, I can help someone else, and "that someone" could help in turn another, and so on.

/*--------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/

ALWAYS GOOD TO READ:

 

Share this post


Link to post
Share on other sites
water

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 ;)

  • Like 1

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
FrancescoDiMuro

@water

I will wait for it!

Should I mark as "Solved" this thread, or you want to update as soon as you document this thing in the Word UDF Wiki? :)

Thanks for your help :)

 

Best Regards.


Click here to see my signature:

Spoiler

Thoughts:

  • I will always thank you for the time you spent for me.
    I'm here to ask, and from your response, I'd like to learn.
    By my knowledge, I can help someone else, and "that someone" could help in turn another, and so on.

/*--------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/

ALWAYS GOOD TO READ:

 

Share this post


Link to post
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

  • Similar Content

    • Tersion
      By Tersion
      Here the this wiki page with list of available UDFs for data compression. For my tasks I only need ZIP support, so I started looking at pure AutoIt UDFs without any 3rd party dlls. And found out that most of available realizations uses standard ("native method") Windows dll - "zipfldr.dll". So for now I chose ZIP UDF by wraithdu. I've tested it on Windows 7 (x64) and it seem works fine. But here the comment from another topic where user says that Windows 10 discontinued support of "zipfldr.dll". Now I confused. I don't have around any Windows 10 machine to tested it. So maybe someone could confirm or deny that? Or maybe would better to switch to UDF with 7zip dll?
      I need an advice...
    • TheSaint
      By TheSaint
      Here is the bare bones of a UDF I have started work on.
      Mostly just a proof of concept at this stage, and still need to add some functions and dress the UDF up a bit ... to look like a UDF ... though it has my own distinct styling, especially as I have never really developed a UDF before now .... used plenty and modified plenty though. I've even invented my own UDF variable naming convention, which I am sure some of you will be aghast at. I work with what feels best for me, but others are free to adapt if they wish.
      The idea is to emulate the simplicity of INI files, but gain the benefits of SQL.
      Two scripts are provided.
      (1) The UDF, a work in progress - SimpleSQL_UDF.au3
      (2) An example or testing script - UDF_Test.au3
      Another first for me, is creating a 2D array from scratch, never done that before, that I can recall ... never had a need, and even for 1 dimension arrays, for a long time now, I have just used _StringSplit to create them. So I needed a bit of a refresher course, which my good buddy @TheDcoder assisted me with ... not without some angst I might add. LOL
      SimpleSQL_UDF.zip  (12 downloads previously)
      (I have now completed all the functions I intended to. My next update will be a big improvement, bringing things more inline with my latest INItoSQL DB program changes.)
      Program requires the sqlite3.dll, not included, but easily enough obtained.
      Hopefully the usage is self-evident ... just change the Job number variable in the UDF_Test.au3 file to check the existing functions out.
      Enjoy!
      P.S. This is also related to a new program I have just finished and uploaded - INItoSQL DB
    • lavascript
      By lavascript
      I have a Word document containing a 9-column table where row 1 is the column headers. My goal is to read the table into a 2d array, remove some rows, update some fields, and add a few rows to the end. The resulting array will likely be a different length. Next, I want to write the data back into the table. If it's easier, I can write the data to a new document from a template containing the same table header with a blank 2nd row.
      Here's my early attempt:
      Local $oWord = _Word_Create() Local $oDoc = _Word_DocOpen($oWord, $sFile) Local $aData = _Word_DocTableRead($oDoc, 1) $aData[3][5] = "Something else" Local $oRange = _Word_DocRangeSet($oDoc, 0) $oRange = _Word_DocRangeSet($oDoc, $oRange, $wdCell, 9) _Word_DocTableWrite($oRange,$aData) This, unfortunately, writes the entire array into the first cell of row 2. What am I doing wrong?
       
    • Rskm
      By Rskm
      Hi, I have the following line in a text file 'input.txt'. I know the line number - say '6'. I wish to replace the text 'WWW' in the below line with a random number (I can generate that with random()).
      WERIS  WWWJP   3.83  8.330  1.000                1097.RAXX 
      The WWW is a 3 digit integer (could be any number between 0 to 999), I can use stringtrimleft and get the numerical value of WWW in this file
      so, basically, I know the string to replace (ie; WWW stored in a variable), I know the line number to work on and the file location/name and the replacement variable (through random()). My requirement is to fill that 3 spaces with my random number (which Is a integer between 1 and 999)
      please put ur suggestions
       
    • xtcislove
      By xtcislove
      Hello,
      as a start in Autoit i tried something i was missing since im using Autoit. 

      I build a custom MessageBox which has a large amount of custom options and which scales its size on the parameters you set. 

      Aviable Settings:
      -Title
      -Unlimited Buttons
      -Text Color (Buttons, Text)

      -Background Color (Msgbox, Buttons, Label) 

      -Button Timeout
      -Autoclose Timeout
      -Icon (Default, No Icon, Custom)

      -Label/ Button Style. 
      -Transparency

      I tried to keep this as close as i could to a Msgbox i was used too on my batch times.

      After i was ready i realised, @Melba23 probably build a way better msgbox which would have suit my needs enterly, anyway thanks to @Melba23 because i use his Stringsize UDF. 

       
       
      local $Message = _sMsgBox("Test", 6, "Continue?") if @extended <> -1 Then MsgBox(0, @extended, $Message&" Button pressed")  
      ScalingMessageBox.au3
×