+ Reply to Thread
Results 1 to 3 of 3

Thread: Opening Excel Application before Opening Workbook

  1. #1
    Forum Contributor
    Join Date
    09-23-2007
    Posts
    109

    Opening Excel Application before Opening Workbook

    I'm trying to understand something.
    I've found the following code on the web. Have cleaned it up to show the relevant parts.

    Why do we Create Excel.Application before opening the excel file with data.
    It's like opening a box and placing a smaller box inside it? Is the first step (Creating the Excel.Application) necessary? This part just gives me a frame. I'm doing this from Access which is why I've placed it in the Access forum.


    P
    rivate Sub cmdLoad_Click()
    Dim excel_app As Object
    Dim excel_sheet As Object
    Dim conn As ADODB.Connection
    
    
        ' Create the Excel application ----------WHY DO WE DO THIS?
        Set excel_app = CreateObject("Excel.Application")
    
        ' Uncomment this line to make Excel visible.
         excel_app.Visible = True
    
        ' Open the Excel spreadsheet.---------------OPENING THE EXCEL FILE WITH DATA
        excel_app.Workbooks.Open FileName:=txtExcelFile.Text

    Thanks so much.

    Danny2000

  2. #2
    Forum Guru davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2007
    Posts
    1,879

    Re: Opening Excel Application before Opening Workbook

    This kind of goes into the concept of object oriented programming. When you want to use an object, you have to first create an instance of that object.

    It may help to think about it as if we were not programming, but rather doing it manually. If you want to open an Excel file, Excel must first be open. Even when you open the file from Windows Explorer, first Excel starts, then Excel loads the file.

    You code above does the same thing. First, it starts an instance of Excel, then it opens a file.
    Is your code running too slowly?
    Does your workbook or database have a bunch of duplicate pieces of data?
    Have a look at this article to learn the best ways to set up your projects.
    It will save both time and effort in the long run!


    Dave

  3. #3
    Forum Contributor
    Join Date
    09-23-2007
    Posts
    109

    Re: Opening Excel Application before Opening Workbook

    Thanks very much. Now that I look at it I believe you are 100% correct. Thanks Again.
    Danny2000

+ 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.2.0