reading rep file in vba - vba

I'm trying to read a ".rep" file in VBA, here's the code I'm using:
FileNum = FreeFile()
Open FileName For Input As #FileNum
Line Input #FileNum, DataLine
LineItems = Split(DataLine, vbTab)
except that after the last code line, I have all of the file in "LineItems" (it becomes an array with 15K cells) and not just the first row (when I open the same file through Excel - I see it in the correct lines)
does anyone have an idea how to read it line-by-line?
Thank you

Try vbNewline instead?
LineItems = Split(DataLine, vbNewLine)
or this:
LineItems = Split(DataLine, vbCrLf)
Edit1: Ok try this then:
Dim LineItems() As String
Open Filename For Input As #Filenum
LineItems() = Split(Input$(LOF(Filenum), #Filenum), vbNewLine)
Close #Filenum

Related

How to read all information from shell command in VBA and save it in .txt file

I try to use this code:
Shell(nppPath & " " & fileToOpen, vbNormalFocus)
which opens txt file nppPath = "C:\Windows\notepad.exe" but I cannot read from it and save it as .txt file using VBA.
Looking forward to any suggestions.
I'm presuming you copied the nppPath over wrong as it should be
nppPath = "C:\Windows\System32\notepad.exe"
but for reading from the text file you could use free file:
Dim IntFile As Integer, StrFileName As String, StrFileContent As String
StrFileName = fileToOpen 'Your file location here
IntFile = FreeFile
Open StrFileName For Input As #IntFile
StrFileContent = Input(LOF(IntFile), IntFile) 'This now contains the text
Close #IntFile
This will read the text file into a single variable that you can then use however you wish.
If you want to write to a text file you can use the following code:
Dim StrNewLocation As String, StrFileContent As String
StrNewLocation = "" 'Put the new files name and location here
StrFileContent = "Example" 'The text to go into the new file
Open StrNewLocation For Output As #1
Write #1, StrFileContent
Close #1
Hopefully this helps you solve your problem if not send me a message and I'll see what I can do.

How to Convert .csv into .txt for Matlab Import

I have a .csv file. I want to convert it into .txt file starting from line the A to the B (A, B are declared at the beginning). The .txt file supposed to have gaps " " instead of original semicolons. Moreover, the end of each copied row from the original .csv file should be indicated by a semicolon in the new .txt file (txt file is going to be used as an input matrix for Matlab).
That's what I have till now, but it's not working
Sub csv2mat()
Dim Filename As String
Dim Filenamenew As String
'Change the path to the Files and and create a txt file
Filename = "C:\Documents and Settings\user\Desktop\as.csv"
Filenamenew = "C:\Documents and Settings\user\Desktop\new.txt"
Open Filenamenew For Output As #2
Open Filenamme For Input As #1
Do While Not EOF(1)
Line Input #1, str
str = Replace(str, ";", """")
Print #2, str
Loop
End Sub
You hade a few spelling mistakes like Filnemme with to m.
And you forgot to close the Files after Reading.
And you didn't add the ";" ad the and of the string if i understand you right
Try this:
Sub csv2mat()
Dim Filename As String
Dim Filenamenew As String
Dim str As String
'Change the path to the Files and and create a txt file
Filename = "C:\Documents and Settings\user\Desktop\as.csv"
Filenamenew = "C:\Documents and Settings\user\Desktop\new.txt"
Open Filenamenew For Output As #2
Open Filename For Input As #1
Do While Not EOF(1)
Line Input #1, str
str = Replace(str, ";", """") & ";"
Print #2, str
Loop
Close #1
Close #2
End Sub

VBA: Replace text from txt for each line and save as

Could somebody help me with the following? I tried making it on my own, but all I could do is open a txt and replace a static word with static word.
VBA script:
Open and Read first line of ThisVbaPath.WordsToUse.txt
Open and Find USER_INPUT in ThisVbaPath.BaseDoc.docx (or txt)
Replace all occurrence of USER_INPUT with first line from WordsToUse.txt
Save BaseDoc.docx as BaseDoc&First_Line.docx
Close all
Go next line and do same, but don't ask for user input, use previous one
If error go next
When done show if there were any errors (unlikely I guess)
I would use it about weekly for 150-ish lines.
Thanks!
I think something like this should work?
Sub test()
Dim text, textReplace, findMe As String
findMe = InputBox("What Text To Find?")
Open "C:\Excel Scripts\test.txt" For Input As #1
Open "C:\Excel Scripts\test2.txt" For Input As #2
Open "C:\Excel Scripts\output.txt" For Output As #3
While Not EOF(1)
Line Input #1, text
While Not EOF(2)
Line Input #2, textReplace
Write #3, Replace(text, findMe, textReplace)
Wend
Wend
Close #1
Close #2
Close #3
End Sub
Sub TextFile_FindReplace()
Dim TextFile As Integer
Dim FilePath As String
Dim FileContent As String
FilePath = Application.ActiveWorkbook.Path & "\NEWTEST.txt"
TextFile = FreeFile
Open FilePath For Input As TextFile
FileContent = Input(LOF(TextFile), TextFile)
FileContent = Replace(FileContent, "FOO", "BAR")
Print #TextFile, FileContent
Close TextFile
End Sub

How to remove the empty line that excel makes when creating a csv file using vba

As some of you probably know, excel creates an empty line at the end of CSV files. I'm looking for a solution that can remove/delete this line because I want to upload the CSV file to a different program, which can't handle this empty line.
First I thought it was the way I created the CSV file, but after spending hours searching for a solution, I found out that it's a bug.
Does anybody have a solution to this problem, removing the last line in a CSV file using VBA?
Try calling this Sub to kill the last line of the csv-file. You have to insert the path into the code:
Sub KillLastLine()
Dim fso As New FileSystemObject
Dim ts As TextStream
Dim filecontent As String
Dim myFile As File
Set myFile = fso.GetFile("YourCSVPathHere")
Set ts = myFile.OpenAsTextStream(ForReading)
While Not ts.AtEndOfStream
filecontent = filecontent & ts.ReadLine & vbCrLf
Wend
Set ts = myFile.OpenAsTextStream(ForWriting)
ts.Write Left(filecontent, Len(filecontent) - 1)
ts.Close
End Sub
Sub ZUtil_TextFile_FindReplace(FilePath As String, strOld As String, strNew As String)
'PURPOSE: Modify Contents of a text file using Find/Replace
'SOURCE: www.TheSpreadsheetGuru.com
Dim TextFile As Integer
Dim FileContent As String
'Determine the next file number available for use by the FileOpen function
TextFile = FreeFile
'Open the text file in a Read State
Open FilePath For Input As TextFile
'Store file content inside a variable
FileContent = Input(LOF(TextFile), TextFile)
'Clost Text File
Close TextFile
'Find/Replace
FileContent = Replace(FileContent, strOld, strNew)
FileContent = Replace(FileContent, "^(?:[\t ]*(?:\r?\n|\r))+", "")
If Right(FileContent, 2) = Chr(13) & Chr(10) Then
FileContent = Left(FileContent, Len(FileContent) - 2)
End If
'Determine the next file number available for use by the FileOpen function
TextFile = FreeFile
'Open the text file in a Write State
Open FilePath For Output As TextFile
'Write New Text data to file
Print #TextFile, FileContent
'Close Text File
Close TextFile
'MsgBox "ZUtil_TextFile_FindReplace TERMINADO"
End Sub

How to read a file and write into a text file?

I want to open mis file, copy all the data and write into a text file.
My mis file.
File name – 1.mis
M3;3395;44;0;1;;20090404;094144;8193;3;0;;;;
M3;3397;155;0;2;;20090404;105941;8193;3;0;;;;
M3;3396;160;0;1;;20090404;100825;8193;3;0;;;;
M3;3398;168;0;2;;20090404;110106;8193;3;0;;;;
so on...,
The above data should appear in a text file with same file name (1.txt).
I tried this code.
Dim sFileText As String
Dim iFileNo As Integer
iFileNo = FreeFile
Open "C:\Clients\Converter\Clockings.mis" For Input As #iFileNo
Do While Not EOF(iFileNo)
Input #iFileNo, sFileText
Loop
Close #iFileNo
Open "C:\Clients\Converter\2.txt" For Output As #iFileNo
Do While Not EOF(iFileNo)
Write #iFileNo, sFileText
Loop
Close #iFileNo
Nothing is saved in 1.txt.
It far easier to use the scripting runtime which is installed by default on Windows
Just go project Reference and check Microsoft Scripting Runtime and click OK.
Then you can use this code which is way better than the default file commands
Dim FSO As FileSystemObject
Dim TS As TextStream
Dim TempS As String
Dim Final As String
Set FSO = New FileSystemObject
Set TS = FSO.OpenTextFile("C:\Clients\Converter\Clockings.mis", ForReading)
'Use this for reading everything in one shot
Final = TS.ReadAll
'OR use this if you need to process each line
Do Until TS.AtEndOfStream
TempS = TS.ReadLine
Final = Final & TempS & vbCrLf
Loop
TS.Close
Set TS = FSO.OpenTextFile("C:\Clients\Converter\2.txt", ForWriting, True)
TS.Write Final
TS.Close
Set TS = Nothing
Set FSO = Nothing
As for what is wrong with your original code here you are reading each line of the text file.
Input #iFileNo, sFileText
Then here you write it out
Write #iFileNo, sFileText
sFileText is a string variable so what is happening is that each time you read, you just replace the content of sFileText with the content of the line you just read.
So when you go to write it out, all you are writing is the last line you read, which is probably a blank line.
Dim sFileText As String
Dim sFinal as String
Dim iFileNo As Integer
iFileNo = FreeFile
Open "C:\Clients\Converter\Clockings.mis" For Input As #iFileNo
Do While Not EOF(iFileNo)
Input #iFileNo, sFileText
sFinal = sFinal & sFileText & vbCRLF
Loop
Close #iFileNo
iFileNo = FreeFile 'Don't assume the last file number is free to use
Open "C:\Clients\Converter\2.txt" For Output As #iFileNo
Write #iFileNo, sFinal
Close #iFileNo
Note you don't need to do a loop to write. sFinal contains the complete text of the File ready to be written at one shot. Note that input reads a LINE at a time so each line appended to sFinal needs to have a CR and LF appended at the end to be written out correctly on a MS Windows system. Other operating system may just need a LF (Chr$(10)).
If you need to process the incoming data then you need to do something like this.
Dim sFileText As String
Dim sFinal as String
Dim vTemp as Variant
Dim iFileNo As Integer
Dim C as Collection
Dim R as Collection
Dim I as Long
Set C = New Collection
Set R = New Collection
iFileNo = FreeFile
Open "C:\Clients\Converter\Clockings.mis" For Input As #iFileNo
Do While Not EOF(iFileNo)
Input #iFileNo, sFileText
C.Add sFileText
Loop
Close #iFileNo
For Each vTemp in C
Process vTemp
Next sTemp
iFileNo = FreeFile
Open "C:\Clients\Converter\2.txt" For Output As #iFileNo
For Each vTemp in R
Write #iFileNo, vTemp & vbCRLF
Next sTemp
Close #iFileNo
If you want to do it line by line:
Dim sFileText As String
Dim iInputFile As Integer, iOutputFile as integer
iInputFile = FreeFile
Open "C:\Clients\Converter\Clockings.mis" For Input As #iInputFile
iOutputFile = FreeFile
Open "C:\Clients\Converter\2.txt" For Output As #iOutputFile
Do While Not EOF(iInputFile)
Line Input #iInputFile , sFileText
' sFileTextis a single line of the original file
' you can append anything to it before writing to the other file
Print #iOutputFile, sFileText
Loop
Close #iInputFile
Close #iOutputFile
FileCopy "1.mis", "1.txt"
An example of reading a file:
Dim sFileText as String
Dim iFileNo as Integer
iFileNo = FreeFile
'open the file for reading
Open "C:\Test.txt" For Input As #iFileNo
'change this filename to an existing file! (or run the example below first)
'read the file until we reach the end
Do While Not EOF(iFileNo)
Input #iFileNo, sFileText
'show the text (you will probably want to replace this line as appropriate to your program!)
MsgBox sFileText
Loop
'close the file (if you dont do this, you wont be able to open it again!)
Close #iFileNo
(note: an alternative to Input # is Line Input # , which reads whole lines).
An example of writing a file:
Dim sFileText as String
Dim iFileNo as Integer
iFileNo = FreeFile
'open the file for writing
Open "C:\Test.txt" For Output As #iFileNo
'please note, if this file already exists it will be overwritten!
'write some example text to the file
Print #iFileNo, "first line of text"
Print #iFileNo, " second line of text"
Print #iFileNo, "" 'blank line
Print #iFileNo, "some more text!"
'close the file (if you dont do this, you wont be able to open it again!)
Close #iFileNo
From Here