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?
y2jkAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mvidasCommented:
y2jk,

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"
 Selection.WholeStory
 docText = Selection.Text
 curSel.Select
 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

Matt
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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.
0
mvidasCommented:
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!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.