Jump to content
Sign in to follow this  
ajit

_WordDocFindReplace issue

Recommended Posts

ajit

#include <Word.au3>
$oWordApp = _WordCreate (@ScriptDir & "\Test.doc")
$oDoc = _WordDocGetCollection($oWordApp, 0)
$oDoc.Range.insertAfter("this" & @CR)

FileDelete("a.txt")
$file = FileOpen("a.txt", 1)
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

For $i = 1 to 10 ; IF THIS NUMBER IS CHANGED FROM 10 TO 100 _WordDocFindReplace DOES NOT WORK
FileWrite($file, "Line " & $i)
Next
FileClose($file)

FileOpen("a.txt")
$String_to_replace  = Fileread($file)

$oFind = _WordDocFindReplace($oDoc, "this", $String_to_replace)

Hi,

Please see attached script wherein I create a text file and then use Readfile to get text from that file to use in _WordDocFindReplace but as soon as I increase the number of characters in text file _WordDocFindReplace will not work.

Can someone guide me on this?

Regards

Ajit

Share this post


Link to post
Share on other sites
MrMitchell

Any work around possible?

Yes.

From your For loop:

For $i = 1 to 100 ; IF THIS NUMBER IS CHANGED FROM 10 TO 100 _WordDocFindReplace DOES NOT WORK
FileWrite($file, "Line " & $i)
Next
FileClose($file)

FileOpen("a.txt")
$String_to_replace  = Fileread($file)
ClipPut($String_to_replace)             ;Put it on the clipboard

$oFind = _WordDocFindReplace($oDoc, "this", "^c")       ;Per MSDN documentation use this ^c to paste from the clipboard.

Reference: http://msdn.microsoft.com/en-us/library/....word.find.execute%28v=office. Scroll down to ReplaceWith to see where I got this from. They stated this is how you would use it for a picture but it seems to work fine with text too.

To answer your original question, I have no idea if there's a limit to the number of characters, couldn't figure that out. But at least you have a workaround.

Edited by MrMitchell
  • Like 1

Share this post


Link to post
Share on other sites
ajit

@MrMitchell,

That was a pleasent surprise. Worked flawlessly.

Thanks very much for your effort in helping me out.

Regards,

Ajit

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  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.