ComObjActive is not working as expected...

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
errorseven
Posts: 5
Joined: 06 Aug 2015, 09:57

ComObjActive is not working as expected...

09 Aug 2015, 13:55

I don't use COM often in office. But when I have, every instance used a new object like

Code: Select all

oWord := ComObjectCreate("Word.Application")
This works great, every time.

However, recently I was trying to tackle an issue with grabbing data from an already open and running Word document and so I looked up examples which pointed me

Code: Select all

oWord := ComObjActive("Word.Application")
but it throws and exception every time.

Further reading led me to other threads discussing this very issue in which it was pointed out that the syntax was wrong, however he still had trouble to get it work properly.
- Yes the document is open
- Yes I tried Minimizing and Maximizing.

I've tried this both Office 2010 and 2013.

Error Message:
Error: 0x800401E3 - Operation unavailable
Further googling error message led me to something called Com Interop Assemblies and formatting Com commands like

Code: Select all

oWord := ComObjActive("Microsoft.Office.Application.Word.Application")
Just an example, none of methods I've tried have worked.

What's happening here?

How can this be fixed?

Is it something to do with the state of Ahk's COM implementation?
gilliduck
Posts: 265
Joined: 06 Oct 2014, 15:01

Re: ComObjActive is not working as expected...

09 Aug 2015, 14:07

When I activate an active Outlook to send email/create calender item/create task/etc all I do is

Code: Select all

Application := ComObjActive("Outlook.Application")
olMailItem := 0
MailItem := Application.CreateItem(OlMailItem)
MailItem.To := "person@person.com"
etc etc
Is it throwing the error right at the ComObjActive line?
errorseven
Posts: 5
Joined: 06 Aug 2015, 09:57

Re: ComObjActive is not working as expected...

09 Aug 2015, 14:23

Yes it fails at ComObjActive, just opened up Outlook and ran your code. Same error.
gilliduck
Posts: 265
Joined: 06 Oct 2014, 15:01

Re: ComObjActive is not working as expected...

09 Aug 2015, 15:57

Can you post your code? At least from the 1st line to right after the ComObjActive?
errorseven
Posts: 5
Joined: 06 Aug 2015, 09:57

Re: ComObjActive is not working as expected...

09 Aug 2015, 16:52

Well there's not much as I hadn't gotten very far... But I've tried variations of below:

Code: Select all

F1::
ActiveDocumentFile := ComObjActive("Word.Application").ActiveDocument.FullName
MsgBox % ActiveDocumentFile
Return
gilliduck
Posts: 265
Joined: 06 Oct 2014, 15:01

Re: ComObjActive is not working as expected...

09 Aug 2015, 16:56

That script works perfectly for me. No changes, I just copy/pasted, ran it and boom, it returned a file name of my currently open Word document. The only time it failed was when Word was not open. Word 2013 fyi.
errorseven
Posts: 5
Joined: 06 Aug 2015, 09:57

Re: ComObjActive is not working as expected...

09 Aug 2015, 17:00

Yup I've Downloaded and installed the latest version AutoHotkey 1.1.22.03 - I'm now wondering if the problem lies with my Os or even Office install... I'm on Win 8.1 Currently testing in Office 2010. I guess I'll have to do some digging.. maybe there's an answer out there...
gilliduck
Posts: 265
Joined: 06 Oct 2014, 15:01

Re: ComObjActive is not working as expected...

09 Aug 2015, 17:10

Well I'm Win 7 with Office 2013. But I don't think that that is the issue. Not sure what the issue is though.... I don't have enough of an understanding of COM to know if there is a way for Office to not be properly registering. Try to repair your install of Office? Or Uninstall/Reinstall to make sure it's proper? Dunno, just spitballing at this point.
errorseven
Posts: 5
Joined: 06 Aug 2015, 09:57

Re: ComObjActive is not working as expected...

09 Aug 2015, 17:29

Thank you gilliduck for testing the code and confirming it worked.

A few months back I uninstalled Office 2010 and installed a Trial of Office 2013 while trying to help a friend out with some scripting for work stuff and wanted to confirm it was compatible with his version. Trial time is up abd didn't notice as I don't use Office for anything really, but a few weeks ago I did notice and I again uninstalled 2013 and reinstalled 2010. Some more time goes by and still I haven't noticed any issues and ComObjectCreate still works... but I think I may have found the issue... https://support.microsoft.com/en-us/kb/3025036

Something got screwed up between the 2 installations...

Can you test the code pasted on StackOverFlow and let me know it works?

http://stackoverflow.com/questions/3189 ... 6#31897856
gilliduck
Posts: 265
Joined: 06 Oct 2014, 15:01

Re: ComObjActive is not working as expected...

09 Aug 2015, 19:01

Well it doesn't work, but let me look through the code and see why. I'll post back in a bit after reviewing it.
gilliduck
Posts: 265
Joined: 06 Oct 2014, 15:01

Re: ComObjActive is not working as expected...

09 Aug 2015, 19:03

