473,395 Members | 1,558 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,395 software developers and data experts.

OpenText method in Excel

Lee
I have been trying for *many* hours to figure out how to
use the OpenText method in Excel. I have a data file in
text format (ASCII) and I want to import this data into
an Excel worksheet using space delimiting. In other
words, there are spaces between the data. Please refer
to the following code:

Dim app As Excel.Application
Dim book As Excel.Workbook
Dim sheet As Excel.Worksheet
Dim strFileName1 As String = "C:\Temp\Data1.txt"
Dim strFileName2 As String = "C:\Temp\Data2.xls"
app = CType(CreateObject("Excel.Application"),
Excel.Application)
book = CType(app.Workbooks.Open(strFileName2),
Excel.Workbook)
app.Visible = True

Could someone please tell me how to use the OpenText
method in place of the Open method and make it space
delimited? Everything I have tried imports each line of
the data into the first column instead separating out
into different columns.

Thank you in advance,
Lee
Nov 20 '05 #1
2 9614
"Lee" <hu*********@pacbell.net> schrieb
I have been trying for *many* hours to figure out how to
use the OpenText method in Excel. I have a data file in
text format (ASCII) and I want to import this data into
an Excel worksheet using space delimiting. In other
words, there are spaces between the data. Please refer
to the following code:

Dim app As Excel.Application
Dim book As Excel.Workbook
Dim sheet As Excel.Worksheet
Dim strFileName1 As String = "C:\Temp\Data1.txt"
Dim strFileName2 As String = "C:\Temp\Data2.xls"
app = CType(CreateObject("Excel.Application"),
Excel.Application)
book = CType(app.Workbooks.Open(strFileName2),
Excel.Workbook)
app.Visible = True

Could someone please tell me how to use the OpenText
method in place of the Open method and make it space
delimited? Everything I have tried imports each line of
the data into the first column instead separating out
into different columns.
Sorry that I didn't answer as promised!

First, here is also the quote from your other thread so that I can refer to
it in this post:
I tried your suggestion but the software tells
me "Expression does not produce a value" for the first
argument of the CType function. I guess I don't know
enough to solve the problem as you suggested.

I copied the code above and it can be compiled without a problem. Now, I add
this line from the other thread:

book = app.Workbooks.OpenText(strFileName, _
Origin:=437, StartRow:=1, DataType:=xlDelimited,
TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=True, Space:=True)

After replacing the constant names by their qualified name, the line is:

book = app.Workbooks.OpenText( _
strFileName1, _
Origin:=437, StartRow:=1, _
DataType:=Excel.XlTextParsingType.xlDelimited, _
TextQualifier:=Excel.XlTextQualifier.xlTextQualifi erDoubleQuote, _
ConsecutiveDelimiter:=True, Space:=True _
)

The error message I get is the same as you got: "Expression does not produce
a value". The reason is obvious: OpenText is a sub, not a function. A sub
does not return a value, so there is nothing to assign to 'book'. After
removing 'book =' the correct line is:

app.Workbooks.OpenText( _
strFileName1, _
Origin:=437, StartRow:=1, _
DataType:=Excel.XlTextParsingType.xlDelimited, _
TextQualifier:=Excel.XlTextQualifier.xlTextQualifi erDoubleQuote, _
ConsecutiveDelimiter:=True, Space:=True _
)

As I am not very familiar with the Excel object model, I do not know how to
get a reference to the opened workbook. Maybe it is always the workbook with
the highest index? You have to try this because I also didn't find an answer
anywhere else. I think you should ask in the Excel VBA group because the
question is related to the Excel object model.
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #2
Lee
That did it!!!!!!
Thank you sooooo much Armin.
-Lee
-----Original Message-----
"Lee" <hu*********@pacbell.net> schrieb
I have been trying for *many* hours to figure out how to use the OpenText method in Excel. I have a data file in text format (ASCII) and I want to import this data into
an Excel worksheet using space delimiting. In other
words, there are spaces between the data. Please refer
to the following code:

Dim app As Excel.Application
Dim book As Excel.Workbook
Dim sheet As Excel.Worksheet
Dim strFileName1 As String = "C:\Temp\Data1.txt" Dim strFileName2 As String = "C:\Temp\Data2.xls" app = CType(CreateObject("Excel.Application"),
Excel.Application)
book = CType(app.Workbooks.Open(strFileName2),
Excel.Workbook)
app.Visible = True

Could someone please tell me how to use the OpenText
method in place of the Open method and make it space
delimited? Everything I have tried imports each line of the data into the first column instead separating out
into different columns.
Sorry that I didn't answer as promised!

First, here is also the quote from your other thread so

that I can refer toit in this post:
I tried your suggestion but the software tells
me "Expression does not produce a value" for the first
argument of the CType function. I guess I don't know
enough to solve the problem as you suggested.

I copied the code above and it can be compiled without a

problem. Now, I addthis line from the other thread:

book = app.Workbooks.OpenText(strFileName, _
Origin:=437, StartRow:=1, DataType:=xlDelimited,
TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=True, Space:=True)

After replacing the constant names by their qualified name, the line is:
book = app.Workbooks.OpenText( _
strFileName1, _
Origin:=437, StartRow:=1, _
DataType:=Excel.XlTextParsingType.xlDelimited, _
TextQualifier:=Excel.XlTextQualifier.xlTextQualifi erDouble
Quote, _ ConsecutiveDelimiter:=True, Space:=True _
)

The error message I get is the same as you got: "Expression does not producea value". The reason is obvious: OpenText is a sub, not a function. A subdoes not return a value, so there is nothing to assign to 'book'. Afterremoving 'book =' the correct line is:

app.Workbooks.OpenText( _
strFileName1, _
Origin:=437, StartRow:=1, _
DataType:=Excel.XlTextParsingType.xlDelimited, _
TextQualifier:=Excel.XlTextQualifier.xlTextQualifi erDouble
Quote, _ ConsecutiveDelimiter:=True, Space:=True _
)

As I am not very familiar with the Excel object model, I do not know how toget a reference to the opened workbook. Maybe it is always the workbook withthe highest index? You have to try this because I also didn't find an answeranywhere else. I think you should ask in the Excel VBA group because thequestion is related to the Excel object model.
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

.

Nov 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Benjamin | last post by:
I am making a Windows EXE (.NET Visual Basic Windows Application). I would like to make it possible to open Excel Worksheets from my EXE. This should work independent of Excel version on client...
0
by: Przemek Wrzesiński | last post by:
Hi, i'm trying to code my first add-in, which should open file selected by user (txt file, space delimited), do some formatting, calculation and pivot table at the end. My problem is, that I can't...
0
by: softengine | last post by:
I have a dataset with multiple tables I need exported to Excel. I can get it into Excel into one worksheet however I want there to be one worksheet for each datatable in the dataset. If I dump my...
0
by: Matt | last post by:
I'm trying to use late binding to automate Excel from C# so as to run with multiple versions of Excel, and am hoping to avoid referencing any Excel PIA in my project. But I need to add a C# event...
3
by: Giovanni Bassi | last post by:
Hello Group, I am running an operation in a different thread. There are resources that are released when the thread is done running. This is done at the end of the execution as it raises an...
0
by: KH Tay | last post by:
Hi, I can able to run the Excel Macro command in VB6. Unfortunately , VB.Net always prompt Array - no declaring. I've insert the Excel references, and it is still not working. Can anyone help me ?...
6
by: =?Utf-8?B?TUNI?= | last post by:
I try to open a log file that is logging infomation by another process: StreamReader sr = File.OpenText(filePath); I got the exception message: The process cannot access the file...
2
by: RohitGBhagwat | last post by:
Hi, I am facing problem in ASP application where I am getting error message "Server object, ASP 0178 (0x80070005) The call to Server.CreateObject failed while checking permissions. Access is...
2
by: JonathanS | last post by:
Hi- I'm using Excel 2000 and attempting to import only the first n lines of a text file. The Wizard allows the user to select the first line, but is there a way to specify the last line of text...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.