need to read an Word table and convert it to an HTML table

C

Cal Who

I can't find an example that I can adapt.

I need to take a document that contains only a table and write an HTML table
in a new document.

That is, if the document is has two columns in the table I need to produce
another document:

<tr><td>CellR1C1</td><td>CellR1C2</td></tr>
<tr><td>CellR2C1</td><td>CellR2C2</td></tr>
<tr><td>CellR3C1</td><td>CellR3C2</td></tr>

where CellRxCy is the data in table cell row x column y

Can you point to an example that does something like that?

Thanks
 
P

Pesach Shelnitz

Hi,

The following macro should do what you want.

Sub ConvertTableToHtml()

Dim myString As String
Dim myRange As Range
Dim newDoc As Document
Dim i As Integer
Dim j As Integer

myString = "<table>" & vbCrLf
With ActiveDocument.Tables(1)
For i = 1 To .Rows.Count
myString = myString & "<tr>"
For j = 1 To .Rows(i).Cells.Count
Set myRange = .Rows(i).Cells(j).Range
myRange.MoveEnd wdCharacter, -1
myString = myString & "<td>" _
& myRange.Text & "</td>"
Next
myString = myString & "</tr>" & vbCrLf
Next
myString = myString & "</table>" & vbCrLf
End With
Set newDoc = Documents.Add
With newDoc
Selection.InsertAfter myString
End With
Set myRange = Nothing
Set newDoc = Nothing
End Sub
 
P

Pesach Shelnitz

Hi,

For compliance with correct coding practices, in the 4th and 5th lines of
the macro in my previous post, change Integer to Long.
 
C

Cal Who

Pesach Shelnitz said:
Hi,

For compliance with correct coding practices, in the 4th and 5th lines of
the macro in my previous post, change Integer to Long.

Exactly what I need! Thanks.

I wonder if you know what HTML files need for endings?

Also, does it mater what the macro uses since when saving as a text file I
get the chance to select endings?

Does Word like CrLf for endings?

Thanks again. It's a good example too. Maybe I can do the next macro myself
:)
 
P

Pesach Shelnitz

Hi,

HTML files are text files. They can be named with the file extension .htm or
..html. HTML files can also contain ASP and PHP code. In such cases, they
would have the file extensions .asp (or .aspx) and .php, respectively.

In HTML files line breaks are generally inserted only for readability, and
they are ignored by browsers. The line breaks that appear in browsers are
created by using block tags, such as <p></p>, or the explicit break tag
<br/>. In the case of a table, the <tr></tr> tags define the rows, and the
line breaks are ignored.

When Word opens a text file, it creates a new line after Cr or CrLf.
 
C

Cal Who

Thanks a lot

Pesach Shelnitz said:
Hi,

HTML files are text files. They can be named with the file extension .htm
or
.html. HTML files can also contain ASP and PHP code. In such cases, they
would have the file extensions .asp (or .aspx) and .php, respectively.

In HTML files line breaks are generally inserted only for readability, and
they are ignored by browsers. The line breaks that appear in browsers are
created by using block tags, such as <p></p>, or the explicit break tag
<br/>. In the case of a table, the <tr></tr> tags define the rows, and the
line breaks are ignored.

When Word opens a text file, it creates a new line after Cr or CrLf.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top