How to Format Text to HTML on VBA

Written by darrin koltow
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to Format Text to HTML on VBA
Visual Basic for Applications is the programming language used to augment Microsoft Word's built-in set of tools and functions. (binary world image by Attila Toro from

Visual Basic for Applications is the programming language used to augment Microsoft Word's built-in set of tools and functions. Developers use this language to write programs that quickly complete many tasks normally requiring significant time to complete. One such task is HTML formatting, which turns ordinary text into code that a Web browser can display. Besides saving time, using VBA to format HTML text can greatly reduce the tedium and number of errors that often accompany HTML coding by hand.

Skill level:


  1. 1

    Open a Word document with existing text, whether formatted or not. Click the "View" menu heading, then click the "Record Macro" function under the "Macros" panel.

  2. 2

    Type, in the dialogue box that appears, "ConvertToHTML" for the macro's name. Press "OK" to start recording the macro.

  3. 3

    Press "Control-S" to display the "Save As" dialogue box, then select the "Web Page" item in the "Save as type" field of the dialogue box. Type the name "VBAWebPage" for the file name. This step saves the document as a Web page, which means all text is converted to HTML. The macro recorder will translate this conversion into a VBA program that you can later replay to convert other documents into HTML pages.

  4. 4

    End the macro recording by pressing the small square button in Word's status bar (at the bottom left of the screen).

  5. 5

    Open Windows Explorer (by pressing "Start," then "Documents") and navigate to the folder containing the file you saved in step 3. Double-click the file to open it in a Web browser. The browser will display the former text document as a regular Web page. This step verifies that the conversion of text to HTML codes was successful.

  6. 6

    Open another document in Word, then press "Alt-F8" to display a list of macros.

  7. 7

    Double-click the macro labelled "ConvertToHTML," which you finished recording in step 4. Word will rerun the VBA code that saves the document as an HTML file.

  8. 8

    Double-click the new HTML document to display it in a browser, just as you did in step 5.

  9. 9

    Press "Alt-F8" in the Word document to list the available macros. Click the "ConvertToHTML" item, then click the "Edit" button to enter the Visual Basic integrated development environment (IDE). In this environment, you'll refine the code created by the macro recorder.

  10. 10

    Above the statement that begins with "ActiveDocument.SaveAs," type the following two lines:

    x = InputBox("Enter a name for the HTML page")

    x = x & ".htm"

    These lines will prompt you for a filename, which Word will assign to the output HTML file. Enabling different file names for each converted HTML document will prevent the problem of overwriting documents converted in a previous run of the macro.

  11. 11

    Type over the text "Filename:=<current filename>" with the text "FileName:=x." This step will make Word save the HTML document with the filename input by the user, which you provided the code for in the previous step. (The "<current filename>" indicates the document's filename prior to conversion.)

  12. 12

    Press "Alt-F11" to return to Word. Open a new document and run the "ConvertToHTML" macro. The macro will now prompt you for a file name. After entering that file name and running the macro, double-click the new HTML file, whose name you just entered, to open the file in your browser.

Don't Miss

  • All types
  • Articles
  • Slideshows
  • Videos
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the site, you consent to the use of cookies. For more information, please see our Cookie policy.