Results 1 to 5 of 5

Thread: [2005] How can I get the full path of an Excel file with the window handle?

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2007
    Posts
    4

    [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!

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: [2005] How can I get the full path of an Excel file with the window handle?

    System.IO.Path.GetFullPath("myworkbook.xls")

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2007
    Posts
    4

    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!

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    New Member
    Join Date
    May 2007
    Posts
    4

    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
  •  



Click Here to Expand Forum to Full Width