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:
Other People Are Reading
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.
Type, in the dialogue box that appears, "ConvertToHTML" for the macro's name. Press "OK" to start recording the macro.
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.
End the macro recording by pressing the small square button in Word's status bar (at the bottom left of the screen).
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.
Open another document in Word, then press "Alt-F8" to display a list of macros.
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.
Double-click the new HTML document to display it in a browser, just as you did in step 5.
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.
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.
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.)
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.
- 20 of the funniest online reviews ever
- 14 Biggest lies people tell in online dating sites
- Hilarious things Google thinks you're trying to search for