Simple Macro Required

Posted on 2006-03-28
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?
Question by:y2jk
    LVL 35

    Accepted Solution


    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


    Author Comment

    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.
    LVL 35

    Expert Comment

    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!

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
    This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
    Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now