Jump to content
Sign in to follow this  
myspacee

change text in .doc file

Recommended Posts

myspacee

Hello,

i've created .doc with barcode.

I want to replace text (is in readable form inside .doc) without touch file structure to avoid destroy it.

Manually things work, so I need :

- to open file

- replace text :

- save it in another .doc

write this (bad) script:

$file = FileOpen("Copia di doc2.doc", 1)
$file_read = FileRead($file)

$changed = StringReplace($file_read, "IT0900071","IT09XXX71")

$fileb = FileOpen("test.doc", 10)
FileWriteLine($fileb, $changed)
FileClose($file)

but doesn't works...

Can anyone correct me ?

thank you,

m.

Edited by myspacee

Share this post


Link to post
Share on other sites
Authenticity

The first call to FileOpen() was called with the last parameter set to 1 (write mode) so you can't read it. Just read the entire file without bothering to open a handle associated with it, like: $sSource = FileRead("FileName.doc").

Another thing that I never have had experience before is opening a file with "Create directory structure if it doesn't exist (8) option. It seems to me much simpler to open it just with the last parameter set to 2.

Share this post


Link to post
Share on other sites
myspacee

read now replies, thank you all, now testing code

m.

Share this post


Link to post
Share on other sites
myspacee

test failed on my word

>"C:\Programmi\AutoIt3\SciTE\..\autoit3.exe" /ErrorStdOut "C:\Documents and Settings\a.user\Desktop\Nuovo AutoIt v3 Script.au3" 
C:\Programmi\AutoIt3\Include\Word.au3 (459) : ==> The requested action with this object has failed.:
$o_doc = $o_object.Documents.Open ($s_FilePath, $f_ConfirmConversions, $f_ReadOnly, $f_AddToRecentFiles, $s_PasswordDocument, "", $f_Revert, $s_WritePasswordDocument, "", $i_Format)
$o_doc = $o_object.Documents.Open ($s_FilePath, $f_ConfirmConversions, $f_ReadOnly, $f_AddToRecentFiles, $s_PasswordDocument, "", $f_Revert, $s_WritePasswordDocument, "", $i_Format)^ ERROR
>Exit code: 1   Time: 1.093

my file has not password, and is in office 2003 format.

anyone can help ?

m.

Edited by myspacee

Share this post


Link to post
Share on other sites
myspacee

James thank you for all,

taken this from help

#include <Word.au3>
$oWordApp = _WordCreate (@ScriptDir & "\Copia di doc2.doc")
$oDoc = _WordDocGetCollection($oWordApp, 0)
$oFind = _WordDocFindReplace($oDoc, "IT0900071", "IT0900073")
If $oFind Then
    MsgBox(0, "FindReplace", "Found and replaced.")
Else
    MsgBox(0, "FindReplace", "Not Found with error : " & @Error)
EndIf
_WordQuit ($oWordApp, 0)

return error 7 ($_WordStatus_NoMatch) = No Match)

How this works ? is capable to read inside a text box ?

thank you again for your time,

m.

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.