+ Reply to Thread
Results 1 to 6 of 6

Open Notepad and run find/replace

  1. #1
    Forum Contributor
    Join Date
    07-18-2012
    Location
    New York
    MS-Off Ver
    2016
    Posts
    192

    Open Notepad and run find/replace

    Hello,

    I have the below code merging multiple .txt documents into one and then resaving it as a new .txt. We've run into an issue where quotation marks seem to be appearing at random in the new .txt document. When I walk through the macro in Excel there aren't any quotes so I'm assuming this has something to do with saving the file from excel to .txt. One possible fix that I can think of is to reopen the new file in notepad and do a find/replace to clear out the quotation marks and then save the file. I'd like to be able to add this step to the end of my macro so a browser window opens and lets the user select the file. Any help would be greatly appreciated.


    Sub mergebooks1sheetsaveasTXT()
    '
    Dim varFilenames As Variant
    Dim strActiveBook As String
    Dim strSourceDataFile As String
    Dim wSht As Worksheet
    Dim allwShts As Sheets
    Dim intResponse As Integer
    Dim counter As Integer
    Dim lRows As Long
    Dim varResult As Variant
    '
    intResponse = MsgBox("This macro will combine all data from all worksheets" & vbCrLf & "from all selected files to a single worksheet in a new workbook. Continue?", vbOKCancel, "Combine Files")
    If intResponse = vbOK Then
    Workbooks.Add
    strActiveBook = ActiveWorkbook.Name
    ' Create array of filenames; the True is for multi-select
    On Error GoTo exitsub
    varFilenames = Application.GetOpenFilename(, , , , True)

    counter = 1

    ' ubound determines how many items in the array
    On Error GoTo quit
    Application.ScreenUpdating = False
    While counter <= UBound(varFilenames)

    'Opens the selected files
    Workbooks.Open varFilenames(counter)
    strSourceDataFile = ActiveWorkbook.Name

    Set allwShts = Worksheets
    For Each wSht In allwShts
    ' Select Entire UsedRange from Source File
    wSht.Activate
    ActiveSheet.UsedRange.Select
    Selection.Copy

    ' Find end of usedrange in destination file
    Workbooks(strActiveBook).Activate
    Range("A1").Select
    ActiveSheet.UsedRange.Select
    lRows = Selection.Rows.Count
    ActiveCell.Offset(lRows, 0).Select

    ' Copy & Paste All including Formatting
    Selection.PasteSpecial Paste:=xlAll, Operation:=xlNone, SkipBlanks:= _
    False, Transpose:=False
    Range("A1").Select

    Next wSht
    Workbooks(strSourceDataFile).Activate
    Application.DisplayAlerts = False
    ActiveWorkbook.Close
    Application.DisplayAlerts = True

    ' displays file name in a message box
    MsgBox varFilenames(counter) & " Has Been Processed", vbOKOnly + vbInformation, "File Processed"

    'increment counter
    counter = counter + 1

    Wend

    quit:
    If Err <> 0 Then
    MsgBox "An Error Occurred Trying to open the File. Please close any open Excel files and try again", vbOKOnly + vbExclamation, "File Open Error"
    On Error GoTo 0
    End If
    End If
    exitsub:
    On Error GoTo 0
    Application.ScreenUpdating = True



    Rows("1:1").Select
    Selection.Delete Shift:=xlUp

    'Retrieve file name to use for Save
    fileSaveName = Application.GetSaveAsFilename( _
    fileFilter:="Text Files (*.txt), *.txt")

    'If user specified file name, perform Save and display msgbox
    If fileSaveName <> False Then
    ActiveWorkbook.SaveAs Filename:=fileSaveName, FileFormat:=xlText

    MsgBox "Save as " & fileSaveName
    End If

    ActiveWorkbook.Close False
    ActiveWorkbook.Close False


    End Sub

  2. #2
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Open Notepad and run find/replace

    Hi,

    You can try to use the sendkeys method, but this is known buggy and it is preferred to use other way if available.
    But if you want to try to play with it, this is the code :

    Please Login or Register  to view this content.
    ^ means hold and press Control key, so ^h means press Ctrl h
    % means hold and press Alt key, so %f means press Alt A
    Chr(34) is doublequote character

    Regards
    1. I care dog
    2. I am a loop maniac
    3. Forum rules link : Click here
    3.33. Don't forget to mark the thread as solved, this is important

  3. #3
    Forum Contributor
    Join Date
    07-18-2012
    Location
    New York
    MS-Off Ver
    2016
    Posts
    192

    Re: Open Notepad and run find/replace

    That worked perfectly, thanks

  4. #4
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: Open Notepad and run find/replace

    As mentioned, SendKeys; while not buggy - it works perfectly, is unreliable.

    If another application brings its window to the foreground, that's where the keystrokes are sent.

    An alternative. This is written as a self contained function you can copy to a module.
    Please Login or Register  to view this content.
    The procedure takes 3 parameters:
    The fully qualified file name
    The Character to replace
    The replacement character - this is optional. If not specified then a null string is assumed. This effectively removes the character

    To call it, use
    Please Login or Register  to view this content.
    The Chr(34) is the ASCII code for a double quote. I find that easier than trying to figure out multiple """'s to try and pass a " to a function...
    As mentioned in the code comments, there are other ways to do this - but this, in my opinion, is the simplest and possibly the fastest. It does 100,000 replaces in a 100mb file in about 20 seconds.

    Just offered as an alternative - I detest SendKeys
    Last edited by cytop; 12-16-2014 at 03:22 PM. Reason: Typo

  5. #5
    Forum Contributor
    Join Date
    07-18-2012
    Location
    New York
    MS-Off Ver
    2016
    Posts
    192

    Re: Open Notepad and run find/replace

    Thanks, the occurrence is extremely small and it's also very unlikely that anything else should come up once the macro is run, when I tested it I would have missed it if I blinked. I'll keep this in mind if anything should change though.

  6. #6
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Open Notepad and run find/replace

    Quote Originally Posted by trisoldee View Post
    That worked perfectly, thanks
    You are welcome, and many thanks for the reputation points.

    Regards

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Find and Replace keeps trying to open files
    By tms12345 in forum Excel General
    Replies: 5
    Last Post: 12-17-2021, 03:09 AM
  2. Find & Replace to open different by default
    By Uni2023 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-15-2013, 10:33 PM
  3. Find and Replace Code in VBE on Open
    By Excelnoub in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 06-11-2013, 12:14 PM
  4. Macro to open word doc, then open another file in word and then excute a find replace
    By Uber-Smee in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-29-2010, 09:43 AM
  5. [SOLVED] Open word then find replace text
    By Shawn G. in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-04-2005, 09:05 AM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1