i want to read line no 5 form notepad and edit it
looking for vb.6 code
fetching value from notepad end editing it
i want to read line no 5 form notepad and edit it
looking for vb.6 code
fetching value from notepad end editing it
Note: Make sure you set the reference (or it won't work).
[vba]
Option Explicit
Public Sub Example()
MsgBox GetLineFromFile("C:\Test\MyFile.txt", 5)
End Sub
Public Function GetLineFromFile(ByVal filePath As String, ByVal line As Long) As String
'For these variables to work you must set a reference to scrrun.dll:
Dim fso As Scripting.FileSystemObject
Dim ts As Scripting.TextStream
Dim lngIndx As Long
On Error GoTo Err_Hnd
Set fso = New Scripting.FileSystemObject
Set ts = fso.OpenTextFile(filePath, ForReading, False)
For lngIndx = 1 To line - 1
ts.SkipLine
Next
Exit_Proc:
On Error Resume Next
GetLineFromFile = ts.ReadLine
ts.Close
Set ts = Nothing
Set fso = Nothing
Exit Function
Err_Hnd:
MsgBox Err.Description
Resume Exit_Proc
End Function
[/vba]
Cordially,
Aaron
Keep Our Board Clean!
- Please Mark your thread "Solved" if you get an acceptable response (under thread tools).
- Enclose your code in VBA tags then it will be formatted as per the VBIDE to improve readability.
Basic was invented in the days of text files, it comes naturally
[vba]
Const FileName As String = "c:\test\test.txt"
Dim FileNumber As Long
Dim myData As String
Dim counter As Long
FileNumber = FreeFile ' Get unused file
' number.
Open FileName For Input As #FileNumber
Do While Not EOF(FileNumber) And counter < 5
Line Input #FileNumber, myData
counter = counter + 1
Loop
If counter = 5 Then MsgBox myData
Close #FileNumber
[/vba]
____________________________________________
Nihil simul inventum est et perfectum
Abusus non tollit usum
Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
James Thurber
Hello OOrang
The code is capturing the required line from the notepad. I want to replace the selected line (marked in red colour) with new line from vb6.
[vba](defun c:dwin () (cWGINSERT)) ; Program Shortcut
(defun cWGINSERT (/ dwglist oldlay pt1)
(setq dwglist ("C:\\13027167-GX2.dwg";
))
(setvar "cmdecho" 0)
(setq oldlay (getvar "clayer"))
(if (not (tblsearch "LAYER" "DWGS"))
(command "-layer" "m" "DWGS" "C" "1" "DWGS" ""))
(foreach dwg dwglist
(if (and
(setq pt1 (getpoint "\nSpecify Insertion Point for Dwg."))
(vl-cmdf "-insert" dwg pt1 "" "" ""))
(princ (strcat "\n" dwg " Inserted."))
(alert "\nDrawing Not Found.")
) ; end if
) ; end foreach
(setvar "cmdecho" 1 )
(setvar "clayer" oldlay)
(princ)
)
[/vba]
Help me with the vb code.
Regards
Sham
i am able to replace [VBA]
Public Sub Example()
Text1.Text = GetLineFromFile("C:\lisp copy.lsp", 3)
End Sub
Public Function GetLineFromFile(ByVal filePath As String, ByVal line As Long) As String
'For these variables to work you must set a reference to scrrun.dll:
Dim fso As Scripting.FileSystemObject
Dim ts As Scripting.TextStream
Dim lngIndx As Long
On Error GoTo Err_Hnd
Set fso = New Scripting.FileSystemObject
Set ts = fso.OpenTextFile(filePath, ForReading, False)
For lngIndx = 1 To line - 1
ts.SkipLine
Next
Exit_Proc:
On Error Resume Next
GetLineFromFile = ts.ReadLine
ts.Close
Set ts = Nothing
Set fso = Nothing
Exit Function
Err_Hnd:
MsgBox Err.Description
Resume Exit_Proc
End Function
Private Sub Command1_Click()
Call SaveXMLInfo("(setq dwglist '(" & """" & App.Path & "\dwg\12-GX2.dwg"")")
End Sub
Private Sub SaveXMLInfo(strID As String)
Dim strOldXML As String, strLine As String, strNewXML As String
Dim lngFree As Long
strOldXML = "C:\lisp copy.lsp"
lngFree = FreeFile
Dim mystring As String
'Load the file into a temporary textbox (should be hidden to make the function faster)
Open strOldXML For Input As #lngFree
Do While Not EOF(lngFree)
Line Input #lngFree, strLine
mystring = mystring & strLine & vbCrLf
Loop
DoEvents
Close #lngFree
'Replace the needed text
mystring = Replace(mystring, Text1.Text, strID)
'Save the new file
strNewXML = "C:\lisp copy.lsp"
lngFree = FreeFile
Open strNewXML For Output As #lngFree
Print #lngFree, mystring
Close #lngFree
End Sub
[/VBA]
xld Thanks for the example, I usually only use open with Get/Put nice to see an Input example.
Cordially,
Aaron
Keep Our Board Clean!
- Please Mark your thread "Solved" if you get an acceptable response (under thread tools).
- Enclose your code in VBA tags then it will be formatted as per the VBIDE to improve readability.
Originally Posted by xld
I am a newbie... and I am trying to edit a notepad using vba..
I ried to run the above the code and got a 'file not found error'.. May be it is because I could not grasp the concept completely...please help me..
rofl... You need to update the path
Cordially,
Aaron
Keep Our Board Clean!
- Please Mark your thread "Solved" if you get an acceptable response (under thread tools).
- Enclose your code in VBA tags then it will be formatted as per the VBIDE to improve readability.
I had tried only after changing path mate!!!Originally Posted by Oorang
but i posted the same code in the forum so that it will be easy for 'xld ' to explain the concept...
Here is the actual code i tried....
Sub test()
Const FileName As String = "D:\dhananjayan\zimmer\test\trail_test.txt"
Dim FileNumber As Long
Dim myData As String
Dim counter As Long
FileNumber = FreeFile ' Get unused file
' number.
Open FileName For Input As #FileNumber
Do While Not EOF(FileNumber) And counter < 5
Line Input #FileNumber, myData
counter = counter + 1
Loop
If counter = 5 Then MsgBox myData
Close #FileNumber
End Sub
Note: trail_test.txt is a proE(cad tool that i am automating) generated test file in proE versioned format...
please help..
here is the sample of what i need to do...
But please give me sample code with funtions for
1. open a txt file.
2.Delete contents
3.write new lines
4. save and close
Eg.
input: test.txt
vbax
is
a
great forum
Output: test.txt
I
am
learning
a lot
here
Last edited by dhananjay; 12-15-2008 at 08:09 PM.
Hi friends!!! I have got the concept..
But the saved txt file is not in proE version file..
Is there a funtion to save the file in same format as that it is being opened...Please .. i am racing against time....
Is this the sort of thing you were after?
[vba]
Sub WriteUTF8()
Const inFileName As String = "C:\test\test.txt" '"D:\dhananjayan\zimmer\test\trail_test.txt"
Const outFileName As String = "C:\test\test2.txt"
With CreateObject("ADODB.Stream")
.Open
.LoadFromFile inFileName
.Type = 2
.Charset = "UTF-8"
.ReadText 'read past all existing data
.Writetext "Here is another line" & vbCrLf
.SaveToFile outFileName, 2 ' adSaveCreateOverWrite
.Close
End With
End Sub
[/vba]
____________________________________________
Nihil simul inventum est et perfectum
Abusus non tollit usum
Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
James Thurber
Thanks a lot Xld!! got it working with minor modification....
Wow XLD, I use ADO all the time and never once noticed that it had a stream object. I have a strong desire to go back through old databases and replace procedures using the Scripting Runtime.
dhananjay - Just as an additional piece of info. You can make xld's method work with intellisense (early binding) by setting a reference to "Microsoft Active X 2.8 Data Objects Library" (C:\Program Files\Common Files\System\msado15.dll). The early bound version would look like this:
[vba]Sub WriteUTF8()
Const inFileName As String = "C:\test\test.txt" '"D:\dhananjayan\zimmer\test\trail_test.txt"
Const outFileName As String = "C:\test\test2.txt"
Dim asMyFile As ADODB.Stream
Set asMyFile = New ADODB.Stream
With asMyFile
.Open
.LoadFromFile inFileName
.Type = 2
.Charset = "UTF-8"
.ReadText 'read past all existing data
.WriteText "Here is another line" & vbCrLf
.SaveToFile outFileName, 2 ' adSaveCreateOverWrite
.Close
End With
End Sub[/vba]
It's not necessary at all to use early binding, it's just my personal preference to have intellisense.
Cordially,
Aaron
Keep Our Board Clean!
- Please Mark your thread "Solved" if you get an acceptable response (under thread tools).
- Enclose your code in VBA tags then it will be formatted as per the VBIDE to improve readability.
Can you tell that I have to work a lot with text filesOriginally Posted by Oorang
____________________________________________
Nihil simul inventum est et perfectum
Abusus non tollit usum
Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
James Thurber