Import text file into excel using vbscript

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Suggz
    New Member
    • Feb 2012
    • 2

    Import text file into excel using vbscript

    Hi,

    I am attempting to import a text file, but I get an error "(10094) ActiveX Automation: Object var is 'Nothing'."

    Not sure what the mistake here is.

    Code:
    Sub vba_excel_importing_file()
        Dim strFileName As String
    
        strFileName = InputBox("Enter the full path to the comma " & _
                   "separated file to import")
    
    	Set oExcel = CreateObject("Excel.Application")
    
        With oExcel.ActiveSheet.QueryTables.Add(Connection:= _
            "TEXT;" & strFileName, Destination:=oExcel.Range("A1") _
            )
            oExcel.Name = "vba excel importing file"
            oExcel.FieldNames = True
            oExcel.RowNumbers = False
            oExcel.FillAdjacentFormulas = False
            oExcel.PreserveFormatting = True
            oExcel.RefreshOnFileOpen = False
            oExcel.RefreshStyle = xlInsertDeleteCells
            oExcel.SavePassword = False
            oExcel.SaveData = True
            oExcel.AdjustColumnWidth = True
            oExcel.RefreshPeriod = 0
            oExcel.TextFilePromptOnRefresh = False
            oExcel.TextFilePlatform = 437
            oExcel.TextFileStartRow = 1
            oExcel.TextFileParseType = xlDelimited
            oExcel.TextFileTextQualifier = xlTextQualifierDoubleQuote
            oExcel.TextFileConsecutiveDelimiter = False
            oExcel.TextFileTabDelimiter = False
            oExcel.TextFileSemicolonDelimiter = False
            oExcel.TextFileCommaDelimiter = True
            oExcel.TextFileSpaceDelimiter = False
            oExcel.TextFileColumnDataTypes = Array(1, 1, 1, 1)
            oExcel.TextFileTrailingMinusNumbers = True
            oExcel.Refresh BackgroundQuery:=False
        End With
    End Sub
  • Guido Geurs
    Recognized Expert Contributor
    • Oct 2009
    • 767

    #2
    This will import a file into Excel.


    Code:
    Do While X = 0
        strAnswer = InputBox _
            ("Please enter a name :","Load TXT File")
        If strAnswer = "" Then
            Wscript.Echo "You must enter a file name."
        Else
            Wscript.Echo strAnswer
            Exit Do
        End If
    Loop
    
    
    Dim objUser, strExcelPath, objExcel, objSheet, _
    objFSO, objFile, aline, l, irow, icol
    
    Const ForReading = 1
    
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFile = objFSO.OpenTextFile(strAnswer, ForReading)
    
    ' strExcelPath = ...
    ' Bind to Excel object.
    'On Error Resume Next
    Set objExcel = CreateObject("Excel.Application")
    If (Err.Number <> 0) Then
        Wscript.Echo "Excel application not found."
        Wscript.Quit
    End If
    
    With objExcel
    	.Visible = True
    	.Workbooks.Add
    	Set objSheet = 	.ActiveWorkbook.Worksheets(1)
    	objSheet.Name = "test"
    End with
    
    irow= 1
    icol= 1
    While Not objFile.AtEndOfStream
        l = objFile.ReadLine
        objSheet.Cells(irow, icol) = l
        irow= irow+ 1
    Wend
    
    objExcel.UserControl = True

    Comment

    Working...