Results 1 to 10 of 10

Thread: Create Excel Application dynamically in vb6

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2010
    Posts
    32

    Create Excel Application dynamically in vb6

    hello guys ,
    Just found the way to create a excel application using vb6 .
    Code:
    Dim AppXls As Excel.Application
    Dim ObjWb As Excel.Workbook
    Dim objws As Excel.Worksheet
    
    Set AppXls = CreateObject("Excel.Application")
    Set ObjWb = AppXls.Workbooks.Add
    Dim a As String
    
    a = "newexel.xls"
    ObjWb.SaveAs (App.Path & "\" & a & "")
    ObjWb.Close (SaveChanges = True)
    well this is for starters like me.

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Create Excel Application dynamically in vb6

    That's a reasonable start, but there are various things that aren't quite ideal, and of course several things missing (such as working with the sheet).

    For info on the way things should be, and other things you can do, see my Excel automation tutorial (link in my signature).


    By the way, you should generally not be saving files within App.Path , for information see the article Where should I store the files that my program uses/creates? from our Classic VB FAQs (in the FAQ forum)

  3. #3

    Thread Starter
    Member
    Join Date
    Feb 2010
    Posts
    32

    Re: Create Excel Application dynamically in vb6

    Thanks for your reply


    I am not able to create a worksheet in the same file , can you please help,
    let the code be simple,

    Regards
    praveen

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Create Excel Application dynamically in vb6

    Quote Originally Posted by cycleinmars View Post
    I am not able to create a worksheet in the same file , can you please help, let the code be simple,
    What happens when you try? Do you get an error? What code are you using?

  5. #5

    Thread Starter
    Member
    Join Date
    Feb 2010
    Posts
    32

    Re: Create Excel Application dynamically in vb6

    Hai,

    when i use the following code

    Code:
    Set objws = ObjWb.Worksheets.Add
        objws.Name = "new"
        Set objws = Nothing
    when i open the excel file i do not see any changes / any sheets added in the workbook


    no error is displayed

    regards

    praveen

  6. #6
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Create Excel Application dynamically in vb6

    Try something like
    Code:
    ObjWB.Sheets.Add Before:=ObjWB.Sheets("Sheet2") 'if you do not care where the new sheet gets
    ObjWB.ActiveSheet.Name = "VBForums"             'added then you can ignore the Before part 
                                                    'of the code

  7. #7
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Create Excel Application dynamically in vb6

    It works perfectly for me, here is the entire code I used (no references needed):
    Code:
    Private Sub Form_Load()
    
    Dim AppXls As Object
    Dim ObjWb As Object
    Dim objws As Object
    
        Set AppXls = CreateObject("Excel.Application")
        Set ObjWb = AppXls.Workbooks.Add
    
        Set objws = ObjWb.Worksheets.Add
        objws.Name = "new"
        objws.cells(1, 1) = "hello"
        Set objws = Nothing
    
        Set ObjWb = Nothing
        AppXls.Visible = True
        Set AppXls = Nothing
    
    End Sub
    If you are still doing the .SaveAs , are you sure that the file doesn't exist when you run it?

    Are you perhaps using "On Error Resume Next"?

  8. #8

    Thread Starter
    Member
    Join Date
    Feb 2010
    Posts
    32

    Re: Create Excel Application dynamically in vb6

    Hai,

    thanks for your reply , i fixed the same,

    i was saving the file before it created the worksheet
    now i just put
    objwb.saveas after creation of the worksheet, so it works perfect for me


    however why is this code required
    Code:
    ObjWb.Close (SaveChanges = True)
    what does savechanges = true does, is it useful?

    regards

    Praveen

  9. #9

    Thread Starter
    Member
    Join Date
    Feb 2010
    Posts
    32

    Re: Create Excel Application dynamically in vb6

    Hai,

    I have created a worksheet "new" ,
    but i also want to create "new1" sheet if new is present
    how will i check whether the same worksheet is there or not in the workbook

    regards

    Praveen

  10. #10
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Create Excel Application dynamically in vb6

    The way you have the code, it doesn't do what you think... it is using a variable you have created (presumably accidentally because you aren't using Option Explicit).

    The way it should be is:
    Code:
    ObjWb.Close SaveChanges := True
    I'm fairly sure my tutorial explains why you would use it.

Tags for this Thread

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