View Single Post
  #10   Report Post  
Posted to microsoft.public.excel.misc
Dave Peterson
 
Posts: n/a
Default Open File within a macro

Record a macro when you parse your input file. You'll need that to lay out each
field.

Then you can merge your recorded code into this sample:

Option Explicit
Sub testme()

Dim WSHShell As Object
Dim DesktopPath As String
Dim myCurrentPath As String
Dim myFileName As Variant

Set WSHShell = CreateObject("WScript.Shell")
DesktopPath = WSHShell.SpecialFolders("Desktop")

myCurrentPath = CurDir

ChDrive DesktopPath
ChDir DesktopPath

myFileName = Application.GetOpenFilename("Text Files, *.txt")

If myFileName = False Then
'do nothing
Else
Workbooks.OpenText Filename:=myFileName, ...rest of recorded macro
End If

ChDrive myCurrentPath
ChDir myCurrentPath

End Sub


Rob wrote:

Thanks Dave,

However, I think I've missunderstood this procedure. I was hoping to obtain
the code to run the Open file dialogue pointing to any desktop, then to
allow the user to make a selection which would then import the text file
selected to a spreadsheet.

Rob

"Dave Peterson" wrote in message
...
The TrailingMinusNumbers is an option that was added in xl2002.

You can delete that portion ", TrailingMinusNumbers:=True" and try it once
more.

Rob wrote:

Thanks "Gary''s Student".

When I run this I get an error message stating that the Named argument
TrailingMinusNumbers not found.

What do I do with that, please? Do I need to stick that in a dim
statement
somehow?

Rob

"Gary''s Student" wrote in
message
...
As this is coded:

Sub GetData()
ChDir "C:\Documents and Settings\Owner\Desktop"
Workbooks.OpenText Filename:="x.txt", Origin:=437, StartRow:=1, _
DataType:=xlFixedWidth, FieldInfo:=Array(Array(0, 1), Array(10,
1),
Array(20, 1) _
, Array(39, 1)), TrailingMinusNumbers:=True

Windows("x.txt").Activate
Call RetainData
Call LabelData
End Sub

It opens a text file on my desktop with a given number of columns
(adapting
code from the macro recorder. (ignore the calls to the other two subs)

Just use an inputbox for your path and filename.
--
Gary's Student


"Rob" wrote:

I would like to know the code (to put within some other code), which
will
run the Open file dialogue, & pointing to the desktop, so that a text
file
can be selected.
(Then, I need that selected text file opened so that the macro will do
some
action on that file and revert back to the workbook for mor action.)
I found something in help, which opens the dialogue box, but it does
not
open the file, just brings up a message box that tells me the file
I've
selected.

Furthermore, is there a way for that code to point to any desktop no
matter
where it is located in the Windows Explorer tree? (As I plan to use
the
workbook on various machines.)

Rob




--

Dave Peterson


--

Dave Peterson