+ Reply to Thread
Results 1 to 11 of 11

Opening an excel file from a cell reference

  1. #1
    Registered User
    Join Date
    05-03-2006
    Posts
    37

    Opening an excel file from a cell reference

    Hiya,

    I want to use a macro to open an existing file on my c drive where the reference to that file is contained in a cell.

    E.g. In my sheet, the cell A1 contains the following text "C:\Documents and Settings\Test". Therefore the file I want to open using a macro will be Test.xls.

    However the file I wish to open (and therefore the reference text in cell A1) could change (and it could also change to be a word document) so it needs to be able to adapt when a new valid file (and document type) reference is entered into the cell.

    Help!

    Also, as an aside, when I want to change the file referred to in A1, the easiest way I've found is to find the file I want in windows explorer, go to properties and copy the file reference and then paste it into A1. However the reference (from the properties tab) seems to miss the ".xls" or ".doc" at the end (see above). Is this an issue? If yes, then I'm happy to manually type the .xls or .doc extension at the end.

    Is there an easier way to do this?

    Thanks a bunch

    Henry
    Last edited by VBA Noob; 11-14-2008 at 05:43 PM.

  2. #2
    Registered User
    Join Date
    05-03-2006
    Posts
    37
    Note to self... always check previous posts before asking a question. Here's the code...

    PHP Code: 
        Dim myFileName As String
        
        myFileName 
    Range("A1").Text
        Workbooks
    .Open Filename:=myFileName 
    However I get an error if the file name doesn't exist! Can anyone help me with some code that will give a simple message box stating the file name doesn't exist?

    Many thanks

    Henry

  3. #3
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678
    Please Login or Register  to view this content.
    Entia non sunt multiplicanda sine necessitate

  4. #4
    Registered User
    Join Date
    05-03-2006
    Posts
    37
    Thank you so much for your reply. But I noticed that it didn't work if A1 contained an address to a file that didn't exist. I think I've managed to work around it by doing this...

    Please Login or Register  to view this content.
    HOWEVER, this only works if the target file is an Excel document. Unfortunately the target files could be either in Excel, Word or even (possibly) Powerpoint. Any ideas how to code it to open any file type? Or am I dreaming?

    Thank you for your help.

    Kind regards

    Henry

  5. #5
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678
    But I noticed that it didn't work if A1 contained an address to a file that didn't exist.
    Just put Exit Sub after the MsgBox. Then point was to illustrate how to know if a file exists.

    The only file types you can open, in the sense of seeing them in Excel, are Excel files, text, csv, ... . You can open any type of file as binary or a textstream object, but I doubt that's what you want to do. (Your title does say open an Excel file.)

    If you want to open Word and PPT files in their native applications from VBA, you would have to create the correct application, and then open the file in the application, or use Shell.

    What are you trying to do?
    Last edited by shg; 11-14-2008 at 04:21 PM.

  6. #6
    Registered User
    Join Date
    05-03-2006
    Posts
    37
    If you want to open Word and PPT files in their native applications from VBA, you would have to create the correct application, and then open the file in the application, or use Shell.

    What are you trying to do?
    Thank you and you make a valid point... I thought that the file address in cell A1 would always be targeting an Excel file. However it turns out it could be an Excel file or a Word document (forget about Powerpoint).

    I drew up the code to launch a Word document...

    Please Login or Register  to view this content.
    [/CODE]

    ... but I now can't figure out how to make it flexible so that the macro will always launch the right application and then open the file, whether it be an Excel or Word file.

    Does that make sense?

    Regards

    Henry

  7. #7
    Registered User
    Join Date
    05-03-2006
    Posts
    37
    BTW apologies for being a bit vague in my initial posts.

  8. #8
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678
    How do you suppose Windows decides what application to use?

  9. #9
    Forum Expert
    Join Date
    01-12-2007
    Location
    New Jersey
    Posts
    2,127
    I've never had to do anything like this, but maybe an option is to add the extension into cell A1 (ie, C:\Documents and Settings\Test.doc), then query the last three letters of the cell to determine file type. Next use a Select Case statement to determine the correct app.

    Please Login or Register  to view this content.

  10. #10
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678
    BigBas has the idea. I'd add references to Word and PowerPoint, and then use early binding so IntelliSense works:
    Please Login or Register  to view this content.
    Last edited by shg; 11-14-2008 at 07:51 PM.

  11. #11
    Registered User
    Join Date
    05-03-2006
    Posts
    37
    Now that is a stroke of genius! I love the logic. Both sets of code work perfectly. Thank you both so much for your efforts.

    I remain, truly indebted

    Henry

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1