Home |
Search |
Today's Posts |
|
#1
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Is there any way I can open a text file in the workbook that the
macros is running in. Everytime I try with my code it automatically opens a new workbook. I have looked at other comments for other people but none of them work because I have a datatype and fieldinfo. This is my code to open a txt file Dim UF As Variant Dim SL As Variant UF = Application.GetOpenFilename(FileFilter:="TXT Files(*.txt),*.txt", Title:="log") Workbooks.OpenText Filename:=UF, DataType:=xlFixedWidth, FieldInfo:=Array(Array(0, 2)) |
#2
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
If you record a macro when you open one of the .txt files, won't you see the
info that you need for your code? wrote: Is there any way I can open a text file in the workbook that the macros is running in. Everytime I try with my code it automatically opens a new workbook. I have looked at other comments for other people but none of them work because I have a datatype and fieldinfo. This is my code to open a txt file Dim UF As Variant Dim SL As Variant UF = Application.GetOpenFilename(FileFilter:="TXT Files(*.txt),*.txt", Title:="log") Workbooks.OpenText Filename:=UF, DataType:=xlFixedWidth, FieldInfo:=Array(Array(0, 2)) -- Dave Peterson |
#3
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Opentext places the textfile in a new workbook. It is trivial to then copy
it to your existing workbook - that is the method I use. Alternately you can use querytable to do it directly. This is an old post that illustrates: -------------<old post----------------- Sub TestGetTxtFile() ' ' TestGetTxtFile Macro ' Macro recorded 10/7/2003 by Authorized User ' With ActiveSheet.QueryTables.Add(Connection:= _ "TEXT;C:\NWACCESS\DATA\TIR Updates\test.txt", Destination:=Range("A1")) .Name = "test" .FieldNames = True .RowNumbers = False .FillAdjacentFormulas = False .PreserveFormatting = True .RefreshOnFileOpen = False .RefreshStyle = xlInsertDeleteCells .SavePassword = False .SaveData = True .AdjustColumnWidth = True .RefreshPeriod = 0 .TextFilePromptOnRefresh = False .TextFilePlatform = xlWindows .TextFileStartRow = 1 .TextFileParseType = xlDelimited .TextFileTextQualifier = xlTextQualifierDoubleQuote .TextFileConsecutiveDelimiter = False .TextFileTabDelimiter = False .TextFileSemicolonDelimiter = False .TextFileCommaDelimiter = True .TextFileSpaceDelimiter = False .TextFileColumnDataTypes = Array(1, 1, 2, 1, 1, 2, 1) .Refresh BackgroundQuery:=False End With End Sub So: You're using the QueryTables collection object. Go to VBA help and type that into the Answer Wizard box and read all about it. If your text file is always saved as a certain name - "MyData mmddyy.txt" (where mmddyy is a date identifier) into the same folder, then you can code your file path and name to get the file without using a dialogue box. Using the .TextFileColumnDataTypes = Array(1, 1, 2, 1, 1, 2, 1) line, you can specify if any columns are general, text, or numeric. I just used General (1) and Text (2). Before the End Sub, insert code to SaveAs with a file path and name, and you should be good to go. -----------<End Old Post----------- -- Regards, Tom Ogilvy " wrote: Is there any way I can open a text file in the workbook that the macros is running in. Everytime I try with my code it automatically opens a new workbook. I have looked at other comments for other people but none of them work because I have a datatype and fieldinfo. This is my code to open a txt file Dim UF As Variant Dim SL As Variant UF = Application.GetOpenFilename(FileFilter:="TXT Files(*.txt),*.txt", Title:="log") Workbooks.OpenText Filename:=UF, DataType:=xlFixedWidth, FieldInfo:=Array(Array(0, 2)) |
#4
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Or use
http://www.cpearson.com/excel/imptext.htm -- Regards Ron de Bruin http://www.rondebruin.nl/tips.htm "Tom Ogilvy" wrote in message ... Opentext places the textfile in a new workbook. It is trivial to then copy it to your existing workbook - that is the method I use. Alternately you can use querytable to do it directly. This is an old post that illustrates: -------------<old post----------------- Sub TestGetTxtFile() ' ' TestGetTxtFile Macro ' Macro recorded 10/7/2003 by Authorized User ' With ActiveSheet.QueryTables.Add(Connection:= _ "TEXT;C:\NWACCESS\DATA\TIR Updates\test.txt", Destination:=Range("A1")) .Name = "test" .FieldNames = True .RowNumbers = False .FillAdjacentFormulas = False .PreserveFormatting = True .RefreshOnFileOpen = False .RefreshStyle = xlInsertDeleteCells .SavePassword = False .SaveData = True .AdjustColumnWidth = True .RefreshPeriod = 0 .TextFilePromptOnRefresh = False .TextFilePlatform = xlWindows .TextFileStartRow = 1 .TextFileParseType = xlDelimited .TextFileTextQualifier = xlTextQualifierDoubleQuote .TextFileConsecutiveDelimiter = False .TextFileTabDelimiter = False .TextFileSemicolonDelimiter = False .TextFileCommaDelimiter = True .TextFileSpaceDelimiter = False .TextFileColumnDataTypes = Array(1, 1, 2, 1, 1, 2, 1) .Refresh BackgroundQuery:=False End With End Sub So: You're using the QueryTables collection object. Go to VBA help and type that into the Answer Wizard box and read all about it. If your text file is always saved as a certain name - "MyData mmddyy.txt" (where mmddyy is a date identifier) into the same folder, then you can code your file path and name to get the file without using a dialogue box. Using the .TextFileColumnDataTypes = Array(1, 1, 2, 1, 1, 2, 1) line, you can specify if any columns are general, text, or numeric. I just used General (1) and Text (2). Before the End Sub, insert code to SaveAs with a file path and name, and you should be good to go. -----------<End Old Post----------- -- Regards, Tom Ogilvy " wrote: Is there any way I can open a text file in the workbook that the macros is running in. Everytime I try with my code it automatically opens a new workbook. I have looked at other comments for other people but none of them work because I have a datatype and fieldinfo. This is my code to open a txt file Dim UF As Variant Dim SL As Variant UF = Application.GetOpenFilename(FileFilter:="TXT Files(*.txt),*.txt", Title:="log") Workbooks.OpenText Filename:=UF, DataType:=xlFixedWidth, FieldInfo:=Array(Array(0, 2)) |
#5
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Ron,
That is a good reference for a delimited file, but in his sample code he showed xlFixedWidth So not sure it is applicable here. -- Regards, Tom Ogilvy "Ron de Bruin" wrote: Or use http://www.cpearson.com/excel/imptext.htm -- Regards Ron de Bruin http://www.rondebruin.nl/tips.htm "Tom Ogilvy" wrote in message ... Opentext places the textfile in a new workbook. It is trivial to then copy it to your existing workbook - that is the method I use. Alternately you can use querytable to do it directly. This is an old post that illustrates: -------------<old post----------------- Sub TestGetTxtFile() ' ' TestGetTxtFile Macro ' Macro recorded 10/7/2003 by Authorized User ' With ActiveSheet.QueryTables.Add(Connection:= _ "TEXT;C:\NWACCESS\DATA\TIR Updates\test.txt", Destination:=Range("A1")) .Name = "test" .FieldNames = True .RowNumbers = False .FillAdjacentFormulas = False .PreserveFormatting = True .RefreshOnFileOpen = False .RefreshStyle = xlInsertDeleteCells .SavePassword = False .SaveData = True .AdjustColumnWidth = True .RefreshPeriod = 0 .TextFilePromptOnRefresh = False .TextFilePlatform = xlWindows .TextFileStartRow = 1 .TextFileParseType = xlDelimited .TextFileTextQualifier = xlTextQualifierDoubleQuote .TextFileConsecutiveDelimiter = False .TextFileTabDelimiter = False .TextFileSemicolonDelimiter = False .TextFileCommaDelimiter = True .TextFileSpaceDelimiter = False .TextFileColumnDataTypes = Array(1, 1, 2, 1, 1, 2, 1) .Refresh BackgroundQuery:=False End With End Sub So: You're using the QueryTables collection object. Go to VBA help and type that into the Answer Wizard box and read all about it. If your text file is always saved as a certain name - "MyData mmddyy.txt" (where mmddyy is a date identifier) into the same folder, then you can code your file path and name to get the file without using a dialogue box. Using the .TextFileColumnDataTypes = Array(1, 1, 2, 1, 1, 2, 1) line, you can specify if any columns are general, text, or numeric. I just used General (1) and Text (2). Before the End Sub, insert code to SaveAs with a file path and name, and you should be good to go. -----------<End Old Post----------- -- Regards, Tom Ogilvy " wrote: Is there any way I can open a text file in the workbook that the macros is running in. Everytime I try with my code it automatically opens a new workbook. I have looked at other comments for other people but none of them work because I have a datatype and fieldinfo. This is my code to open a txt file Dim UF As Variant Dim SL As Variant UF = Application.GetOpenFilename(FileFilter:="TXT Files(*.txt),*.txt", Title:="log") Workbooks.OpenText Filename:=UF, DataType:=xlFixedWidth, FieldInfo:=Array(Array(0, 2)) |
#6
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Correct Tom
I am building a page on this moment to import a lot of txt files and use Chip's ImportTextFile in the loop in one of the examples. Testing opentext also but I think I go for the QueryTables because it is much faster if you want to import a lot of txt files Any thoughts ? -- Regards Ron de Bruin http://www.rondebruin.nl/tips.htm "Tom Ogilvy" wrote in message ... Ron, That is a good reference for a delimited file, but in his sample code he showed xlFixedWidth So not sure it is applicable here. -- Regards, Tom Ogilvy "Ron de Bruin" wrote: Or use http://www.cpearson.com/excel/imptext.htm -- Regards Ron de Bruin http://www.rondebruin.nl/tips.htm "Tom Ogilvy" wrote in message ... Opentext places the textfile in a new workbook. It is trivial to then copy it to your existing workbook - that is the method I use. Alternately you can use querytable to do it directly. This is an old post that illustrates: -------------<old post----------------- Sub TestGetTxtFile() ' ' TestGetTxtFile Macro ' Macro recorded 10/7/2003 by Authorized User ' With ActiveSheet.QueryTables.Add(Connection:= _ "TEXT;C:\NWACCESS\DATA\TIR Updates\test.txt", Destination:=Range("A1")) .Name = "test" .FieldNames = True .RowNumbers = False .FillAdjacentFormulas = False .PreserveFormatting = True .RefreshOnFileOpen = False .RefreshStyle = xlInsertDeleteCells .SavePassword = False .SaveData = True .AdjustColumnWidth = True .RefreshPeriod = 0 .TextFilePromptOnRefresh = False .TextFilePlatform = xlWindows .TextFileStartRow = 1 .TextFileParseType = xlDelimited .TextFileTextQualifier = xlTextQualifierDoubleQuote .TextFileConsecutiveDelimiter = False .TextFileTabDelimiter = False .TextFileSemicolonDelimiter = False .TextFileCommaDelimiter = True .TextFileSpaceDelimiter = False .TextFileColumnDataTypes = Array(1, 1, 2, 1, 1, 2, 1) .Refresh BackgroundQuery:=False End With End Sub So: You're using the QueryTables collection object. Go to VBA help and type that into the Answer Wizard box and read all about it. If your text file is always saved as a certain name - "MyData mmddyy.txt" (where mmddyy is a date identifier) into the same folder, then you can code your file path and name to get the file without using a dialogue box. Using the .TextFileColumnDataTypes = Array(1, 1, 2, 1, 1, 2, 1) line, you can specify if any columns are general, text, or numeric. I just used General (1) and Text (2). Before the End Sub, insert code to SaveAs with a file path and name, and you should be good to go. -----------<End Old Post----------- -- Regards, Tom Ogilvy " wrote: Is there any way I can open a text file in the workbook that the macros is running in. Everytime I try with my code it automatically opens a new workbook. I have looked at other comments for other people but none of them work because I have a datatype and fieldinfo. This is my code to open a txt file Dim UF As Variant Dim SL As Variant UF = Application.GetOpenFilename(FileFilter:="TXT Files(*.txt),*.txt", Title:="log") Workbooks.OpenText Filename:=UF, DataType:=xlFixedWidth, FieldInfo:=Array(Array(0, 2)) |
#7
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Here it is
http://www.rondebruin.nl/txtcsv.htm -- Regards Ron de Bruin http://www.rondebruin.nl/tips.htm "Ron de Bruin" wrote in message ... Correct Tom I am building a page on this moment to import a lot of txt files and use Chip's ImportTextFile in the loop in one of the examples. Testing opentext also but I think I go for the QueryTables because it is much faster if you want to import a lot of txt files Any thoughts ? -- Regards Ron de Bruin http://www.rondebruin.nl/tips.htm "Tom Ogilvy" wrote in message ... Ron, That is a good reference for a delimited file, but in his sample code he showed xlFixedWidth So not sure it is applicable here. -- Regards, Tom Ogilvy "Ron de Bruin" wrote: Or use http://www.cpearson.com/excel/imptext.htm -- Regards Ron de Bruin http://www.rondebruin.nl/tips.htm "Tom Ogilvy" wrote in message ... Opentext places the textfile in a new workbook. It is trivial to then copy it to your existing workbook - that is the method I use. Alternately you can use querytable to do it directly. This is an old post that illustrates: -------------<old post----------------- Sub TestGetTxtFile() ' ' TestGetTxtFile Macro ' Macro recorded 10/7/2003 by Authorized User ' With ActiveSheet.QueryTables.Add(Connection:= _ "TEXT;C:\NWACCESS\DATA\TIR Updates\test.txt", Destination:=Range("A1")) .Name = "test" .FieldNames = True .RowNumbers = False .FillAdjacentFormulas = False .PreserveFormatting = True .RefreshOnFileOpen = False .RefreshStyle = xlInsertDeleteCells .SavePassword = False .SaveData = True .AdjustColumnWidth = True .RefreshPeriod = 0 .TextFilePromptOnRefresh = False .TextFilePlatform = xlWindows .TextFileStartRow = 1 .TextFileParseType = xlDelimited .TextFileTextQualifier = xlTextQualifierDoubleQuote .TextFileConsecutiveDelimiter = False .TextFileTabDelimiter = False .TextFileSemicolonDelimiter = False .TextFileCommaDelimiter = True .TextFileSpaceDelimiter = False .TextFileColumnDataTypes = Array(1, 1, 2, 1, 1, 2, 1) .Refresh BackgroundQuery:=False End With End Sub So: You're using the QueryTables collection object. Go to VBA help and type that into the Answer Wizard box and read all about it. If your text file is always saved as a certain name - "MyData mmddyy.txt" (where mmddyy is a date identifier) into the same folder, then you can code your file path and name to get the file without using a dialogue box. Using the .TextFileColumnDataTypes = Array(1, 1, 2, 1, 1, 2, 1) line, you can specify if any columns are general, text, or numeric. I just used General (1) and Text (2). Before the End Sub, insert code to SaveAs with a file path and name, and you should be good to go. -----------<End Old Post----------- -- Regards, Tom Ogilvy " wrote: Is there any way I can open a text file in the workbook that the macros is running in. Everytime I try with my code it automatically opens a new workbook. I have looked at other comments for other people but none of them work because I have a datatype and fieldinfo. This is my code to open a txt file Dim UF As Variant Dim SL As Variant UF = Application.GetOpenFilename(FileFilter:="TXT Files(*.txt),*.txt", Title:="log") Workbooks.OpenText Filename:=UF, DataType:=xlFixedWidth, FieldInfo:=Array(Array(0, 2)) |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
opentext | Excel Programming | |||
OpenText | Excel Programming | |||
opentext | Excel Programming | |||
After OpenText all pastes from external apps use format from OpenText | Excel Programming | |||
Using Workbooks.OpenText to read a tab-delimited file into a worksheet | Excel Programming |