Sign in to follow this  
Followers 0
WalmartCart

AutoIT - Word Document Word Replacement Question

7 posts in this topic

Hi AutoIT folks,

I have a question on the _WordDocFindReplace() Function. I would like to describe the scenario that I have. Thanks in advanced!

Here I have a three dimensional array: $Data[10][10][10]. 

Say each element already has its value and I want to parse some of the value/element into a Word file.

In the Word Document that I have, it looks like this:

Iamtext

Iamtext

[.data,1,1,1]

[.data,2,3,5]

Iamtext

Iamtext

Iamtext

[.data,3,5,7]

Iamtext

[.data,7,2,10]

[.data,2,1,3]

Each [.data,x,y,z] is unique through out the word document.

What I would like to achieve is to search for "[.data", and replace the whole [.data,x,y,z] string with the value stored in $Data[x][y][z]

Once it is replaced, I would like to repeat it until every [.data,x,y,z] is replaced by $Data[x][y][z]

I tried to use _WordDocFindReplace() to achieve this, but I found out I would have to input the whole string(e.g. [.data,1,1,1]) to search for the word and replace it.

Plus, I don't want to hard code it so it will search for every element (10x10x10 = 1000 elements). Because the word file might only need 20 data to be parsed.

Again, the goal is to "search in the .doc file" for "[.data" and recognize the 3 numbers after that so I can then replace the whole string with $Data[x][y][z]

Or, is there other method/structure is an alternative for me?

Thanks a lot. 

Share this post


Link to post
Share on other sites



Welcome to Autoit and the forum!

As

 

Each [.data,x,y,z] is unique through out the word document.

why don't you search for this unique strings?


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Hi Water, 

Thanks for the reply and the warm greeting! :bye:

 

why don't you search for this unique strings?

 

The reason that I would not search for the unique strings is because the document might want to have other data in the future. Or maybe there would be other type of document that uses $Data[][][] Value as well, yet need different set of the data. Hence, I prefer to have a generic code.

I have thought about this issue last night. And I came out with another way to achieve this:

  • Instead of using the Word UDF, I can use FileRead to search for "[.data" String and do some parsing manipulation to store all of the a,b,c value in an array. From there, I can then go back to Word UDF, and search from those pieces and replace them.

However, I am still wondering if it can be achieved by using Word UDF only. If not, there are many alternatives like you suggest.

I read your WordEX.au3 -> _Word_DocFind() and I saw there is this $bMatchWildcards. Would it help me in my situation?

Sincerely,

WalmartCart

Edited by WalmartCart

Share this post


Link to post
Share on other sites

You could use my rewrite of the Word UDF. Use function _Word_RangeFind to search for "[.data", then use _Word_RangeSet to extend the end of the range by 6 words and 1 character to the right of the range so the range covers [.data,x,y,z] and then replace the range with the new content.

Sounds a bit complex at the moment but isn't too hard to do ;)


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Example to be run with the latest AutoIt beta version. Changes all "[data" occurrences:

#include <Word.au3>

; Create application object
Global $oWord = _Word_Create()
If @error <> 0 Then Exit MsgBox(16, "Word Example", "Error creating a new Word application object." & @CRLF & "@error = " & @error & ", @extended = " & @extended)
; Open document
Global $oDoc = _Word_DocOpen($oWord, "C:\temp\test.docx")
If @error <> 0 Then Exit MsgBox(16, "Word Example", "Error opening 'Test.doc'." & @CRLF & "@error = " & @error & ", @extended = " & @extended)

While 1
    $oRange = _Word_DocFind($oDoc, "[.data")
    If @error Then ExitLoop
    $oRange = _Word_DocRangeSet($oDoc, $oRange, Default, Default, $WdWord, 6) ; Move end of range 6 words to the right
    $oRange = _Word_DocRangeSet($oDoc, $oRange, Default, Default, $WdCharacter, 1) ; Move end of range 1 character to the right
    $oRange.Text = "[Replacement Text]"
WEnd
MsgBox(0, "", "Finished")
_Word_DocClose($oDoc)
_Word_Quit($oWord)
Exit

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Hi Water,

This is exactly what I wanted!

 

Example to be run with the latest AutoIt beta version. Changes all "[data" occurrences:

#include <Word.au3>

; Create application object
Global $oWord = _Word_Create()
If @error <> 0 Then Exit MsgBox(16, "Word Example", "Error creating a new Word application object." & @CRLF & "@error = " & @error & ", @extended = " & @extended)
; Open document
Global $oDoc = _Word_DocOpen($oWord, "C:\temp\test.docx")
If @error <> 0 Then Exit MsgBox(16, "Word Example", "Error opening 'Test.doc'." & @CRLF & "@error = " & @error & ", @extended = " & @extended)

While 1
    $oRange = _Word_DocFind($oDoc, "[.data")
    If @error Then ExitLoop
    $oRange = _Word_DocRangeSet($oDoc, $oRange, Default, Default, $WdWord, 6) ; Move end of range 6 words to the right
    $oRange = _Word_DocRangeSet($oDoc, $oRange, Default, Default, $WdCharacter, 1) ; Move end of range 1 character to the right
    $oRange.Text = "[Replacement Text]"
WEnd
MsgBox(0, "", "Finished")
_Word_DocClose($oDoc)
_Word_Quit($oWord)
Exit

 

By reviewing at your code, I realize $oRange.Text gives the string within the Range.

Sadly, I am not familiar with the object in the word function and I didn't know that we can use those API/Object in AutoIT as well :zorro: (Any good site that I can learn about these objects?)

Anyways, I don't know why I would want to extend a character to the right though. I just moved 6 words and read the text within that range.

Then, analyze the text within the string to know which element in $Data[][][] to used to replace the $oRange.Text

Thank you very much Water.

I am going to start another thread regarding to some image manipulation in Word as well. Looking forward to have such an awesome discussion with you again! 

 

Share this post


Link to post
Share on other sites

MSDN is all I use.

Or I use Google to search for an example script written in Visual Basic which then can easily be converted to AutoIt.

I just moved an additional character to the right to include the closing bracket.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

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
Sign in to follow this  
Followers 0