Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Excel + VB.net = Whaaaaa? 1

Status
Not open for further replies.

hayt

IS-IT--Management
Oct 5, 2004
132
US
I have written a dll in vb.net that SHOULD take a dataset as an argument, and place some of the data into an excel file based on values in the datacolumns.

Here is the problem i am having. I have an excel file with the formatting, and labels in the correct cells (like a template). I then run code to insert into the corresponding cells, data that the user needs. But, given that the Object Model for Excel is sooooo weird, I cannot get it to work. I need some help. Here is the main problem i am having currently.

I want to copy the contents of the "template" active worksheet to the new workbook worksheet. If i use the following code:
worksheets(n) = template.activesheet
then i get a reference to the template.activesheet, and all changes occur in the template workbook. I have searched everywhere, and connot find what i need or documentation that is useful. Please Please Please....i will not only name my firstborn after whomever helps me, but i will convince my friends to name their first borns after you as well.
 
The Excel from VB is quit confusing at first but you get use to it. I tested it out and it works if I understood properly what you are trying to achieve:

Code:
Dim xlsApp As Excel.Application
		xlsApp = CType(CreateObject("Excel.Application"), Excel.Application)

		Dim wbNew As Excel.Workbook = xlsApp.Workbooks.Add("Template Full Path & Name")
		Dim shtNew As Excel.Worksheet = wbNew.Worksheets.Item("Sheet to Use from Template")

		'** What ever changes
		'shtNew.Cells(1, 1) = "Updated"

		wbNew.SaveAs("New Name")
		wbNew.Close()
		xlsApp.Quit()

		'** All Excel release

 
thank you for posting....

i actually got it working earlier today, and i don't know why it works. I finally found some info, telling me i have to specify each object in the parent-child relations from workbook to range (variables for workbooks, workbook, worksheets, worksheet, range) and that you could not declare and instatiate on one line. Anyway, I tried that, and here is where it gets weird. i have two workbooks (one that is an add, and one that opens an existing file). Well, when it came time to instatiate my worksheet objects, i had to set the existing file's sheet variable to my worksheets collection .sheet type, and the new workbook's worksheet variable to it's worksheets collection .worksheet type.
Why one needs worksheet and one needs sheet i will never know, or ever want to. but that combination works (and only that combination).

I miss hello world console apps!


thanks again though.


hayt
 
PS - you get a star.....sorry....no naming of children
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top