logo
search
VBA & Macro Problems

How to Convert Word Tables and Paragraphs to HTML Using VBA

Huda QurayshiHuda Qurayshi Oct 15, 2026 871 views

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

How to Convert Word Tables and Paragraphs to HTML Using VBA
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.
Before you start

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.

Solution 1Recommended

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.

1
Open the VBA Editor

Press Alt + F11 on your keyboard to open the Visual Basic for Applications (VBA) editor.

2
Insert a New Module

From the top menu, click on Insert > Module to create a blank workspace for your macro script.

3
Draft the Macro Logic

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.

4
Run the Script

Press F5 or go to Run > Run Sub/UserForm to execute the code and convert the tables in your active document into HTML.

Write a Basic VBA Macro to Convert Tables to HTML
Handling Special Characters: Word automatically adds control characters (such as Chr(13) & Chr(7)) to the end of table cells. Ensure your script strips the last two characters from the cell range text to avoid weird symbols in your HTML.
Automate with WPS Office

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. 1. Enable Developer Tools: Open WPS Writer, navigate to the top ribbon, and click on the 'Developer' tab to access the macro environment.
  2. 2. Open the Visual Basic Editor: Click the 'Visual Basic' icon or press Alt + F11 to launch the built-in code editor.
  3. 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. 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.
Fully compatible with Microsoft Word VBA syntax and scriptsNatively supports .doc, .docx, and macro-enabled .docm formatsIncludes a built-in Visual Basic Editor (VBE) for easy script debuggingLightweight, fast, and highly cost-effective alternative
microsoft office alternative - wps office

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.