Simple Macro Required

I have a WORD document which is a 5 page long table of contents. I want to paste the contents into an html page. I DON'T want to do a "Save As Html" because that puts tons of formatting on the page, and makes it look like the original WORD document (I just want to paste the contents into an html page in keeping with the web site style).

I have done similar things with Excel, but trying to paste the WORD contents into Excel doesn't work either, due to the number of Carriage Returns in some of the cells.

What I want to be able to do is look for all carriage returns in the WORD document and replace them with some text , i.e. "<br>". I tried recording a macro so start me off, but I don't really understand what it's doing. The line that I think is doing what I need is:

    Selection.Delete Unit:=wdCharacter, Count:=1

I need to start from the top of the document, scroll all the way through it, and replace every instance of wdCharacter (I think!) with <br>.

I'm sure it's simple if you know how, although I'm offering points for a moderately difficult question (I like to be generous!). Can anyone help?
Who is Participating?
mvidasConnect With a Mentor Commented:

The following will replace all carriage returns with "<br>":

Sub y2jkSimpleMacro()
 'selects all the text, uncomment if you need it
' Selection.WholeStory

 Selection.Find.Execute FindText:=vbCr, ReplaceWith:="<br>", Replace:=wdReplaceAll
 'vbCr = carriage return, vbLF = line feed, vbCrLf = both
End Sub

However, the following will replace all CR's (and LFs if desired) and export text to an html file:

Sub y2jkSimpleMacro()
 Dim docText As String, vFF As Long, vFile As String, curSel As Range
 Set curSel = Selection.Range
 vFile = "C:\your exported file.html"
 docText = Selection.Text
 docText = Replace(docText, vbCr, "<br>") 'replace carriage returns
' docText = Replace(docText, vbLf, "<br>") 'to replace the line feeds too
 vFF = FreeFile
 Open vFile For Output As #vFF
 Print #vFF, docText
 Close #vFF
End Sub

y2jkAuthor Commented:
Perfect! I tried your first suggestion and it did exactly what I wanted. I may have a play with your longer suggestion another time, but for now it's done exactly what I needed. Thanks also for such a quick response.
Not a problem, the longer one just creates the html file for you.  Glad to help, and let me know if you need anything else!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.