1
votes

It’s often requires to quick locate the folder location of open active file and highlight or select the active file while working on different software applications. Quick locating the residing folder needed for finding related files in same folder, rename of the opened files or residing folder or move the file to different related folder. Current options require navigating through the loads of folders to find and locate the specific folder where it’s buried with bunch of similar other files (similar to find needle in a haystack). Microsoft Office suite has built-in feature named “document location” which can be added to quick access toolbar. But it only allow to see the folder location or full path but no single click command or key available (AFAIK) to conveniently jump to that locating folder and highlight/identified the opened file so that further operation (e.g. rename, move) could be done on that specific file/folder. This is also the case for other software applications where native program have options to get full path but no way to jump to the specific file/folder. Considering one of Microsoft Office suites application (e.g. word) as test cases the processes I could imagine as follows;

  • 1 Get the full path (D:\Folder\Subfolder\Mywordfile.docx) of currently opened word document
  • 2 Close the file
  • 3 Explorer command to select and highlight the file in folder given full path (process 1)
  • Operation on file/folder as desire manually and double click to return to file operating applications (e.g. word).

In my assessment for Implementation of above tasks following are possibilities

  • Task 1 Microsoft Word has a built-in function called "document location" to get the full path of the opened document and its currently possible to copy the file path in the clipboard.
  • Task 2 Close the file (Ctrl+W or Ctrl+F4)
  • Task 3 AHK code for Explorer command to select the file for a given full path (available in Task 1)

I am facing difficulties in Task 3 and I tried each of these but so far no luck

Clipboard := “fullpath” ; Full path (D:\Folder\Subfolder\Mywordfile.docx ) copied from Word 

Run explorer /e, “Clipboard” 

Run %COMSPEC% /c explorer.exe /select`, "%clipboard%"

So far above explorer command only take me to my documents folder not in the specific folder location (path in Task 1). I am curious know what would be the right explorer code to select the file for a given full path in clipboard. Appreciate for supporting AHK code or better way to do this task. Thank in advance.

3
Wow, this is really well written question. I'm upvoting it now but will also start to work on it.Ro Yo Mi
@RoYoMi I saw that Rhinemine removed the accept from your answer. I noted this, I upvoted your answer because you did gave the AHK solution to her/her. I believe they should have not retracted the accept. I got them to ask a new question on the VBA to solve it.ib11
I appreciate that, thank you. Rhinemine is a new Stack Overflow user and is still learning the ropes here.Ro Yo Mi
Thanks @Ro Yo Mi and ib11 My apology if I vote in wrong way since I am in learning curve of stack and so not familiar with practices. Personally I believe in respect, give credit where its due and contributing to other when it possible. I choose this code as way of locating folder/file where I previously faced road block of how to execute it. This is nice code to locate file for given path but it also has room for enhancement to demonstrate its robustness in different applications. So far seem to working fine for word but sometime does not do well for excel. I will try more and posted here.Rhinemine
No problems, you are learning fast.ib11

3 Answers

2
votes

I'm not clear on why your sample code doesn't work. I suspect it's because of the extra characters.

After running this command Windows Explorer will be open and have the desired file selected (if it exists).

FullPathFilename := "e:\temp\test.csv"
Explorer := "explorer /select," . FullPathFilename
Run, %Explorer%
2
votes

I don't know if you tried the other approach, but I think this is simpler and shorter:

  • 1) Store the full path of the document in a string: oldfile = ActiveDocument.FullName

  • 2) SaveAs the document with ActiveDocument.SaveAs

  • 3) Delete the old file with Kill oldfile

All this is from VBA directly, no need to use Explorer shell. The same exists for the other applications.

Here is a fully working code for the Word Documents:

Sub RenameActiveDoc()

    Dim oldfile As String

    Set myDoc = ActiveDocument

    '1) store current file
    oldfile = myDoc.FullName

    '2) save as the active document (prompt user for file name)
    myDoc.SaveAs FileName:=InputBox("Enter new name", "Rename current document", myDoc.Name)

    '3) Delete the old file with
    On Error GoTo FileLocked
    Kill oldfile
    On Error GoTo 0

    Exit Sub

FileLocked:
    MsgBox "Could not delete " & oldfile, vbInformation + vbOKOnly, "File is locked"

End Sub
0
votes

With contribution of Ro Yo Mi I am able to come up with following solution. However I am assuming that there might better solution to this task.

 ;;; Customize Document Location (Choose form All Commands) in Quick Access Toolbar and get its position (#4 for my case)
#If WinActive("ahk_class OpusApp") || WinActive("ahk_class XLMAIN") || WinActive("PPTFrameClass")
#p:: ;Close Word/Excel/PowerPoint Document and Locate in Explorer Folder Location
clipboard = ;empty the clipboard
Send !4 ; Select full path while document location at #4 position in Quick Access toolbar
Send ^c ; copy the full path
ClipWait ; waits for the clipboard to have content
Send {esc}
Send, ^{f4} ; Close opened document only but keep Word/Excel/PPT program running
Explorer := "explorer /select," . clipboard
Run, %Explorer%\
return