Sign in to follow this  
Followers 0
crystalburner

controlsettext

20 posts in this topic

doesnt seem to work on word docs

i see it requires a control ID

word doesnt seem to return one to the autoitwindow info tool

:whistle:

Share this post


Link to post
Share on other sites



Is there a question here?


[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

Is there a question here?

Scary I know but one of you will be the lucky guy to be awarded the 5 * scripter of the month award. So who is it going to be then?

Who will be first, who is the best? Who is the mac daddy of AutoIT?

Ready, set..... GO FOR YOUR KEYBOARDS!

Share this post


Link to post
Share on other sites

Scary I know but one of you will be the lucky guy to be awarded the 5 * scripter of the month award. So who is it going to be then?

Who will be first, who is the best? Who is the mac daddy of AutoIT?

Ready, set..... GO FOR YOUR KEYBOARDS!

LOL

Ok well to put it simply

i need to change the text of a document without using SEND commands

any ideas?

(its word 2003)

Share this post


Link to post
Share on other sites

:whistle:

You need to ask a question (preferably with as much detail as possible) before we can help you.

Share this post


Link to post
Share on other sites

:whistle:

You need to ask a question (preferably with as much detail as possible) before we can help you.

I have a word document that contains dummy pieces of text

like this

[name]

[address1]

[address2]

Dear [name]

I have a script that contains variables like

$name = "nick christou"

$address1="6 south island place"

$address2="London"

I want to modify the word document to replace these dummy pieces of text with my strings

kind of like

ControlSetText ( "mydoc.doc - Microsoft Word", "[name]", "", $name , -1 )

except this doesnt work, i think because word returns no control IDs hence i have left it blank and also tried edit1 and -1

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

When you say "dummy pieces", are you refering to fields? If so then you will need to use com objects to update the fields with your variables. Otherwise if they are just text, then you can still use com but with a find method.

Edit: spelling

Edited by big_daddy

Share this post


Link to post
Share on other sites

When you say "dummy pieces", are you refering to fields? If so then you will need to use com objects to update the fields with your variables. Otherwise if they are just text, then you can still use com but with a find method.

Edit: spelling

i cant use COM cos smoke doesnt know how to do that

Share this post


Link to post
Share on other sites

i cant use COM cos smoke doesnt know how to do that

and to be honest neither do I

surely this is a fairly common request has nobody had to do this be4?

Share this post


Link to post
Share on other sites

i cant use COM cos smoke doesnt know how to do that

:whistle:

Do you have some kind of contract with SmOke_N that I'm not aware of?

Share this post


Link to post
Share on other sites

Ha!!, how the hell did I get brought into this :whistle:


[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

Ha!!, how the hell did I get brought into this :whistle:

you're my mate smokey

Share this post


Link to post
Share on other sites

you're my mate smokey

There are lots of contacts with SmOke you aren't aware of.

Ask his wife

:whistle:

Share this post


Link to post
Share on other sites

There are lots of contacts with SmOke you aren't aware of.

Ask his wife

:whistle:

im off

and dissapointed cos i've seen some impressive stuff here but nobody seems to be able to just change some text in a word doc

im amazed

i think there will be a workaround, open the file in raw mode and replace things (ouch) use another prog to do the replace, @SW_HIDE word and use SEND keys

dunno

will let you know anyhow what worked

Share this post


Link to post
Share on other sites

#15 ·  Posted (edited)

So do you want help on this or not? If you do, then you need to answer my question above.

Edit: Just read your previous post. Do you seriously expect an answer to your "question" with what you have provided so far?

Edited by big_daddy

Share this post


Link to post
Share on other sites

So do you want help on this or not? If you do, then you need to answer my question above.

Edit: Just read your previous post. Do you seriously expect an answer to your "question" with what you have provided so far?

What do you need to know?

Open word

type in [blank]

save it

now change that [blank] piece of text u just typed automatically

i really dont understand what else i need to tell you

i want to do an automatic find and replace on a word doc

im shocked nobody has done this before

Share this post


Link to post
Share on other sites

So do you want help on this or not? If you do, then you need to answer my question above.

Edit: Just read your previous post. Do you seriously expect an answer to your "question" with what you have provided so far?

What do you need to know?

Open word

type in [blank]

save it

now change that [blank] piece of text u just typed automatically

i really dont understand what else i need to tell you

i want to do an automatic find and replace on a word doc

im shocked nobody has done this before

Share this post


Link to post
Share on other sites

This does what you are wanting.

Requires:

Word.au3 - check my signature

#include <Word.au3>

; Register the default Word.au3 COM Error Handler
_WordErrorHandlerRegister ()

; Specify a file path for later use
$sFilePath = @ScriptDir & "\Test.doc"

; Try to attach to existing word window with the specified
; file, if one does not exist it will be created
$oWordApp = _WordCreate ($sFilePath)
Sleep(2000)
; Get reference to the active document
$oDoc = $oWordApp.ActiveDocument
; Get a range of the entire document
$oRng = $oDoc.Range
; Set the text for the entire document
$oRng.Text = "[name]" & @CRLF & @CRLF & _
        "[address1]" & @CRLF & @CRLF & _
        "[address2]" & @CRLF & @CRLF & _
        "Dear [name]"

Sleep(2000)
; Close document, saving changes, with original document format
_WordDocumentClose($oDoc, -1, 1)

Sleep(2000)
; Open the document, returning the active document object
$oDoc = _WordDocumentOpen($oWordApp, $sFilePath)
Sleep(1000)
; Find and replace text within the document
_FindReplace($oDoc, "[name]", "Joe Shmoe")
Sleep(1000)
; Find and replace text within the document
_FindReplace($oDoc, "[address1]", "12345 Broadway Ave.")
Sleep(1000)
; Find and replace text within the document
_FindReplace($oDoc, "[address2]", "Los Angeles, CA 54321")

Func _FindReplace($o_object, $s_search, $s_replace)
    If Not IsObj($o_object) Then
        SetError(1)
        Return 0
    EndIf
    
    $o_Rng = $o_object.Range
    $o_Find = $o_Rng.Find
    
    $o_Find.Execute ( _
            $s_search, _ ; FindText
            False, _ ; MatchCase
            True, _ ; MatchWholeWord
            False, _ ; MatchWildcards
            False, _ ; MatchSoundsLike
            False, _ ; MatchAllWordForms
            True, _ ; Forward
            1, _ ; wdFindStop = 0, wdFindContinue = 1, wdFindAsk = 2
            False, _ ; Format
            $s_replace, _ ; ReplaceWith
            2)  ; wdReplaceNone = 0, wdReplaceOne = 1, wdReplaceAll = 2
EndFunc   ;==>_FindReplace

Share this post


Link to post
Share on other sites

BD i will give this a go sorry i didnt reply earlier it was 1am had to sleeeep!

This does what you are wanting.

Requires:

Word.au3 - check my signature

#include <Word.au3>

; Register the default Word.au3 COM Error Handler
_WordErrorHandlerRegister ()

; Specify a file path for later use
$sFilePath = @ScriptDir & "\Test.doc"

; Try to attach to existing word window with the specified
; file, if one does not exist it will be created
$oWordApp = _WordCreate ($sFilePath)
Sleep(2000)
; Get reference to the active document
$oDoc = $oWordApp.ActiveDocument
; Get a range of the entire document
$oRng = $oDoc.Range
; Set the text for the entire document
$oRng.Text = "[name]" & @CRLF & @CRLF & _
        "[address1]" & @CRLF & @CRLF & _
        "[address2]" & @CRLF & @CRLF & _
        "Dear [name]"

Sleep(2000)
; Close document, saving changes, with original document format
_WordDocumentClose($oDoc, -1, 1)

Sleep(2000)
; Open the document, returning the active document object
$oDoc = _WordDocumentOpen($oWordApp, $sFilePath)
Sleep(1000)
; Find and replace text within the document
_FindReplace($oDoc, "[name]", "Joe Shmoe")
Sleep(1000)
; Find and replace text within the document
_FindReplace($oDoc, "[address1]", "12345 Broadway Ave.")
Sleep(1000)
; Find and replace text within the document
_FindReplace($oDoc, "[address2]", "Los Angeles, CA 54321")

Func _FindReplace($o_object, $s_search, $s_replace)
    If Not IsObj($o_object) Then
        SetError(1)
        Return 0
    EndIf
    
    $o_Rng = $o_object.Range
    $o_Find = $o_Rng.Find
    
    $o_Find.Execute ( _
            $s_search, _ ; FindText
            False, _ ; MatchCase
            True, _ ; MatchWholeWord
            False, _ ; MatchWildcards
            False, _ ; MatchSoundsLike
            False, _ ; MatchAllWordForms
            True, _ ; Forward
            1, _ ; wdFindStop = 0, wdFindContinue = 1, wdFindAsk = 2
            False, _ ; Format
            $s_replace, _ ; ReplaceWith
            2)  ; wdReplaceNone = 0, wdReplaceOne = 1, wdReplaceAll = 2
EndFunc   ;==>_FindReplace

Share this post


Link to post
Share on other sites

BD i will give this a go sorry i didnt reply earlier it was 1am had to sleeeep!

yep

THANKS

BIG D

!

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