Results 1 to 8 of 8

Thread: Excel And VB6 Question?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2003
    Location
    Out there somewhere!
    Posts
    386

    Talking Excel And VB6 Question?

    I have 2 .csv files that I want to convert into a xls file showing each of the .csv files on a seperate worksheet.

    At the moment I have the following code -

    Code:
        Dim objExcel As Excel.Application
        Dim objWrkBk As Excel.Workbook
        Dim objSheet As Excel.Worksheet
        Dim strFilename As String
        Dim strExcelFileName As String
        Dim intResp As Integer
        
        Set objExcel = CreateObject("Excel.Application")
        
        objExcel.Workbooks.OpenText FileName:=App.Path & "/" & "StandardHmeLabelsRep.csv", Origin:=xlWindows, StartRow:=1, _
            DataType:=xlDelimited, TextQualifier:=xlTextQualifierDoubleQuote, ConsecutiveDelimiter:=False, _
            Tab:=False, Semicolon:=True, Comma:=False, Space:=False, Other:=False, _
            FieldInfo:=Array(Array(1, 1), Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1), Array(6, 1), _
            Array(7, 1), Array(8, 1), Array(9, 1))
        
        strExcelFileName = App.Path & "\LabelsStandardReport.xls"
        
        If Dir(strExcelFileName) <> "" Then
            intResp = MsgBox(strExcelFileName & " already exists, do you wish to overwrite this file?", vbQuestion + vbYesNoCancel, "FIFG - separation")
            
            If intResp = vbNo Then
                comGetFile.ShowOpen
                strExcelFileName = comGetFile.FileName
                
                If strExcelFileName = "" Or IsNull(strExcelFileName) Then
                    objExcel.Workbooks.Close
                    objExcel.Quit
                    Set objExcel = Nothing
                    MsgBox "You have cancelled the Actual Paid report!", vbInformation + vbOKOnly
                    g_ShowGlass False
                    Exit Sub
                End If
            ElseIf intResp = vbYes Then
                Kill strExcelFileName
            ElseIf intResp = vbCancel Then
                objExcel.Workbooks.Close
                objExcel.Quit
                Set objExcel = Nothing
                MsgBox "You have cancelled the Actual Paid report!", vbInformation + vbOKOnly
                g_ShowGlass False
                Exit Sub
            End If
        End If
        
        objExcel.ActiveWorkbook.SaveAs FileName:=strExcelFileName, FileFormat:= _
        xlNormal, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False _
        , CreateBackup:=False
        
        objExcel.Workbooks.Open strExcelFileName
        objExcel.Worksheets.Add
    
        
        objExcel.Workbooks.OpenText FileName:=App.Path & "/" & "StandardExpLabelsRep.csv", Origin:=xlWindows, StartRow:=1, _
        DataType:=xlDelimited, TextQualifier:=xlTextQualifierDoubleQuote, ConsecutiveDelimiter:=False, _
        Tab:=False, Semicolon:=False, Comma:=True, Space:=False, Other:=False, _
        FieldInfo:=Array(Array(1, 1), Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1), Array(6, 1), _
        Array(7, 1), Array(8, 1), Array(9, 1))
    '
    '    objExcel.ActiveWorkbook.SaveAs FileName:=strExcelFileName, FileFormat:= _
    '        xlNormal, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False _
    '        , CreateBackup:=False
    
        objExcel.Workbooks.Close
        objExcel.Quit
        Set objExcel = Nothing
    I can place the first csv file fine and create a new worksheet, but how do I place the second csv file onto the newlyt created worksheet?

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Excel And VB6 Question?

    the worksheets are a collection so you need to move to the next sheet and set it as the worksheet object

    rgds pete

  3. #3
    Addicted Member thiru_rajamani's Avatar
    Join Date
    Aug 2004
    Location
    Chennai,India
    Posts
    214

    Re: Excel And VB6 Question?

    Since already you have a workbook, just add a sheet or activate the next sheet
    Thanks
    Raj

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2003
    Location
    Out there somewhere!
    Posts
    386

    Re: Excel And VB6 Question?

    Thanks i have done that now.

    Is there a quick way of loading a comma delimited file into the active work sheet?

    thanks

    Kev

  5. #5
    Frenzied Member trisuglow's Avatar
    Join Date
    Jan 2002
    Location
    Horsham, Sussex, UK
    Posts
    1,536

    Re: Excel And VB6 Question?

    Does this work for you?

    VB Code:
    1. Workbooks.Open "C:\PersonalContacts.csv", , , 2
    This world is not my home. I'm just passing through.

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2003
    Location
    Out there somewhere!
    Posts
    386

    Re: Excel And VB6 Question?

    I don't think so.

    I have an exsiting spreadsheet that I open, add a new worksheet and make that worksheet active. I then want to place the contents of a csv file into the new worksheet. So far all I have is

    Code:
        objExcel.Workbooks.Open strExcelFileName
        objExcel.Worksheets.Add after:=Sheets(Worksheets.Count)
    
        objExcel.Worksheets(Worksheets.Count).Select
    I tried the loading using the opentext method, but is only used for creating a new workbook.

  7. #7
    Frenzied Member trisuglow's Avatar
    Join Date
    Jan 2002
    Location
    Horsham, Sussex, UK
    Posts
    1,536

    Re: Excel And VB6 Question?

    What about this then?

    VB Code:
    1. Sub ImportCSV()
    2.     Dim f As Integer
    3.     Dim s As String
    4.     Dim l() As String
    5.     Dim r As Long
    6.    
    7.     r = 20
    8.     f = FreeFile
    9.     Open "C:\PersonalContacts.csv" For Input As f
    10.     While Not EOF(f)
    11.         Line Input #f, s
    12.         l = Split(s, ",")
    13.         For c = 1 To UBound(l)
    14.             Cells(r, c).Value = l(c)
    15.         Next
    16.         r = r + 1
    17.     Wend
    18.     Close f
    19. End Sub
    This world is not my home. I'm just passing through.

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2003
    Location
    Out there somewhere!
    Posts
    386

    Re: Excel And VB6 Question?

    That sort of works, it is missing the first Line and first column but I can get around that. It also is puts quotes around everything.

    I'll figure it out now that you gave me a workable suggestion. Thnaks for your help.



    Kev.

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