Sign in to follow this  
Followers 0
sgebbie

Script stuck in While loop

12 posts in this topic

Script seems to get stuck in the While loop. It's working as designed, although I'm pretty sure I've got something wrong in the While loop. Could someone have a look and point me in the right direction as to what I might have messed up here? Also, will a WinWait command work on a locked computer? I can test it out if necessary but if someone knows that off the top of their head please let me know.

;Winword exists
#include "Word.Au3"
$oWordApp = ObjGet("", "Word.application")
_WordQuit ($oWordApp)
While WinWait("Microsoft Office Word")
    ControlClick("Microsoft Office Word", "", "[ID:6]")
    Sleep(2000)
    ControlSend("Save As", "", "", "{ENTER}")
WEnd

Exit

I'd also like to make this particular piece a function of a larger script. Is there anything special I need to do for that aside from the Func() and EndFunc? I tried this earlier and it was complaining constantly about no EndFunc. EndFunc was there but it still wasn't working for some reason.

Share this post


Link to post
Share on other sites



I read your script. I'm not following what you are trying to do. It makes no sense. You have While WinWait("Microsoft Office Word"). Winwait the way you are using it has no timeout. Also, you have it sleeping for 2 seconds, then clicking onto a control. What is the point of this script?

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

my guess is the while winwait line.

instead try something like

$var=winexists("Microsoft Office Word","do you want to save")

while $var=0

$var=winexists("Microsoft Office Word","do you want to save")

ControlClick("Microsoft Office Word", "", "[iD:6]")

Sleep(2000)

ControlSend("Save As", "", "", "{ENTER}")

wend

Edited by kaotkbliss

010101000110100001101001011100110010000001101001011100110010000

001101101011110010010000001110011011010010110011100100001

My Android cat and mouse game
https://play.google.com/store/apps/details?id=com.KaosVisions.WhiskersNSqueek

We're gonna need another Timmy!

Share this post


Link to post
Share on other sites

Thanks for the responses. I was messing with it a minute ago and was able to get past that part. I forgot about the WinExist. That's exactly what I was looking to do and ended up getting around it like this before I read your post.

#include "Word.Au3"
$oWordApp = ObjGet("", "Word.application")
_WordQuit ($oWordApp)
While ProcessExists("WINWORD.EXE")
    WinWait("Microsoft Office Word")
    ControlClick("Microsoft Office Word", "", "[ID:6]")
    Sleep(2000)
    ControlSend("Save As", "", "", "{ENTER}")
    Sleep(2000)
WEnd
EndFunc

This stopped the loop. I'm having some trouble turning this section of code into a function though. I get this kind of an error if i turn it into a Function like this.

If ProcessExists("WINWORD.EXE") Then Word()
    

;Winword exists
Func Word()
#include "Word.Au3"
$oWordApp = ObjGet("", "Word.application")
_WordQuit ($oWordApp)
While ProcessExists("WINWORD.EXE")
    WinWait("Microsoft Office Word")
    ControlClick("Microsoft Office Word", "", "[ID:6]")
    Sleep(2000)
    ControlSend("Save As", "", "", "{ENTER}")
    Sleep(2000)
WEnd
EndFunc

Exit

This is what it gives me when trying to make it a Func

Line 85 (File "C:\Program Files\AutoIt3\Include\Word.Au3"):

Func _WordCreate($s_FilePath= "", $f_tryAttach=0, $f_visible = 1, $f_takeFocus = 1)

Error: "Func" statement has no matching "EndFunc".

It looks like this is a result of an incorrect usage of the #include Word.Au3?

Share this post


Link to post
Share on other sites

Remember that #includes are just copied in the place the include-line is, and Word.Au3 contain lots of funcs, so that can obviously not work ;)

You need to move that #include. Normally you have them at the top of your script.

Share this post


Link to post
Share on other sites

#include "Word.Au3" ;includes go to the top of your script
If ProcessExists("WINWORD.EXE") Then _Word()
    
;Winword exists
Func _Word()

$oWordApp = ObjGet("", "Word.application")
_WordQuit ($oWordApp)
While ProcessExists("WINWORD.EXE")
    WinWait("Microsoft Office Word")
    ControlClick("Microsoft Office Word", "", "[ID:6]")
    Sleep(2000)
    ControlSend("Save As", "", "", "{ENTER}")
    Sleep(2000)
WEnd
EndFunc

Exit

Share this post


Link to post
Share on other sites

I read your script. I'm not following what you are trying to do. It makes no sense. You have While WinWait("Microsoft Office Word"). Winwait the way you are using it has no timeout. Also, you have it sleeping for 2 seconds, then clicking onto a control. What is the point of this script?

