|
-
Aug 19th, 2011, 12:02 PM
#1
Thread Starter
Member
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.
-
Aug 19th, 2011, 12:30 PM
#2
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)
-
Aug 19th, 2011, 12:48 PM
#3
Thread Starter
Member
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
-
Aug 19th, 2011, 12:52 PM
#4
Re: Create Excel Application dynamically in vb6
 Originally Posted by cycleinmars
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?
-
Aug 19th, 2011, 12:57 PM
#5
Thread Starter
Member
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
-
Aug 19th, 2011, 01:01 PM
#6
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
-
Aug 19th, 2011, 01:03 PM
#7
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"?
-
Aug 19th, 2011, 01:16 PM
#8
Thread Starter
Member
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
-
Aug 19th, 2011, 01:20 PM
#9
Thread Starter
Member
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
-
Aug 19th, 2011, 01:20 PM
#10
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|