How to insert HTML table into Word doc using C#?

S

Stitch 2.0

Hi all,

I'm desperatly trying to add an HTML Table into a Word document using
C#.

For example:
Here is the table I want to insert:
<table>
<tr>
<td>some text here</td>
</tr>
<tr>
<td>some other text here</td>
</tr>
</table>

Currently I am using the following code to do it:

object fileToOpen = (object) @"C:\input.doc";
app = new MSWord.ApplicationClass();
doc = new MSWord.Document();
doc = app.Documents.Open(ref fileToOpen, ref Missing, ref Missing, ref
Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref
Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref
Missing, ref Missing,ref Missing);
bookmarkName = (object) "myBookmark";
rng = doc.Bookmarks.get_Item(ref bookmarkName).Range;
MSWord.Table oTable;
oTable = doc.Tables.Add(rng, 1 , 1, ref Missing, ref Missing);
Table.Cell(1,1).Range.Text = "<table><tr><td>some text
here</td></tr><tr><td>some other text here</td></tr></table>";
object fileToSave = (object) @"C:\output.doc";
doc.SaveAs(ref fileToSave, ref Missing, ref Missing, ref Missing, ref
Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref
Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref
Missing, ref Missing);

Unfortunately, the resulting document only contains a single string:
some text heresome other text here

Meaning, word simply ignores the HTML tags and inserts the text only.

Any ideas on how to solve this?

I tried to google for a solutions, but couldn't find one...

And, by the way, it's Word 2003 we're using here. ;)

Thanks in Advance,
Stitch
 
S

Stitch 2.0

After a little trial and error, I found out, that one problem was, that
the html source code was stripped from the string due to some
manipulation before.

Now the HTML source is passed to the document as shown in the source
code in my last message.
But the result is still wrong, because now the HTML source is displayed
just like that in the word. Meaning the resulting Word document
contains:
<table><tr><td>some text here</td></tr><tr><td>some other text
here</td></tr></table>

And since this is still not the result wanted, the question remains:
How can I get Word to interpret the HTML source?

Thanks in Advance,
Stitch2.0
 
C

Cindy M -WordMVP-

Hi Stitch,
I'm desperatly trying to add an HTML Table into a Word document using
C#.
The Word object model has no provision for "converting" HTML code into
its binary format. You have the following options

1. Stream the HTML to a (temp) file and insert it using the InsertFile
method. This will trigger the conversion filter.

2. Put the HTML on the clipboard, then use the Paste method to insert
it. Again, this will trigger the conversion filter

3. If you're targeting Word 2003, generate a table in Words WordML XML
vocabulary and then use the InsertXML method.

4. Generate the table using basic Word automation.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question
or reply in the newsgroup and not by e-mail :)
 
Z

zajac.bartosz

Cindy M -WordMVP- napisal(a):
2. Put the HTML on the clipboard, then use the Paste method to insert
it. Again, this will trigger the conversion filter

Hello. Can you explain more this metod??? It doesnt work in my code:(
 
C

Cindy M.

2. Put the HTML on the clipboard, then use the Paste method to insert
Hello. Can you explain more this metod??? It doesnt work in my code:(
Not really... Especially since we have no idea in what programming
language you're working, what your code is like, or how it's "not
working".

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 

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