[2005] How can I get the full path of an Excel file with the window handle?
Hello,
I am developing this little application in VB 2005, and got stuck trying to get the full path of an open Excel workbook, even though I got its window handle, and the file name (e.g., myworkbook.xls)?
By the way, the Excel file is opened externally by the user, and not by my application. I want to get the full path of this file, and then make a copy to another folder.
Is this possible? Thanks for the help!
Re: [2005] How can I get the full path of an Excel file with the window handle?
System.IO.Path.GetFullPath("myworkbook.xls")
Re: [2005] How can I get the full path of an Excel file with the window handle?
Paul,
Thanks for the reply, however, if I use the System.IO.Path.GetFullPath("myworkbook.xls"), it is returning the path where my application resides. Since I am still running from code, it is returning the ..\...\bin\Debug folder path.
Thanks again!
Re: [2005] How can I get the full path of an Excel file with the window handle?
I could be wrong but I doubt you could do that via the Windows API. I would think that Office Automation would be the way to go. I'm not sure but I would think that you could attach to an existing instance of Excel and then use the object model to determine the path of the open file. The Office Development forum would be the place to ask, then come back here if you need help to implement what you've learned in VB.NET.
Re: [2005] How can I get the full path of an Excel file with the window handle?
Thanks jmcilhinney,
I did find out how to attach my app to an existing Excel process:
'Grab a running instance of Excel.
xlApp = Marshal.GetActiveObject("Excel.Application")
xlcolwbooks = xlApp.Workbooks
'Parse the Workbooks collection and perform the Save and Save As...
For Each xlWBook In xlcolwbooks
xlWBook.Application.DisplayAlerts = False 'Avoid confirmation messages to make the process invisible to the user
xlWBook.Save() 'Save document to current path
sBackupPath = mSettings.sFolder
xlWBook.SaveCopyAs(sBackupPath & "\" & xlWBook.Name)
'Reset confirmation message
xlWBook.Application.DisplayAlerts = True
Next
This was accomplished using the GetActiveObject API.
However, this method will only work for the first instance of Excel that appears on the ROT (Running Object Table).
If there are other processes running aroung, they will not be detected by GetActiveObject.
Thus, my app is doing only part of the job. I wanted to share this insight and perhaps somebody more knowledgeable than me may have the solution for getting all Excel processes.
Thanks again for pointing me to the Automation process...