Excel troubles

TimmyHot123

New member
Joined
Nov 27, 2003
Messages
3
I am trying to insert a row between two rows and i want the program to do it consistantly because I have about 10 files with 200 lines that i need this to be done to. so for example

;ldsjlsadfj

asdljf;slda

asd;fljsfs

This is what i would like to be done in the program. Thank you for your help.
 
can someone please help me i dont really know anything about programming in excel and it would be a great help if someone could givem e some kind of information.
 
This should give something to build from:

Notes:
You will need to add a COM reference to the Microsoft Office Object Library.

Ive noticed that after you run it, you will need to close the app for excel to truly close.

While testing and debugging if Excel starts acting wierd, go to task manager and close any instances of "EXCEL.EXE"

Code:
        Misc Vars
        Dim iRow As Integer = 1
        Dim x As Integer = 0
        Dim y As Integer = 0

        Get the Excel Application ready
        Dim xlApp As Excel.Application
        xlApp = CType(CreateObject("Excel.Application"), Excel.Application)
        xlApp.Visible = True
        xlApp.DisplayAlerts = False

        Get the Excel Workbook ready
        Dim xlBook As Excel.Workbook
        xlBook = CType(xlApp.Workbooks.Add, Excel.Workbook)

        Get the Excel WorkSheet ready
        Dim xlSheet As New Excel.Worksheet
        xlSheet = CType(xlBook.Worksheets(1), Excel.Worksheet)


        This is where you would get the file names, and
        add them to some sort of a list or array
        Dim strFiles() As String = {"FILE1", "FILE2", "FILE3"}

        For each file...
        For x = 0 To strFiles.Length - 1

            Add the file name to the WorkSheet
            xlSheet.Cells(iRow, 1) = strFiles(x)

            This is where you would read the lines of the file, and
            add them to some sort of a list or array
            Dim strFileLines() As String = {"Line 1", "Line 2", "Line 3"}

            For each line...
            For y = 0 To strFileLines.Length - 1

                Add the file name to the WorkSheet
                xlSheet.Cells(iRow, 2) = strFileLines(y)

                Set the next row
                iRow += 1

            Next

            Add an empty row after each file
            iRow += 1

        Next

        Cleanup...
        xlSheet = Nothing

        xlBook.SaveAs("c:\test.xls")
        xlBook.Save()
        xlBook.Close()
        xlBook = Nothing

        xlApp.Quit()
        xlApp = Nothing
 
Couple of changes:

Actually it is a COM reference to Microsoft Excel Object Library, "Microsoft Excel 10.0 Object Library" for me.

I just found on another post that putting GC.Collect at the end may help with the Excel staying alive bug. So the last few lines would look like this:

Code:
.......

 xlApp.Quit()
 xlApp = Nothing
 
 GC.Collect()
 
Wow, that looks like some good code... a mini-tutorial, really :)

But I wonder, is the explicit use of CType() everywhere really necessary, such as here:
Code:
        xlBook = CType(xlApp.Workbooks.Add, Excel.Workbook)
Im completely new to VB.Net, so I really am asking... Is this actually needed here, or were you just being 100% safe?

-- Mike
 
I think you should use the CType. If you turn "Option Strict" on. ("Project" -> [project] Properties -> Build) and you arent using the CType, then you will get these type of errors when you build:

"Option Strict On disallows implicit conversions from System.Object to Excel.Application."
 
Ok, thanks... still seems odd to me. If the xlBook is declared As Excel.Workbook and Workbooks.Add() can only create one type of Object (an Excel.Workbook), I dont see the need for CType(), but then I see it thrown around everywhere by everyone, so I guess it is necessary...

Thanks :),
Mike
 
Ok, thanks... still seems odd to me. If the xlBook is declared As Excel.Workbook and Workbooks.Add() can only create one type of Object (an Excel.Workbook), I dont see the need for CType(), but then I see it thrown around everywhere by everyone, so I guess it is necessary...

Thanks :),
Mike
 
Back
Top