How to Convert Word Tables and Paragraphs to HTML Using VBA
Question details
The user wants to use a VBA macro in Word to process document content by identifying tables and paragraphs and wrapping them in standard HTML tags like <table>, <tr>, <td>, and <p>.

- Product
- Word Processing Software
- Device & OS
- not provided
- Scenario
- Automating the conversion of document text and table structures into raw HTML code using a VBA script.
- Observed behavior
- Basic VBA approaches succeed on simple tables but produce incorrect row numbers and malformed cells when multiple paragraphs or empty cells exist inside a single table cell.
Ensure you have the Developer tab enabled in your word processor and save a backup copy of your document before running any VBA macros to prevent accidental data loss during the conversion.
Write a Basic VBA Macro to Convert Tables to HTML
Use this solution to create a standard macro that iterates through tables, rows, and cells to wrap content in HTML tags. This is ideal for simple tables containing one paragraph per cell.
A basic VBA script works by using nested loops. It loops through the tables in the ActiveDocument, then rows, and finally cells, inserting opening and closing HTML tags around the extracted text.
Press Alt + F11 on your keyboard to open the Visual Basic for Applications (VBA) editor.
From the top menu, click on Insert > Module to create a blank workspace for your macro script.
Write a script utilizing 'For Each tbl In ActiveDocument.Tables' to target the table, and 'For Each rw In tbl.Rows' to loop through rows. Append '<tr>' and '<td>' string tags before the cell text, and their closing counterparts after.
Press F5 or go to Run > Run Sub/UserForm to execute the code and convert the tables in your active document into HTML.

Modify the VBA Script for Multiple Paragraphs per Cell
Implement nested paragraph logic to accurately process table cells that contain multiple paragraphs, empty lines, or complex text structures without breaking the HTML layout.
Run VBA Macros Natively in WPS Writer
WPS Office offers comprehensive support for VBA macros in its advanced versions, allowing you to easily run, edit, and debug scripts to automate document-to-HTML conversions with seamless compatibility.
- 1. Enable Developer Tools: Open WPS Writer, navigate to the top ribbon, and click on the 'Developer' tab to access the macro environment.
- 2. Open the Visual Basic Editor: Click the 'Visual Basic' icon or press Alt + F11 to launch the built-in code editor.
- 3. Execute Your Script: Paste your customized Table-to-HTML VBA code into a new module and hit Run to automatically format your document.
- 4. Save as Macro-Enabled Document: Go to Menu > Save As, and choose the '.docm' format to safely preserve your VBA scripts for future conversions.

Frequently Asked Questions
Why does my VBA code output weird squares at the end of table cells?
Word table cells end with an invisible end-of-cell marker (ASCII character 13 followed by 7). Your VBA script needs to remove the last two characters of the cell's range text (using the Left() function) before appending HTML tags to prevent these symbols from appearing in the output.
Can I convert formatting like bold and italics to HTML tags using VBA?
Yes, but it requires additional logic. Instead of just grabbing the plain text of a paragraph, you must loop through the Characters or Words collection within the cell, check properties like .Font.Bold or .Font.Italic, and dynamically wrap those specific ranges in <b> or <i> tags.
Will this basic VBA macro convert images inside the table to HTML?
No, a standard text-to-HTML macro only processes strings. To handle images, your VBA code needs to iterate through the InlineShapes collection in the document, save the images to a local folder using a separate export function, and insert an <img> tag with the correct local file path.




