Jump to content

Word - search/replace issue with SPECIFIC document


Go to solution Solved by gerardsweeney,

Recommended Posts

Posted

Hi, there.

I've just started dabbling with the Word UDF, as I'm hoping to use it to batch populate a document.

The code below works absolutely fine for a dummy Word Document I've created, but not for the actual template document I need.

#include <word.au3>

ProcessWordDoc("TestDocument.docx")
ProcessWordDoc("OfficialDocument.docx")
Msgbox(0, "", "Done")
EXIT

Func ProcessWordDoc($F)
$FirstName = "Han"
$Surname = "Solo"
$Building = "Millenium Falcon"
$ID = "TK421"
$File = @ScriptDir & "\" & $F

; Create application object
Global $oWord = _Word_Create()
Global $oDoc = _Word_DocOpen($oWord, $File)

    For $oStoryRange In $oDoc.StoryRanges
        For $Loop = 1 to 1
        _Word_DocFindReplace($oDoc, "FIRSTNAME", $FirstName, Default, $oStoryRange)
        _Word_DocFindReplace($oDoc, "SECONDNAME", $Surname, Default, $oStoryRange)
        _Word_DocFindReplace($oDoc, "BUILDINGNAME", $Building, Default, $oStoryRange)
        _Word_DocFindReplace($oDoc, "000000000", $ID, Default, $oStoryRange)
        Next
    Next
EndFunc


I don't know much about Word, so bear with me here..

It looks like the official document has page 1 configured differently from other pages.
It has the "000000000" id text on page 1 in a text box, and then in the footer of subsequent pages.
Page 1 also has text boxes with the First name/surname/building fields (which I've populated with the FIRSTNAME/SECONDNAME/BUILDINGNAME strings for search/replace.


In Word, I can replace all of the Search/replace fields no problem (If I use CTRL H/Search/Replace/All).

With the above script, page 1 isn't being touched. The footer gets replaced, but nothing else.

I'm hoping it's something REALLY obvious that I'm missing, here?

I don't really want to attach the official document here - for fairly obvious reasons. I realise that doesn't help with troubleshooting.
 

All advice gratefully received!

Posted

I would start with collecting information about the document and adding error checking. Something like:

Consolewrite("Number of Stories:" & $oDoc.StoryRanges & @CRLF)
    For $oStoryRange In $oDoc.StoryRanges
        Consolewrite(@CRLF & "Type of Story:" & $oStoryRange.StoryType & @CRLF)
;        For $Loop = 1 to 1 ; Not needed at the moment
        _Word_DocFindReplace($oDoc, "FIRSTNAME", $FirstName, Default, $oStoryRange)
        Consolewrite("@error of Replace1: " & @error & @CRLF)
        _Word_DocFindReplace($oDoc, "SECONDNAME", $Surname, Default, $oStoryRange)
        Consolewrite("@error of Replace2: " & @error & @CRLF)
        _Word_DocFindReplace($oDoc, "BUILDINGNAME", $Building, Default, $oStoryRange)
        Consolewrite("@error of Replace3: " & @error & @CRLF)
        _Word_DocFindReplace($oDoc, "000000000", $ID, Default, $oStoryRange)
        Consolewrite("@error of Replace4: " & @error & @CRLF)
 ;       Next
    Next

The wdstorytype enumeration can be found here: https://learn.microsoft.com/en-us/office/vba/api/word.wdstorytype

My UDFs and Tutorials:

  Reveal hidden contents

 

Posted (edited)

Thanks for the suggestion.

Here's the results from the SciTE console..

I updated the script to include the @extended error as well, in case it helped.

I'm assuming error 3 means there was nothing found to replace.

It replaced for Story 9 - which is the primary footer. So that ties in with what I'm seeing.
 

Number of Stories:

Type of Story:1
@error of Replace1: 3 (Extended: 0)
@error of Replace2: 3 (Extended: 0)
@error of Replace3: 3 (Extended: 0)
@error of Replace4: 3 (Extended: 0)

Type of Story:5
@error of Replace1: 3 (Extended: 0)
@error of Replace2: 3 (Extended: 0)
@error of Replace3: 3 (Extended: 0)
@error of Replace4: 3 (Extended: 0)

Type of Story:6
@error of Replace1: 3 (Extended: 0)
@error of Replace2: 3 (Extended: 0)
@error of Replace3: 3 (Extended: 0)
@error of Replace4: 3 (Extended: 0)

Type of Story:7
@error of Replace1: 3 (Extended: 0)
@error of Replace2: 3 (Extended: 0)
@error of Replace3: 3 (Extended: 0)
@error of Replace4: 3 (Extended: 0)

Type of Story:8
@error of Replace1: 3 (Extended: 0)
@error of Replace2: 3 (Extended: 0)
@error of Replace3: 3 (Extended: 0)
@error of Replace4: 3 (Extended: 0)

Type of Story:9
@error of Replace1: 3 (Extended: 0)
@error of Replace2: 3 (Extended: 0)
@error of Replace3: 3 (Extended: 0)
@error of Replace4: 0 (Extended: 0)

Type of Story:10
@error of Replace1: 3 (Extended: 0)
@error of Replace2: 3 (Extended: 0)
@error of Replace3: 3 (Extended: 0)
@error of Replace4: 3 (Extended: 0)

Type of Story:12
@error of Replace1: 3 (Extended: 0)
@error of Replace2: 3 (Extended: 0)
@error of Replace3: 3 (Extended: 0)
@error of Replace4: 3 (Extended: 0)

Type of Story:13
@error of Replace1: 3 (Extended: 0)
@error of Replace2: 3 (Extended: 0)
@error of Replace3: 3 (Extended: 0)
@error of Replace4: 3 (Extended: 0)

Type of Story:15
@error of Replace1: 3 (Extended: 0)
@error of Replace2: 3 (Extended: 0)
@error of Replace3: 3 (Extended: 0)
@error of Replace4: 3 (Extended: 0)

Type of Story:16
@error of Replace1: 3 (Extended: 0)
@error of Replace2: 3 (Extended: 0)
@error of Replace3: 3 (Extended: 0)
@error of Replace4: 3 (Extended: 0)
>Exit code: 0    Time: 9.668

 

Edited by gerardsweeney
  • Solution
Posted

Water had very kindly looked at a blank copy of the document, and concluded the same as me - it's horrific :)

I've cobbled together a script to use SEND to trigger CTRL H to do the search/replacing.. Nowhere near as elegant as using the functions, but still infinitely preferable to doing it manually :)

 

 

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
  • Recently Browsing   0 members

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