473,378 Members | 1,386 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Import text file into excel using vbscript

2
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.

Expand|Select|Wrap|Line Numbers
  1.  
  2. Sub vba_excel_importing_file()
  3.     Dim strFileName As String
  4.  
  5.     strFileName = InputBox("Enter the full path to the comma " & _
  6.                "separated file to import")
  7.  
  8.     Set oExcel = CreateObject("Excel.Application")
  9.  
  10.     With oExcel.ActiveSheet.QueryTables.Add(Connection:= _
  11.         "TEXT;" & strFileName, Destination:=oExcel.Range("A1") _
  12.         )
  13.         oExcel.Name = "vba excel importing file"
  14.         oExcel.FieldNames = True
  15.         oExcel.RowNumbers = False
  16.         oExcel.FillAdjacentFormulas = False
  17.         oExcel.PreserveFormatting = True
  18.         oExcel.RefreshOnFileOpen = False
  19.         oExcel.RefreshStyle = xlInsertDeleteCells
  20.         oExcel.SavePassword = False
  21.         oExcel.SaveData = True
  22.         oExcel.AdjustColumnWidth = True
  23.         oExcel.RefreshPeriod = 0
  24.         oExcel.TextFilePromptOnRefresh = False
  25.         oExcel.TextFilePlatform = 437
  26.         oExcel.TextFileStartRow = 1
  27.         oExcel.TextFileParseType = xlDelimited
  28.         oExcel.TextFileTextQualifier = xlTextQualifierDoubleQuote
  29.         oExcel.TextFileConsecutiveDelimiter = False
  30.         oExcel.TextFileTabDelimiter = False
  31.         oExcel.TextFileSemicolonDelimiter = False
  32.         oExcel.TextFileCommaDelimiter = True
  33.         oExcel.TextFileSpaceDelimiter = False
  34.         oExcel.TextFileColumnDataTypes = Array(1, 1, 1, 1)
  35.         oExcel.TextFileTrailingMinusNumbers = True
  36.         oExcel.Refresh BackgroundQuery:=False
  37.     End With
  38. End Sub
  39.  
  40.  
Feb 8 '12 #1
1 14521
Guido Geurs
767 Expert 512MB
This will import a file into Excel.


Expand|Select|Wrap|Line Numbers
  1. Do While X = 0
  2.     strAnswer = InputBox _
  3.         ("Please enter a name :","Load TXT File")
  4.     If strAnswer = "" Then
  5.         Wscript.Echo "You must enter a file name."
  6.     Else
  7.         Wscript.Echo strAnswer
  8.         Exit Do
  9.     End If
  10. Loop
  11.  
  12.  
  13. Dim objUser, strExcelPath, objExcel, objSheet, _
  14. objFSO, objFile, aline, l, irow, icol
  15.  
  16. Const ForReading = 1
  17.  
  18. Set objFSO = CreateObject("Scripting.FileSystemObject")
  19. Set objFile = objFSO.OpenTextFile(strAnswer, ForReading)
  20.  
  21. ' strExcelPath = ...
  22. ' Bind to Excel object.
  23. 'On Error Resume Next
  24. Set objExcel = CreateObject("Excel.Application")
  25. If (Err.Number <> 0) Then
  26.     Wscript.Echo "Excel application not found."
  27.     Wscript.Quit
  28. End If
  29.  
  30. With objExcel
  31.     .Visible = True
  32.     .Workbooks.Add
  33.     Set objSheet =     .ActiveWorkbook.Worksheets(1)
  34.     objSheet.Name = "test"
  35. End with
  36.  
  37. irow= 1
  38. icol= 1
  39. While Not objFile.AtEndOfStream
  40.     l = objFile.ReadLine
  41.     objSheet.Cells(irow, icol) = l
  42.     irow= irow+ 1
  43. Wend
  44.  
  45. objExcel.UserControl = True
Feb 12 '12 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Kevin Forbes | last post by:
Hi there, When importing a text file using SQL Server, how do I specify the path to a file on the same remote server that SQL Server is running on? I tried //c:/filename but that doesn't seem to...
4
by: news | last post by:
Our production database in an exported textfil runs about 60 MB. Compressed that's about 9 MB. I'm trying to import the export into another machine running FC3 and mySQL 11.18, and it appears as...
2
by: John | last post by:
HI, I'm having trouble importing text files. I had problem with Access '97 that forced me to reinstall. After I did this I couldn't import text files. The Help file recommends reinstall again,...
3
by: Dave G | last post by:
I will shortly be receiving data in the form of a text file, like this: id: 123456 first name: Fred surname: Bloggs age: 26 and so on, for about 60 fields. Each line ends with a carriage...
1
by: sachin10 | last post by:
hi i m sachin, i m new to VB programming.i face some problem regarding the sorting of text file contents using arrays.text file have data in the format as below seq...
0
by: piyush bharadwaj | last post by:
Hi friends , i want to creat a new excel file and than import data from text file in C#.Net plz help me
3
by: tjuiowa | last post by:
Hi I am trying to import a text file into a MS Access database. The first 11 lines of the text file are in a different format, so i would like to skip those lines and start at line 12. is there...
4
by: angelasg | last post by:
Hello. I am saving an Excel file as a text file then importing it into Access using a specification. One of the fields in the file is a currency formatted with commas. Those numbers over 999...
1
by: Noorain | last post by:
sir my data are in text file. i want import text file in mysql data to use php script. please help me............. my text file is 1#AA#2,345.00 2#BB#850.00 3#CC#31,855.00
3
by: Veeky | last post by:
Hi All, I am very new in C# and am doing a test project. In my project i m using a text file which has records along with feild name in each line. How can i import the data in a Hashtable ?? ...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.