|
-
May 30th, 2007, 02:33 PM
#1
Thread Starter
New Member
[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!
-
May 30th, 2007, 08:14 PM
#2
Re: [2005] How can I get the full path of an Excel file with the window handle?
System.IO.Path.GetFullPath("myworkbook.xls")
-
May 30th, 2007, 10:04 PM
#3
Thread Starter
New Member
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!
-
May 31st, 2007, 05:40 AM
#4
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.
-
Jun 2nd, 2007, 07:37 AM
#5
Thread Starter
New Member
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...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|