AutoHotkey Community

It is currently May 27th, 2012, 5:45 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: unattended COM Help
PostPosted: March 17th, 2010, 8:34 pm 
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()


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: March 17th, 2010, 9:21 pm 
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.


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: March 17th, 2010, 10:35 pm 
Offline
User avatar

Joined: December 21st, 2007, 3:14 pm
Posts: 3826
Location: Louisville KY USA
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?

_________________
No matter what your oppinion Please join this discussion
Formal request to Polyethene
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 18th, 2010, 5:42 am 
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.


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: March 18th, 2010, 3:39 pm 
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.


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: March 18th, 2010, 8:48 pm 
Offline

Joined: October 15th, 2007, 3:10 pm
Posts: 790
Location: England
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]


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: Apollo, Bing [Bot], Google Feedfetcher, mrhobbeys, rbrtryn and 61 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group