Consulting

Results 1 to 6 of 6

Thread: need help with regex

  1. #1
    VBAX Contributor
    Joined
    Jun 2014
    Posts
    114
    Location

    need help with regex

    Hello guys I found this code on the forum

    Could someone help me to add a regex expression


    instead of
    "change me" and "Changed!"
    I want to search for "><"

    and replace with ">\n\r<"

    see code below


    Sub CleanupXML()
    Dim FSO As Object '//FileSystemObject
    Dim ts(1) As Object '//TextStream
    Dim s As String, t As String
    Dim FileContents As String

    '---------------------------------------------------------
    Const SEARCH_FOR As String = "Change me" 'Search for ><
    Const REPLACE_WITH As String = "Changed!" 'replace with >\n\r<
    '---------------------------------------------------------

    On Error GoTo ErrHandler:
    s = Application.GetOpenFilename()
    If s <> "False" Then
    Set FSO = CreateObject("Scripting.FileSystemObject")
    If FSO.FileExists(s) Then

    '//Get File Contents
    t = FSO.GetParentFolderName(s) & "\" & Replace(FSO.GetTempName(), ".tmp", ".xml")
    Name s As t
    Set ts(0) = FSO.OpenTextFile(t, 1, False, -2) '//For reading, use default encoding
    FileContents = ts(0).ReadAll
    ts(0).Close
    Set ts(0) = Nothing

    '//Make replacement
    FileContents = Replace(FileContents, SEARCH_FOR, REPLACE_WITH)

    '//Write new file contents
    Set ts(1) = FSO.OpenTextFile(s, 2, True, -2) '//For writing, use default encoding
    ts(1).Write (FileContents)
    ts(1).Close
    Set ts(1) = Nothing

    '//Delete Temp file if all actions succeeded
    FSO.DeleteFile (t)

    End If
    End If

    '//Check that all files are closed
    My_Exit:
    If Not ts(0) Is Nothing Then
    ts(0).Close
    End If
    If Not ts(1) Is Nothing Then
    ts(1).Close
    End If
    Set FSO = Nothing
    Exit Sub

    ErrHandler:
    MsgBox "Error " & Err.Number & ": " & Err.Description
    Resume My_Exit
    End Sub


    Thank you in advance

  2. #2
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    This will suffice:

    Sub M_snb()
        Set fc = CreateObject("scripting.filesystemobject")
        
        With Application.FileDialog(1)
           If .Show <> False Then fc.opentextfile(.SelectedItems(1), ForWriting).write Replace(fc.opentextfile(.SelectedItems(1)).readall, "><", ">\\n\\r<")
        End With
    End Sub

    NB please use codetags around VBA-code

  3. #3
    VBAX Contributor
    Joined
    Jun 2014
    Posts
    114
    Location
    I get this error:

    Run-time error '5';

    Invalid producedure call or argument.

  4. #4
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    Where, which line? Please be specific in your feedback !

    Which file did you select ?

  5. #5
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    @snb

    '2' instead of 'ForWriting'?
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  6. #6
    VBAX Contributor
    Joined
    Jun 2014
    Posts
    114
    Location
    I did select an xml file

    vASycG4.jpg

Tags for this Thread

Posting Permissions

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