Results 1 to 10 of 10
  1. #1
    4 Star Lounger
    Join Date
    Jan 2001
    Location
    Illinois
    Posts
    551
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Open notepad and delete character (VB6)

    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

  2. #2
    Bronze Lounger
    Join Date
    Nov 2001
    Location
    Arlington, Virginia, USA
    Posts
    1,394
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Open notepad and delete character (VB6)

    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:

    <code>Public Sub TestOpenTextFile()</code>
    <code></code>
    <code> OpenTextFile strPath:="C:Access", _</code>
    <code> strFile:="TextFile1.txt", _</code>
    <code> strNewFile:="TextFile2.txt", _</code>
    <code> strFind:="_", _</code>
    <code> strReplace:=vbNullString</code>
    <code>End Sub</code>
    <code></code>
    <code>Private Sub OpenTextFile(ByRef strPath As String, _</code>
    <code> ByRef strFile As String, _</code>
    <code> ByRef strNewFile As String, _</code>
    <code> ByRef strFind As String, _</code>
    <code> ByRef strReplace As String)</code>
    <code> On Error GoTo Err_Handler</code>
    <code> </code>
    <code> ' Requires reference to Scripting Runtime Library</code>
    <code> Dim fso As New Scripting.FileSystemObject</code>
    <code> Dim txt As Scripting.TextStream</code>
    <code> Dim strText As String</code>
    <code> Dim strMsg As String</code>
    <code> </code>
    <code> If fso.FileExists(strPath & "" & strFile) Then</code>
    <code> ' Open for reading:</code>
    <code> Set txt = fso.OpenTextFile(Filename:=strPath & "" & strFile, _</code>
    <code> IOMode:=ForReading, _</code>
    <code> Create:=False, _</code>
    <code> Format:=TristateUseDefault)</code>
    <code> ' Read whole file:</code>
    <code> strText = txt.ReadAll</code>
    <code> txt.Close</code>
    <code> Set txt = Nothing</code>
    <code> </code>
    <code> ' Open new file (delete existing file if any):</code>
    <code> If fso.FileExists(strPath & "" & strNewFile) Then</code>
    <code> fso.DeleteFile strPath & "" & strNewFile, True</code>
    <code> End If</code>
    <code> </code>
    <code> Set txt = fso.OpenTextFile(Filename:=strPath & "" & strNewFile, _</code>
    <code> IOMode:=ForAppending, _</code>
    <code> Create:=True, _</code>
    <code> Format:=TristateUseDefault)</code>
    <code> strText = Replace(strText, strFind, strReplace, , , vbBinaryCompare)</code>
    <code> txt.Write strText</code>
    <code> txt.Close</code>
    <code> </code>
    <code> ' Save new file over original:</code>
    <code> fso.CopyFile strPath & "" & strNewFile, strPath & "" & strFile, True</code>
    <code> fso.DeleteFile strPath & "" & strNewFile</code>
    <code> Else</code>
    <code> MsgBox "File not found.", vbExclamation, "FILE NOT FOUND"</code>
    <code> End If</code>
    <code> </code>
    <code>Exit_Sub:</code>
    <code> Set fso = Nothing</code>
    <code> Set txt = Nothing</code>
    <code> Exit Sub</code>
    <code>Err_Handler:</code>
    <code> strMsg = "Error No " & Err.Number & ": " & Err.Description</code>
    <code> MsgBox strMsg, vbExclamation, "OPEN TEXT FILE ERROR MSG"</code>
    <code> Resume Exit_Sub</code>
    <code>End Sub</code>

    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

  3. #3
    4 Star Lounger
    Join Date
    Aug 2002
    Location
    Dallas, Texas, USA
    Posts
    594
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Open notepad and delete character (VB6)

    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.

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 18 Times in 18 Posts

    Re: Open notepad and delete character (VB6)

    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 = "Cocuments 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

  5. #5
    4 Star Lounger
    Join Date
    Jan 2001
    Location
    Illinois
    Posts
    551
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Open notepad and delete character (VB6)

    thank u all very much

  6. #6
    4 Star Lounger
    Join Date
    Aug 2002
    Location
    Dallas, Texas, USA
    Posts
    594
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Open notepad and delete character (VB6)

    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!

  7. #7
    Super Moderator
    Join Date
    Dec 2000
    Location
    Salt Lake City, Utah, USA
    Posts
    9,507
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Re: Open notepad and delete character (VB6)

    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.
    -John ... I float in liquid gardens
    [acronym title="Gday mate!"][/acronym] [acronym title="What up Dude?"][/acronym] UTC -7±DS

  8. #8
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 18 Times in 18 Posts

    Re: Open notepad and delete character (VB6)

    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

  9. #9
    Super Moderator
    Join Date
    Dec 2000
    Location
    Salt Lake City, Utah, USA
    Posts
    9,507
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Re: Open notepad and delete character (VB6)

    <img src=/S/bow.gif border=0 alt=bow width=15 height=15> Thanks, Hans. I get it, and I have it working. <img src=/S/thumbup.gif border=0 alt=thumbup width=15 height=15>
    -John ... I float in liquid gardens
    [acronym title="Gday mate!"][/acronym] [acronym title="What up Dude?"][/acronym] UTC -7±DS

  10. #10
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    5,016
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Open notepad and delete character (VB6)

    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

Posting Permissions

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