• Open notepad and delete character (VB6)

    Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » Open notepad and delete character (VB6)

    Author
    Topic
    #406625

    I have a vb6 project. I need to open a text file in notepad and delete all the underscore characters in it. I found this code to open notepad and open a text file but how do i search through it and delete all the underscore characters. Thank you for the help.

    Shell “Notepad.exe” & ” ” & “c:Temp.txt”, vbMaximizedFocus

    Viewing 4 reply threads
    Author
    Replies
    • #844635

      Don’t think you can “automate” Notepad, it’s not an Automation server, an alternative option would be to use the Scripting FileSystemObject and TextStream objects. Sample code:

      Public Sub TestOpenTextFile()

      OpenTextFile strPath:="C:Access", _
      strFile:="TextFile1.txt", _
      strNewFile:="TextFile2.txt", _
      strFind:="_", _
      strReplace:=vbNullString
      End Sub

      Private Sub OpenTextFile(ByRef strPath As String, _
      ByRef strFile As String, _
      ByRef strNewFile As String, _
      ByRef strFind As String, _
      ByRef strReplace As String)
      On Error GoTo Err_Handler

      ' Requires reference to Scripting Runtime Library
      Dim fso As New Scripting.FileSystemObject
      Dim txt As Scripting.TextStream
      Dim strText As String
      Dim strMsg As String

      If fso.FileExists(strPath & "" & strFile) Then
      ' Open for reading:
      Set txt = fso.OpenTextFile(Filename:=strPath & "" & strFile, _
      IOMode:=ForReading, _
      Create:=False, _
      Format:=TristateUseDefault)
      ' Read whole file:
      strText = txt.ReadAll
      txt.Close
      Set txt = Nothing

      ' Open new file (delete existing file if any):
      If fso.FileExists(strPath & "" & strNewFile) Then
      fso.DeleteFile strPath & "" & strNewFile, True
      End If

      Set txt = fso.OpenTextFile(Filename:=strPath & "" & strNewFile, _
      IOMode:=ForAppending, _
      Create:=True, _
      Format:=TristateUseDefault)
      strText = Replace(strText, strFind, strReplace, , , vbBinaryCompare)
      txt.Write strText
      txt.Close

      ' Save new file over original:
      fso.CopyFile strPath & "" & strNewFile, strPath & "" & strFile, True
      fso.DeleteFile strPath & "" & strNewFile
      Else
      MsgBox "File not found.", vbExclamation, "FILE NOT FOUND"
      End If

      Exit_Sub:
      Set fso = Nothing
      Set txt = Nothing
      Exit Sub
      Err_Handler:
      strMsg = "Error No " & Err.Number & ": " & Err.Description
      MsgBox strMsg, vbExclamation, "OPEN TEXT FILE ERROR MSG"
      Resume Exit_Sub
      End Sub

      Note: Requires reference to Microsoft Scripting Runtime library (SCRRUN.DLL). To delete character, simply specify a null string (vbNullString or “”) for the strReplace argument, as illustrated. Modify code as required. For more info, look up FileSystemObject and TextStream objects in VB/VBA Help.

      HTH

    • #844636

      Don’t think you can “automate” Notepad, it’s not an Automation server, an alternative option would be to use the Scripting FileSystemObject and TextStream objects. Sample code:

      Public Sub TestOpenTextFile()

      OpenTextFile strPath:="C:Access", _
      strFile:="TextFile1.txt", _
      strNewFile:="TextFile2.txt", _
      strFind:="_", _
      strReplace:=vbNullString
      End Sub

      Private Sub OpenTextFile(ByRef strPath As String, _
      ByRef strFile As String, _
      ByRef strNewFile As String, _
      ByRef strFind As String, _
      ByRef strReplace As String)
      On Error GoTo Err_Handler

      ' Requires reference to Scripting Runtime Library
      Dim fso As New Scripting.FileSystemObject
      Dim txt As Scripting.TextStream
      Dim strText As String
      Dim strMsg As String

      If fso.FileExists(strPath & "" & strFile) Then
      ' Open for reading:
      Set txt = fso.OpenTextFile(Filename:=strPath & "" & strFile, _
      IOMode:=ForReading, _
      Create:=False, _
      Format:=TristateUseDefault)
      ' Read whole file:
      strText = txt.ReadAll
      txt.Close
      Set txt = Nothing

      ' Open new file (delete existing file if any):
      If fso.FileExists(strPath & "" & strNewFile) Then
      fso.DeleteFile strPath & "" & strNewFile, True
      End If

      Set txt = fso.OpenTextFile(Filename:=strPath & "" & strNewFile, _
      IOMode:=ForAppending, _
      Create:=True, _
      Format:=TristateUseDefault)
      strText = Replace(strText, strFind, strReplace, , , vbBinaryCompare)
      txt.Write strText
      txt.Close

      ' Save new file over original:
      fso.CopyFile strPath & "" & strNewFile, strPath & "" & strFile, True
      fso.DeleteFile strPath & "" & strNewFile
      Else
      MsgBox "File not found.", vbExclamation, "FILE NOT FOUND"
      End If

      Exit_Sub:
      Set fso = Nothing
      Set txt = Nothing
      Exit Sub
      Err_Handler:
      strMsg = "Error No " & Err.Number & ": " & Err.Description
      MsgBox strMsg, vbExclamation, "OPEN TEXT FILE ERROR MSG"
      Resume Exit_Sub
      End Sub

      Note: Requires reference to Microsoft Scripting Runtime library (SCRRUN.DLL). To delete character, simply specify a null string (vbNullString or “”) for the strReplace argument, as illustrated. Modify code as required. For more info, look up FileSystemObject and TextStream objects in VB/VBA Help.

      HTH

    • #845023

      This will do what you want, without Notepad, and is a little shorter then Mark’s code.

      Dim f As Long
      Dim strPath as String
      Dim strTemp as String
      strPath=”C:Temp.txt”
      f=freefile
      Open strPath for Binary Access Read as f
      strTemp=space(lof(f))
      close f
      kill strPath
      strTemp=Replace(strTemp,”_”,””)
      f=freefile
      Open strPath for Binary Access Write as f
      put f,,strTemp
      close f

      And that would do it.

      • #845061

        Drew,

        You never read the contents of the file, so the string strTemp will consist of a lot of spaces. You write this string into the new file, effectively ruining it. You need to add one line to the code:

        Dim f As Long
        Dim strPath As String
        Dim strTemp As String
        strPath = “C:Documents and SettingsHansMijn documentenWord-bestandenTest.txt”
        f = FreeFile
        Open strPath For Binary Access Read As #f
        strTemp = Space(LOF(f))
        Get #f, , strTemp
        Close #f
        Kill strPath
        strTemp = Replace(strTemp, “_”, “”)
        f = FreeFile
        Open strPath For Binary Access Write As #f
        Put #f, , strTemp
        Close #f

        • #845105

          thank u all very much

        • #845106

          thank u all very much

        • #845128

          LOL, you’re absolutely right. That’s what I get for writing code in the lounge, I skipped a step, and didn’t debug it! Oh Well, no one is perfect!

        • #845129

          LOL, you’re absolutely right. That’s what I get for writing code in the lounge, I skipped a step, and didn’t debug it! Oh Well, no one is perfect!

        • #925965

          I’m not too familiar with these methods. Is there a quick and dirty way to modify this code to read line by line rather than replace on the entire text file content?

          The problem I’m looking to resolve is that when I save certain files from PDF to text, tables of numbers which only contain spaces and number 0 – 9 show in the text file like this, and import to Excel fine:

          99 98 101 3

          but as soon as they run into thousands and a comma is included in the line, the line gets quoted as if it were text:

          “99 1,321 101 3″

          and after

          Workbooks.OpenText Filename:=strFileName, Origin:=xlWindows, _
          StartRow:=1, DataType:=xlFixedWidth, OtherChar:=False, _
          FieldInfo:=Array(Array(0, 1), Array(32767, 1)), _
          DecimalSeparator:=”.”, ThousandsSeparator:=”,”, TrailingMinusNumbers:=True

          it is in turn munging my TextToColumns operation; they don’t work on the quoted lines. I have modified the above code to strip out all quote charcters, but that messes with some other parts of the text file content. TIA for any advice.

          • #926012

            The code in the reply you refer to reads a binary file. To read a text file, you can open it for Input and read it line by line using Line Input:

            Dim f As Long
            Dim strTemp As String
            f = FreeFile

            ‘ Open the file
            Open “C:Test.txt” For Input As #f

            ‘ Loop through the file
            Do While Not EOF(f)
            ‘ Read a line
            Line Input #f, strTemp
            ‘ Do something with strTemp
            Debug.Print strTemp
            Loop

            ‘ Close the file
            Close #f

      • #845062

        Drew,

        You never read the contents of the file, so the string strTemp will consist of a lot of spaces. You write this string into the new file, effectively ruining it. You need to add one line to the code:

        Dim f As Long
        Dim strPath As String
        Dim strTemp As String
        strPath = “C:Documents and SettingsHansMijn documentenWord-bestandenTest.txt”
        f = FreeFile
        Open strPath For Binary Access Read As #f
        strTemp = Space(LOF(f))
        Get #f, , strTemp
        Close #f
        Kill strPath
        strTemp = Replace(strTemp, “_”, “”)
        f = FreeFile
        Open strPath For Binary Access Write As #f
        Put #f, , strTemp
        Close #f

    • #845024

      This will do what you want, without Notepad, and is a little shorter then Mark’s code.

      Dim f As Long
      Dim strPath as String
      Dim strTemp as String
      strPath=”C:Temp.txt”
      f=freefile
      Open strPath for Binary Access Read as f
      strTemp=space(lof(f))
      close f
      kill strPath
      strTemp=Replace(strTemp,”_”,””)
      f=freefile
      Open strPath for Binary Access Write as f
      put f,,strTemp
      close f

      And that would do it.

    • #926377

      If you want to achieve this using a single command line, like in your script, you could use one of the many little purpose-built DOS utilities to shell to. For instance alter (at only 7KB) could be used in your Shell command as (something like):

      command /c alter c:temp.txt “_” “”

      Alan

    Viewing 4 reply threads
    Reply To: Open notepad and delete character (VB6)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: