#1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Open Text

I am working in Excel 2000 and need to program a way to open a text file
saved as a .dat file. I used the macro recorded and this is what I get.
When I use this it works, but I don't want to hardcode the file name. I have
a screen that the user can use to select the file and I store the path in a
variable. However, when I try to put the variable in the place of the actual
file name I get errors. How can I use a variable in the below:

Workbooks.OpenText Filename:= _
"C:\Documents and Settings\YI016695\My Documents\J&J\SDR-000J.DAT",
Origin:= _
xlWindows, StartRow:=1, DataType:=xlFixedWidth,
FieldInfo:=Array(Array(0, _
1), Array(8, 1), Array(17, 1), Array(52, 1), Array(65, 1), Array(71,
1))
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 17
Default Open Text

All you need to do is:

Dim Location As String

Location = "C:\Documents and Settings\YI016695\My Documents\J&J\SDR-000J.DAT"

Then the following code should work:

Workbooks.OpenText Filename:= _
Location,
Origin:= _
xlWindows, StartRow:=1, DataType:=xlFixedWidth,
FieldInfo:=Array(Array(0, _
1), Array(8, 1), Array(17, 1), Array(52, 1), Array(65, 1), Array(71,
1))



"Dwaine Horton" wrote:

I am working in Excel 2000 and need to program a way to open a text file
saved as a .dat file. I used the macro recorded and this is what I get.
When I use this it works, but I don't want to hardcode the file name. I have
a screen that the user can use to select the file and I store the path in a
variable. However, when I try to put the variable in the place of the actual
file name I get errors. How can I use a variable in the below:

Workbooks.OpenText Filename:= _
"C:\Documents and Settings\YI016695\My Documents\J&J\SDR-000J.DAT",
Origin:= _
xlWindows, StartRow:=1, DataType:=xlFixedWidth,
FieldInfo:=Array(Array(0, _
1), Array(8, 1), Array(17, 1), Array(52, 1), Array(65, 1), Array(71,
1))

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Open Text

It would be better to use the GetOpenFileName method, which allows you to
navigate to the file. Look it up in help.

--

HTH

RP
(remove nothere from the email address if mailing direct)


"Nick Shinkins" wrote in message
...
All you need to do is:

Dim Location As String

Location = "C:\Documents and Settings\YI016695\My

Documents\J&J\SDR-000J.DAT"

Then the following code should work:

Workbooks.OpenText Filename:= _
Location,
Origin:= _
xlWindows, StartRow:=1, DataType:=xlFixedWidth,
FieldInfo:=Array(Array(0, _
1), Array(8, 1), Array(17, 1), Array(52, 1), Array(65, 1),

Array(71,
1))



"Dwaine Horton" wrote:

I am working in Excel 2000 and need to program a way to open a text file
saved as a .dat file. I used the macro recorded and this is what I get.
When I use this it works, but I don't want to hardcode the file name. I

have
a screen that the user can use to select the file and I store the path

in a
variable. However, when I try to put the variable in the place of the

actual
file name I get errors. How can I use a variable in the below:

Workbooks.OpenText Filename:= _
"C:\Documents and Settings\YI016695\My

Documents\J&J\SDR-000J.DAT",
Origin:= _
xlWindows, StartRow:=1, DataType:=xlFixedWidth,
FieldInfo:=Array(Array(0, _
1), Array(8, 1), Array(17, 1), Array(52, 1), Array(65, 1),

Array(71,
1))



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Cannot open text workbook jat Excel Worksheet Functions 1 December 23rd 09 07:21 AM
How to set up a worksheet to open where the next text should go? whazzuptoo Excel Discussion (Misc queries) 1 July 13th 09 05:47 PM
use a button to open a text box Simon Blackburn New Users to Excel 1 July 12th 07 04:50 PM
Excel VBA - open text file, replace text, save file? Cybert Excel Programming 2 October 2nd 04 01:05 AM
Workbooks.Open / .Open Text - How do you stop the .xls addition? Dave[_20_] Excel Programming 2 July 31st 03 04:03 AM


All times are GMT +1. The time now is 08:48 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"