Sorry it's confusing, i'm pretty new to code so it's probably a bit hard to decipher why I would do some things this way for some of you that are advanced coders. Only about my first week or so using AutoIT or coding anything really.. Just using what I've been able to figure out so far between the Help file, the boards and some questions I've posted on here.

Basically my goal is for the script to check for the WINWORD.exe process and if found move to the next stage which is the part I posted the code for. In this step, I want to save and close only currently open word documents and then exit back to the main script once that part has completed. I have it somewhat working now and it's not looping. I'm stuck on the Func part since I need this piece of code somewhat seperate from the main script.

The post above has the code and the error after attempting to make this piece a Func

Hope I'm making some sense with this ;) has been a lot to take in

Share this post


Link to post
Share on other sites

Remember that #includes are just copied in the place the include-line is, and Word.Au3 contain lots of funcs, so that can obviously not work ;)

You need to move that #include. Normally you have them at the top of your script.

So for any includes that I want to use they should go at the beginning of the main script? I'm guessing that since it's at the beginning of the main script it would still work correctly if this section is say..a sub section of the main script? Like making it a Func as I'm trying to do? If I'm using both a Word.Au3 and an Excel.Au3 both would go to the top of the main script?

Share this post


Link to post
Share on other sites

What you may want to do is write a action story of just what you have in mind. If I'm following your story right:

1. Script loads

2. Script idles around looking for winword.exe to exist.

3. Winword.exe is found to exist.

4. Retrieve COM object for winword process

5. You have _WordQuit which removes the object reference. You sure you want to do this here?

6. ControlSend to a specific control in the word window. Could one of the _Word commands work here instead?

7. Save as - You could use _WOrdDocSave here.

8. Once saved, then what? Do you want to close it? If so, then use _WordQuit here.

Share this post


Link to post
Share on other sites

#include "Word.Au3" ;includes go to the top of your script
If ProcessExists("WINWORD.EXE") Then _Word()
    
;Winword exists
Func _Word()

$oWordApp = ObjGet("", "Word.application")
_WordQuit ($oWordApp)
While ProcessExists("WINWORD.EXE")
    WinWait("Microsoft Office Word")
    ControlClick("Microsoft Office Word", "", "[ID:6]")
    Sleep(2000)
    ControlSend("Save As", "", "", "{ENTER}")
    Sleep(2000)
WEnd
EndFunc

Exit

Thank you for the code edit. Things are starting to make a lot more sense. If I had a second function similar to the one shown here for say Excel. The #include "Excel.Au3" can go right above or below the Word #include correct? Do they have to follow any certain order? Word.Au3 first if that happens first in the script? Or is it not that specific?

Share this post


Link to post
Share on other sites

What you may want to do is write a action story of just what you have in mind. If I'm following your story right:

1. Script loads

2. Script idles around looking for winword.exe to exist.

3. Winword.exe is found to exist.

4. Retrieve COM object for winword process

5. You have _WordQuit which removes the object reference. You sure you want to do this here?

6. ControlSend to a specific control in the word window. Could one of the _Word commands work here instead?

7. Save as - You could use _WOrdDocSave here.

8. Once saved, then what? Do you want to close it? If so, then use _WordQuit here.

Ideally when the script starts there is say a 50/50 chance that word will be running. If it is, it will be one of these scenarios. Word is running with a new document that isn't saved, word is running and it's just a blank unsaved document, or an existing document is open and changes have or haven't been made. So far I think what I have covers what I want to have happen? Seems to work ok during testing at least.

Honestly with #5 I used _WordQuit because it performed what I needed it to oppose to using _WordDocSave when I was testing some commands out. I am still not entirely sure how to use the _WordDocSave command without actually opening a Word document first. All the examples I was able to find tied into opening the document first then doing a save or save / close based on that original document that was opened. All I will ever need to do with this script is save documents that are already open or already exist and have been changed. I shouldn't ever need to actually open anything.

_WordQuit worked since it still prompts to save if the document already exists and has been changed or if it's a new document that hasn't yet been saved. If it's blank or an existing document that hasn't changed it just closes out which works for me. Is there parts of this that can be eliminated? I didn't look too closely at this since it was working for the most part. Any suggestions for cleaning things up a bit would be greatly appreciated.

I am just starting into the Excel section and I've noticed that a lot of the Excel commands are different from Word so that's my next task is figuring out how to do the same thing with a different MS Office Program. This will all eventually tie into one large script. Just taking it one step at a time rather than trying to debug one huge script. Easier for me to debug pieces and then tie them in to the main script.

Share this post


Link to post
Share on other sites

You may want to do an action story on your entire project. It makes things much easier in the long run for it looks like what you have in mind is much more complicated than you have posted.

A second suggestion is to try the examples for the word commands. That will show you how it works.

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