tags:

views:

21

answers:

2

Problem that I am trying to resolve:

I want to convert a word document into HTML and along with that I want to convert specific text into Hyperlinks. I can't find a way in C# to add hyperlinks before saving the word document as HTML. So, I do the trick I save document as HTML and then read the HTML document and replace the specific text with hyperlink.

I don't know if that is the best way to do it but so far I didn't find any other option.

Now the problem is that when I read the HTML file, I got garbage characters for some special characters. In my word document I have "..." three dots which I think is a special character in word, so when I read that in HTML I got garbage letters... The strange thing is that when I open that HTML file in notepad it shows me correctly.

However, through coding i am not able to read such characters.

Please help me.

A: 

Sounds like you may need to look into the encoding used by the ms word doc. MS Word may be using ASCIIEncoding.

Also have a look for existing c# libraries that do this.

cofiem
I already tried using encodings like UTF8 (because that is the encoding that word uses) and also tried with Ansi but that didn't help. Anyways, I've figured out the solution now I add hyperlinks in the word document before saving it as HTML so I don't need to read the HTML anymore. Here is how I am doing it nowFind the text and the starting position and then Address = doc.Range(start, end).Text + ".pdf"doc.Hyperlinks.Add(doc.Range(start, end), Address, _ SubAddress, ScreenTip, TextToDisplay, Target)
Kashif
Even though I have found the solution but still i am confused why I can't read such letters through filestream even though notepad and HTML browser or editor can show the same characters.
Kashif
Also, could you please explain what is the difference between doc.Content.Text and doc.Range(start, end).Text Actually, if I extract a string like doc.Content.Text.SubString(start, lenofText) and if I do the same with doc.Range(start, start + lenofText)I get correct result for doc.Content.Text but incorrect result with doc.Range ... do you know the reason?
Kashif
A: 

My actual problem was that I wanted to convert specific text in word document to hyperlinks and I couldn't find a way to do that. The MSDN help is really pathetic and had errors that wasted lot of my time. I am new in automating word documents.

That's why I found it easy to add hyperlinks after converting the file to HTML but that has its own issues. Here is how I am doing it now

Word library provides you the Find function but that does not tell you the position where the text was found.

Find the text and the starting position and then

Address = doc.Range(start, end).Text + ".pdf"

doc.Hyperlinks.Add(doc.Range(start, end), Address, _

                                SubAddress, ScreenTip, TextToDisplay, Target)
Kashif