 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
Greener than NEWB Guest
|
Posted: Wed Mar 17, 2010 7:34 pm Post subject: unattended COM Help |
|
|
Hello All,
I'm trying to automate the formatting of some Word Documents via a scheduled script.
Please note that the workstation would be in logoff state.
This code works when the workstation is logged on, but it hangs when the script is scheduled. After I login to check Processes, there is a WINWORD.exe application that is hung.
Here is the snippet:
| Code: |
MacroName = bsu ; name of the existing Macro
COM_Init()
Word := COM_CreateObject("Word.Application")
COM_Invoke(Word,"Visible=",False)
COM_Invoke(Word, "Run", "!"MacroName)
COM_Release("!"MacroName)
COM_Release("Word")
COM_Term()
|
|
|
| Back to top |
|
 |
answer4u Guest
|
Posted: Wed Mar 17, 2010 8:21 pm Post subject: |
|
|
First off: | Code: | MacroName = bsu ; name of the existing Macro
COM_Init()
Word := COM_CreateObject("Word.Application") ; did you mean to use COM_GetActiveObject ?
COM_Invoke(Word,"Visible=",False)
; are you accessing any WORD document?!
COM_Invoke(Word, "Run", "!" MacroName) ; should need a space after "!"
; COM_Release("!" MacroName) ; no need to release here
COM_Release(Word) ; don't use quotes
COM_Term() |
Secondly, I belive you workstation would need to be logged on to run AHK. |
|
| Back to top |
|
 |
tank
Joined: 21 Dec 2007 Posts: 3700 Location: Louisville KY USA
|
Posted: Wed Mar 17, 2010 9:35 pm Post subject: |
|
|
I beleive your code is correct except for these corrections
| Code: | COM_Invoke(Word, "Run", "!" MacroName) ; should need a space after "!"
; COM_Release("!" MacroName) ; no need to release here |
the actual problem is that word requires you to have a desktop logged into
I am afraid there is no solution to this your going to have to have a user logged in ... this is not to say you cant have it run with windows locked
| Code: | | ; are you accessing any WORD document?! | I dont automate word often there just isnt any point but the run command would be done at the application level i think.
Does the macro run fine if your logged in? _________________
We are troubled on every side‚ yet not distressed; we are perplexed‚
but not in despair; Persecuted‚ but not forsaken; cast down‚ but not destroyed; |
|
| Back to top |
|
 |
answer4u Guest
|
Posted: Thu Mar 18, 2010 4:42 am Post subject: |
|
|
| tank wrote: | | ... the run command would be done at the application level i think | You're right - I was thinking the macros were stored in the word document. |
|
| Back to top |
|
 |
Greener than NEWB Guest
|
Posted: Thu Mar 18, 2010 2:39 pm Post subject: |
|
|
I just thought that since CDO COM worked while workstation was logged off that Word would work also.
The macro opens up the file, formats it, and then saves it as a document.
And yes the snippet works when a user is logged in. I will go the route of having a locked workstation. |
|
| Back to top |
|
 |
OceanMachine
Joined: 15 Oct 2007 Posts: 780 Location: England
|
Posted: Thu Mar 18, 2010 7:48 pm Post subject: |
|
|
I think it is because you are trying to run an actual VBA macro that is the problem.
I was however able to get the same functionality as a macro directly from AHK using COM, and this ran even when I was logged off.
Try the following example (requires COM Standard Library): | Code: | FileName := "c:\test.doc"
TestText := "test text 123"
COM_Init()
oWord := COM_CreateObject("Word.Application") ; create word object
COM_Invoke(oWord, "Visible=", False) ; make sure it's not visible
COM_Invoke(oWord, "Documents.Add") ; add a document
COM_Invoke(oWord, "Selection.TypeText", TestText) ; add some text
COM_Invoke(oWord, "ActiveDocument.SaveAs", FileName, 0) ; save the document
COM_Invoke(oWord, "Quit") ; close word
COM_Release(oWord) ; tidy up
COM_Term() |
This should create a test file at c:\test.doc with some text in it if you trigger it via Scheduled Tasks, even if you are logged off (worked for me anyway).
See the below link for more info on how to automate Word (and other office apps) - see if you can get the functionality of your macros ported directly over to AHK and this should then mean it (hopefully) works: MS Office Automation Functions (via COM) [thanks Sean] |
|
| Back to top |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|