How do I get the name of the current document Excel?

I am trying to get the path and name of the current document in Excel. I have tried various means, but it keeps giving me the name of the earliest opened document

Here’s my latest attempt which I was sure would work…


		set this_path to path of front document
		set this_file to name of front document

It doesn’t work in AppleWorks either - and it generates an error saying it can’t get the path of Document 1. MS Word also gives that errorr

So AppleScript is translating this Document 1, which Excel treats as the earliest opened document.

So how do I get Excel to know that I mean the document which currently has focus?

And how do I get this to work at all in Appleworks and MS Word?

thankyou

That’s pretty annoying. But you can work around it with System Events.


[This script was automatically tagged for color coded syntax by Convert Script to Markup Code]

Thanks jakacmar

that works… but not in Word!

Strangely, Word does not support the Path property for documents (or any kind of file) so it doesn’t look like its’ going to be possible with Word. It doesn’t look like PowerPoint does either. However, Office X does support VB Script for Applications which may be able to get this info for you. Unfortunately, I don’t really know anything about VB Script. Maybe someone at the Microsoft forums (uggghh)?

This works for me (this assumes the document has already been saved):

Jon


[This script was automatically tagged for color coded syntax by Convert Script to Markup Code]

I guess I just assumed “name” would return the name. Oh well, glad someone found it. Then you can just parse the path to get the actual name if you need it.

thanks Jon, that works for both Word and Excel, but how do I get it to be generic AND use the document that has focus (using “front” doesn’t work in Office)? I don’t want to specify the application name.

thanks

I don’t quite understand. If you’re trying to make a script that generically gets the path to the document in the frontmost app no matter what that app is, well, that’s going to be a lot of work and will be a Sisyphean effort. To just get it from Excel or Word, you can use this code but running it from Script editor will not work because it will always be the frontmost app unless you add some sort of delay at the beginning, run the script, then switch to the appropriate app:

Jon


[This script was automatically tagged for color coded syntax by Convert Script to Markup Code]

Jon, that didn’t seem to work… it seems to return the front_app as “System Events”

I want to link an open (and saved) file into DevonThink. I thought it would be as simple as:
Get full file name (inc path)
link to it in DT

Here’s the code I expected to work…


[This script was automatically tagged for color coded syntax by Convert Script to Markup Code]

The problems are:

  1. Office sees front document as the earliest open one. This was fixed by Jak’s first script, but it specified the app.
  2. My script above doesn’t work in all apps. This was rectified by your script
tell application "Microsoft Word" to set doc_path to name of document 1

but again, is app dependent and specifies doc 1.

But I don’t seem to be able to combine it all into one. Also, with the script jak provided which returns the full path and file (which is perfect), I’f have to change the If clause.

thanks for your help

PS I don’t work for DT. This is just a feature that is missing which can be achieved (hopefully!) with scripting.

ok!! am getting somewhere!

Developed this code which sort of works…


[This script was automatically tagged for color coded syntax by Convert Script to Markup Code]

Now, in Word or Excel I have three documents open.
When I am in Document 3, it returns window no. 3 and the name of document 3
When I am in Document 2, it returns window no. 2 and the name of document 2
when I am in document 1, it returns window no. 1 and the name of document 3, which is the earliest opened document!!!

am i allowed to swear??

I will try to fix this with a loop…

realised I didn’t need a loop just had to get the list of open documents and select it from the list.
thus…


	tell application appname to set alldocs to documents
	display dialog "ALLDOCS: " & alldocs
	tell application appname to set the_doc to name of item winno of alldocs
	display dialog "THE_DOC: " & the_doc

And works in Word!! But not Excel which doesn’t recognize the all documents command

it also failed in TextEdit

There must be some generic way for getting this info!! (ie. the name and path of the current doc in the current app)