Consulting

Results 1 to 14 of 14

Thread: Solved: vb.6 code to read and edit particular line in notepad

  1. #1
    VBAX Contributor
    Joined
    May 2008
    Location
    bangalore
    Posts
    199
    Location

    Solved: vb.6 code to read and edit particular line in notepad

    i want to read line no 5 form notepad and edit it
    looking for vb.6 code

    fetching value from notepad end editing it

  2. #2
    Knowledge Base Approver VBAX Master Oorang's Avatar
    Joined
    Jan 2007
    Posts
    1,135
    Location
    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.

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,443
    Location
    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

  4. #4
    VBAX Contributor
    Joined
    May 2008
    Location
    bangalore
    Posts
    199
    Location
    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

  5. #5
    VBAX Contributor
    Joined
    May 2008
    Location
    bangalore
    Posts
    199
    Location
    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]

  6. #6
    Knowledge Base Approver VBAX Master Oorang's Avatar
    Joined
    Jan 2007
    Posts
    1,135
    Location
    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.

  7. #7

    edit notepad

    Quote Originally Posted by xld
    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]

    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..

  8. #8
    Knowledge Base Approver VBAX Master Oorang's Avatar
    Joined
    Jan 2007
    Posts
    1,135
    Location
    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.

  9. #9
    Quote Originally Posted by Oorang
    rofl... You need to update the path
    I had tried only after changing path mate!!!

    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.

  10. #10
    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....

  11. #11
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,443
    Location
    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

  12. #12
    Thanks a lot Xld!! got it working with minor modification....

  13. #13
    Knowledge Base Approver VBAX Master Oorang's Avatar
    Joined
    Jan 2007
    Posts
    1,135
    Location
    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.

  14. #14
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,443
    Location
    Quote Originally Posted by Oorang
    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.
    Can you tell that I have to work a lot with text files
    ____________________________________________
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •