macro key

R

redburns

Is it possible to make a Macro in Word or Excel, to go
into a Folder and open a text file, then append to a
Microsoft file? The catch is every day is other name on
the file.
 
R

redburns

-----Original Message-----
Is it possible to make a Macro in Word or Excel, to go
into a Folder and open a text file, then append to a
Microsoft file? The catch is every day is other name on
the file.
Sorry the file is the same format every day!
 
D

Dave Peterson

Record a macro when you do it once (This is for excel--I don't know Word!).

When you recorded your macro, you got something that looked like:

Option Explicit
Sub Macro1()

Workbooks.OpenText Filename:="C:\myfile.txt", Origin:=437, StartRow:=1, _
DataType:=xlFixedWidth, FieldInfo:=Array(Array(0, 1), Array(15, 1), _
Array(41, 1))

End Sub

Well, instead of having your filename in the code, you can give the user a
chance to pick it themselves (take a look at getopenfilename in VBA's help):

Sub macro1A()

Dim myFileName As Variant

myFileName = Application.GetOpenFilename(filefilter:="Text Files, *.Txt", _
Title:="Pick a File")

If myFileName = False Then
MsgBox "Ok, try later" 'user hit cancel
Exit Sub
End If

Workbooks.OpenText Filename:=myFileName '....rest of recorded code here!

End Sub
 
Top