0
votes

I would like have AHK code which can allow open Explorer with the active microsoft word file selected. Word seem to be does not have a command allowing direrctly to get path of currently opened file. However in word select document location allows to display and select the full path of currently opened file. Later I tried following AHK code to select the file in explorer.

#If WinActive("ahk_class OpusApp")
;In word highlight the document location box
#,::
send ^c ; copy the file path from document location
clipwait
run explorer.exe /select, "%clipboard%
return

So far no luck, it just open the explorer, does not select the file as intended.

2

2 Answers

0
votes

I think you're going about this the wrong way. Write a small VBA macro which you can store in your Normal template.

Microsoft Word VBA has ActiveDocument.Path and ActiveDocument.Name which can be concatenated with a \ between to get a full document name including its path.

0
votes

To retrieve the Full File Path and Name of an Open and Active Word Document in AutoHotkey using Component Object Model use the code below:

ActiveFileFullName := ComObjActive("Word.Application").ActiveDocument.FullName

Further reading @ MSDN on FullName

Additionally here is the Code you seek, in AutoHotkey using COM.

Press F1 -> Retrieve the Open Word Document Path and Filename -> Open Explorer -> Navigate to Folder -> Select the File

;Press F1 while MicroSoft Word is open.  
SetTitleMatchMode, 2
#IfWinExist, ahk_class OpusApp 
F1::
        ActiveDocumentFile := ComObjActive("Word.Application").ActiveDocument.FullName
        SplitPath, ActiveDocumentFile, FileName, FolderPath
        RunWait, Explorer
        sleep 100
        WinActivate, ahk_class CabinetWClass
        WinWaitActive, ahk_class CabinetWClass
        sleep 100
        Explorer_NavSelect(FolderPath, FileName)
Return


Explorer_NavSelect(Path, File,  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) ; Navigate to folder
            sleep 100
            pExp.Document.SelectItem(File, 1 8 16) ; Select our File
            Return
        }
    }
}

Read more about SelectItem on MSDN.