What is this supposed to do exactly? Get the full path of the open word document then open the file folder?
geek
Posts: 1055
Joined: 02 Oct 2013, 22:13
Location: GeekDude
Contact:

Re: ComObjActive is not working as expected...

09 Aug 2015, 19:07

Have you tried running your script as administrator? Sounds to me like this could be a permissions issue.
gilliduck
Posts: 265
Joined: 06 Oct 2014, 15:01

Re: ComObjActive is not working as expected...

09 Aug 2015, 19:13

I modified the first section to be easier. SplitPath does what we need if I'm understanding the purpose of this.

Code: Select all

F1::
	oWord := ComObjActive("Word.Application").ActiveDocument.FullName
	SplitPath, oWord, FileName, FolderPath
	RunWait, Explorer
    sleep 100
    WinActivate, ahk_class CabinetWClass
    WinWaitActive, ahk_class CabinetWClass
	sleep 100
	Explorer_NavSelect(FolderPath, File)
	Return


Explorer_NavSelect(Path, FileName,  hwnd="") {  
    hwnd := (hwnd="") ? WinExist("A") : hwnd 
    WinGet, ProcessName, ProcessName, % "ahk_id " hwnd
    if (ProcessName != "explorer.exe")  
        return
    For pExp in ComObjCreate("Shell.Application").Windows
    {
        if (pExp.hwnd = hwnd) { ; matching window found
            pExp.Navigate("file:///" Path)
            sleep 100
            pExp.Document.SelectItem(FileName, 1 8 16)
            Return
        }
    }
}
Try that and see if it opens the folder location for you. It seems to work fine for me. Not sure what they were doing with that loop, obviously not the way I would have done it (doesn't mean it's not a better way though, it just errored out on me).
lexikos
Posts: 9690
Joined: 30 Sep 2013, 04:07
Contact:

Re: ComObjActive is not working as expected...

11 Aug 2015, 01:01

What does GetActiveObjects example #1 return when you have an Office program open?
shixuguo
Posts: 4
Joined: 25 Nov 2016, 23:38

Re: ComObjActive is not working as expected...

25 Nov 2016, 23:42

have you solved the problem?
I have the same problem at win 10 and office 2016
User avatar
FanaticGuru
Posts: 1908
Joined: 30 Sep 2013, 22:25

Re: ComObjActive is not working as expected...

26 Nov 2016, 01:44

I have encountered this error before but the details are vague in my memory.

I believe in my case it was caused when I opened an old file in a new version of Word and the file was opened in compatibility mode. I got around the problem by saving the file in the new Word file format.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
shixuguo
Posts: 4
Joined: 25 Nov 2016, 23:38

Re: ComObjActive is not working as expected...

26 Nov 2016, 01:56

FanaticGuru wrote:I have encountered this error before but the details are vague in my memory.

I believe in my case it was caused when I opened an old file in a new version of Word and the file was opened in compatibility mode. I got around the problem by saving the file in the new Word file format.

FG
I do not have your case

So what's your environment about os and office version?
User avatar
Xeo786
Posts: 760
Joined: 09 Nov 2015, 02:43
Location: Karachi, Pakistan

Re: ComObjActive is not working as expected...

26 Nov 2016, 07:08

gilliduck wrote:That script works perfectly for me. No changes, I just copy/pasted, ran it and boom, it returned a file name of my currently open Word document. The only time it failed was when Word was not open. Word 2013 fyi.
ComObjActive is supposed to work not only with open word document, that documents has also need to be active . if you use ComObjectCreate just after use visible so that document will be treated as active document, you can also used ComObjGet.

Code: Select all

wordfile := "d:\SomeWhere\filename.docx"
Oword := ComObjGet(wordfile)
"When there is no gravity, there is absolute vacuum and light travel with no time" -Game changer theory
shixuguo
Posts: 4
Joined: 25 Nov 2016, 23:38

Re: ComObjActive is not working as expected...

26 Nov 2016, 07:58

Xeo786 wrote:
gilliduck wrote:That script works perfectly for me. No changes, I just copy/pasted, ran it and boom, it returned a file name of my currently open Word document. The only time it failed was when Word was not open. Word 2013 fyi.
ComObjActive is supposed to work not only with open word document, that documents has also need to be active . if you use ComObjectCreate just after use visible so that document will be treated as active document, you can also used ComObjGet.

Code: Select all

wordfile := "d:\SomeWhere\filename.docx"
Oword := ComObjGet(wordfile)

how to understand "active"?

if I open an untitled ppt ,is it active?
a visible window is active?
lexikos
Posts: 9690
Joined: 30 Sep 2013, 04:07
Contact:

Re: ComObjActive is not working as expected...

26 Nov 2016, 23:29

Several users have mentioned "ComObjectCreate" in this thread. I think it is important to note that you are using the wrong function name.

It is important because if you use this name in your actual script, you will not get an error message. It will just call ComObjActive.
In current versions, any function-call beginning with "ComObj" that does not match one of the other COM functions actually calls ComObjActive.
Source: ComObjActive()

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: apeironn, Bing [Bot], Draken, Google [Bot], Spawnova and 94 guests