We help IT Professionals succeed at work.

Simple Macro Required

y2jk asked
Medium Priority
Last Modified: 2011-09-20
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?
Watch Question


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


Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts


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!
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.