Consulting

Results 1 to 10 of 10

Thread: Search and replace <tags> in html files using macros

  1. #1

    Search and replace <tags> in html files using macros

    Dear All,
    Can one of you please help me in identifying the issue? I want to replace all <br> tags with <br><p></p> tags in a htm file. I used below code. It seems to work for .txt files but not for .htm

    Any help is highly appreciated.

    Sub try4()
    Dim vFF As Integer, TempStr As String
    vFF = FreeFile
    Open "C:\Temp\Sample.htm" For Binary As vFF


    TempStr = Space$(LOF(vFF))
    Get #vFF, , TempStr
    Close vFF
    TempStr = Replace(TempStr, "<br>", "<p></p>")
    Open "C:\Temp\Sample.htm" For Output As vFF
    Print #vFF, TempStr
    Close vFF


    End Sub

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,711
    Location
    1. Are you sure you don't want <br/> ?

    2. Instead of 'Print' try 'Put'

    If that doesn't help, then post a small sample htm file
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    Thanks Paul. Put command throws an error for me. I am attaching a sample htm file. Please help. I just want to replace all <br> tags with <br><p></p> tags

  4. #4
    Link to download htm file. see below

  5. #5
    Please ignore the two messages. the forum did not allow me to share links unless i post/respond for 5 messages.

  6. #6
    please ignore again. One more.

  7. #7

  8. #8
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,711
    Location
    This works for me


    Option Explicit
    Sub try5()
     
     Dim vFF As Integer, TempStr As String
     
     vFF = FreeFile
     
     Open "C:\users\daddy\downloads\Sample.htm" For Binary As vFF
     TempStr = Space$(LOF(vFF))
     Get #vFF, , TempStr
     Close vFF
     
     MsgBox Len(TempStr)
     TempStr = Replace(TempStr, "<br>", "<p></p>")
     MsgBox Len(TempStr)
     
     Open "C:\users\daddy\downloads\Sample.htm" For Binary As vFF
     Put #vFF, , TempStr
     Close vFF
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  9. #9
    Thanks heaps Paul. I tried your script. It created a new html file But with the same content.
    <br> was not replaced by <br><p></p>. I was able to find another method to do search and replace in html files. I opened them in notepad and "sendkey"ed my requirement.

    This is the macro i wrote.

    sourceHtm = Dir(SourcePath & "*.htm")
    If sourceHtm <> "" Then
    RetVal = Shell("NOTEPAD.exe " & SourcePath & sourceHtm, 1)
    AppActivate RetVal
    SendKeys "^h", True
    SendKeys "<br>", True
    SendKeys "{TAB}", True
    SendKeys "<br><p></p>", True
    SendKeys "%a", True
    SendKeys "%{F4}", True
    SendKeys "%fs", True
    SendKeys "%fx", True
    End If

  10. #10
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,711
    Location
    Your code in #1

    TempStr = Replace(TempStr, "<br>", "<p></p>")
    just replaced a break with an empty paragraph, so that's what my macro did in #8


    Your SendKeys in #9 is now replacing a break with a break+empty paragraph


    Depending on what it is you really want to do, this macro will replace a break with a break+empty paragraph

    The only change was changing the 'Replace With' parameter


    Option Explicit
    Sub try6()
         
        Dim vFF As Integer, TempStr As String
         
        vFF = FreeFile
         
        Open "C:\users\userid\desktop\Sample.htm" For Binary As vFF
        TempStr = Space$(LOF(vFF))
        Get #vFF, , TempStr
        Close vFF
         
        MsgBox Len(TempStr)
        TempStr = Replace(TempStr, "<br>", "<br><p></p>")
        MsgBox Len(TempStr)
         
        Open "C:\users\userid\desktop\Sample_1.htm" For Binary As vFF
        Put #vFF, , TempStr
        Close vFF
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

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
  •