Log in

View Full Version : Search and replace <tags> in html files using macros



er_warun
06-03-2016, 03:14 PM
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

Paul_Hossler
06-03-2016, 05:46 PM
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

er_warun
06-04-2016, 11:07 PM
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

er_warun
06-04-2016, 11:13 PM
Link to download htm file. see below

er_warun
06-04-2016, 11:14 PM
Please ignore the two messages. the forum did not allow me to share links unless i post/respond for 5 messages.

er_warun
06-04-2016, 11:16 PM
please ignore again. One more.

er_warun
06-04-2016, 11:16 PM
Please download the htm from below link.

https://drive.google.com/open?id=0B0Hv1jK_yxRqS1ljaFN3LXhEVFU (https://drive.google.com/open?id=0B0Hv1jK_yxRqS1ljaFN3LXhEVFU)

Paul_Hossler
06-09-2016, 10:49 AM
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

er_warun
06-10-2016, 07:19 AM
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

Paul_Hossler
06-16-2016, 07:31 AM
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