Results 1 to 8 of 8
  1. #1
    Join Date
    Oct 2007
    Posts
    214

    Question Unanswered: Command button question

    Hi All,

    I want to make a command button in my access databse form open a specific excel document by clicking the button. What code would I need to enter for the command button to open a specific excel file. Basically, I want the button to automatically open up an employee excel file so that I don't have to find it in my folers on my desktop. I just want to be in access and have the button automatically open the file. Thanks!!

  2. #2
    Join Date
    Aug 2006
    Posts
    559
    Quote Originally Posted by benz1984
    Hi All,

    I want to make a command button in my access databse form open a specific excel document by clicking the button. What code would I need to enter for the command button to open a specific excel file. Basically, I want the button to automatically open up an employee excel file so that I don't have to find it in my folers on my desktop. I just want to be in access and have the button automatically open the file. Thanks!!

    Try this code, I found it online. Just replace the "C:\Test.xls" with the location of the MS Excel document you want to open. HOWEVER, you'll ONLY BE ABLE TO OPEN THAT SINGLE EXCEL FILE.

    Private Sub Command37_Click()

    Dim oApp As Object

    Set oApp = CreateObject("Excel.Application")
    oApp.Workbooks.Open ("C:\Test.xls")
    oApp.Visible = True
    oApp.UserControl = True

    Exit_Command37_Click:
    Exit Sub

  3. #3
    Join Date
    Aug 2008
    Posts
    58
    Make sure you have the correct references enabled. Otherwise you will get a "User defined type not defined" error message. In the Visual Basic menu, go to Tools and then click on References. Make sure the box next to "Microsoft Excel 12.0 Library" is checked. Best of luck,

    J

    Edit: You may also want to include some code that will prevent another instance of Excel from opening if the user already has Excel open.

  4. #4
    Join Date
    Dec 2004
    Location
    Madison, WI
    Posts
    3,926
    or you can also just use the shell command...

    Call Shell("MyFilePathAndName",1)
    or
    Call Shell("Excel.exe " & MyPathAndFileName, 1)

    (but probably not the ideal route to go.)
    Last edited by pkstormy; 08-28-08 at 21:03.
    Expert Database Programming
    MSAccess since 1.0, SQL Server since 6.5, Visual Basic (5.0, 6.0)

  5. #5
    Join Date
    Nov 2007
    Location
    Adelaide, South Australia
    Posts
    4,049
    I use an API for opening files personally.
    Owner and Manager of
    CypherBYTE, Microsoft Access Development Specialists.
    Microsoft Access MCP.
    And all around nice guy!


    "Heck it's something understood by accountants ... so it can't be 'that' difficult..." -- Healdem
    "...teach a man to code and he'll be frustrated for life! " -- georgev

  6. #6
    Join Date
    Oct 2007
    Posts
    214
    Thanks all! The code worked great!

  7. #7
    Join Date
    Dec 2004
    Location
    Madison, WI
    Posts
    3,926
    Out of curiosity, which code worked?
    Expert Database Programming
    MSAccess since 1.0, SQL Server since 6.5, Visual Basic (5.0, 6.0)

  8. #8
    Join Date
    Oct 2007
    Posts
    214
    Hi,

    This is the code that worked the best.

    Private Sub Command37_Click()

    Dim oApp As Object

    Set oApp = CreateObject("Excel.Application")
    oApp.Workbooks.Open ("C:\Test.xls")
    oApp.Visible = True
    oApp.UserControl = True

    Exit_Command37_Click:
    Exit Sub

Posting Permissions

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