How to call Interop Class to convert a HTML Page in ASP to DOC or DOCX ....?

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I have a requirement where there is a excel sheet which contains the URLs of some ASP pages containing HTML whose design need to be converted to MS Word. The code will first read the webpage URL from the excel sheet and then will convert that page to WORD.
I am not able to use the Microsoft Word Object Library to convert the HTML content to WORD. I have used the following code to convert ASP page to PDF using iTextSharp.dll and need to do some modifications in HtmlToPdf() only so that the code can convert asp
page to Word.
<span style="font-size:10.0pt; font-family:Courier New; color:blue protected<span style="font-size:10.0pt; font-family:Courier New; color:black
<span style="font-size:10.0pt; font-family:Courier New; color:blue void<span style="font-size:10.0pt; font-family:Courier New; color:black BTN_Click(<span style="font-size:10.0pt; font-family:Courier New; color:blue object<span style="font-size:10.0pt; font-family:Courier New; color:black
sender, <span style="font-size:10.0pt; font-family:Courier New; color:#2B91AF EventArgs<span style="font-size:10.0pt; font-family:Courier New; color:black e)
<span style="color:black {<br/>

<span style="font-size:10.0pt; font-family:Courier New; color:black
<span style="font-size:10.0pt; font-family:Courier New; color:#2B91AF DataSet<span style="font-size:10.0pt; font-family:Courier New; color:black dtExcelData = GetExcelData();
<span style="font-size:10.0pt; font-family:Courier New; color:black
<span style="font-size:10.0pt; font-family:Courier New; color:blue foreach<span style="font-size:10.0pt; font-family:Courier New; color:black (<span style="font-size:10.0pt; font-family:Courier New; color:#2B91AF DataRow<span style="font-size:10.0pt; font-family:Courier New; color:black
dr <span style="font-size:10.0pt; font-family:Courier New; color:blue in<span style="font-size:10.0pt; font-family:Courier New; color:black dtExcelData.Tables[0].Rows)
<span style="font-size:10.0pt; font-family:Courier New; color:black {

<span style="font-size:10.0pt; font-family:Courier New; color:black
<span style="font-size:10.0pt; font-family:Courier New; color:blue string<span style="font-size:10.0pt; font-family:Courier New; color:black url = dr[<span style="font-size:10.0pt; font-family:Courier New; color:#A31515 "FileURL"<span style="font-size:10.0pt; font-family:Courier New; color:black ].ToString();
<span style="font-size:10.0pt; font-family:Courier New; color:black
<span style="font-size:10.0pt; font-family:Courier New; color:blue string<span style="font-size:10.0pt; font-family:Courier New; color:black [] urlSpit = url.Split(<span style="font-size:10.0pt; font-family:Courier New; color:#A31515 /<span style="font-size:10.0pt; font-family:Courier New; color:black );
<span style="font-size:10.0pt; font-family:Courier New; color:black
<span style="font-size:10.0pt; font-family:Courier New; color:blue string<span style="font-size:10.0pt; font-family:Courier New; color:black fileName =
<span style="font-size:10.0pt; font-family:Courier New; color:blue string<span style="font-size:10.0pt; font-family:Courier New; color:black .Empty;
<span style="font-size:10.0pt; font-family:Courier New; color:black
<span style="font-size:10.0pt; font-family:Courier New; color:blue foreach<span style="font-size:10.0pt; font-family:Courier New; color:black (<span style="font-size:10.0pt; font-family:Courier New; color:blue string<span style="font-size:10.0pt; font-family:Courier New; color:black
s <span style="font-size:10.0pt; font-family:Courier New; color:blue in<span style="font-size:10.0pt; font-family:Courier New; color:black urlSpit)
<span style="font-size:10.0pt; font-family:Courier New; color:black {
<span style="font-size:10.0pt; font-family:Courier New; color:black
<span style="font-size:10.0pt; font-family:Courier New; color:blue if<span style="font-size:10.0pt; font-family:Courier New; color:black (s.Contains(<span style="font-size:10.0pt; font-family:Courier New; color:#A31515 ".asp"<span style="font-size:10.0pt; font-family:Courier New; color:black ))
<span style="font-size:10.0pt; font-family:Courier New; color:black fileName = s;
<span style="font-size:10.0pt; font-family:Courier New; color:black }
<span style="font-size:10.0pt; font-family:Courier New; color:black
<span style="font-size:10.0pt; font-family:Courier New; color:blue if<span style="font-size:10.0pt; font-family:Courier New; color:black (!<span style="font-size:10.0pt; font-family:Courier New; color:blue string<span style="font-size:10.0pt; font-family:Courier New; color:black .IsNullOrEmpty(fileName))
<span style="font-size:10.0pt; font-family:Courier New; color:black {
<span style="font-size:10.0pt; font-family:Courier New; color:black
<span style="font-size:10.0pt; font-family:Courier New; color:blue string<span style="font-size:10.0pt; font-family:Courier New; color:black [] fileASP = fileName.Split(<span style="font-size:10.0pt; font-family:Courier New; color:#A31515 .<span style="font-size:10.0pt; font-family:Courier New; color:black );
<span style="font-size:10.0pt; font-family:Courier New; color:black
<span style="font-size:10.0pt; font-family:Courier New; color:blue string<span style="font-size:10.0pt; font-family:Courier New; color:black filePDF = fileASP[0].ToString();
<span style="font-size:10.0pt; font-family:Courier New; color:black filePDF = filePDF +
<span style="font-size:10.0pt; font-family:Courier New; color:#A31515 ".pdf"<span style="font-size:10.0pt; font-family:Courier New; color:black ;
<span style="font-size:10.0pt; font-family:Courier New; color:black HTMLToPdf(GetPageText(url), filePDF);
<span style="font-size:10.0pt; font-family:Courier New; color:black }
<span style="font-size:10.0pt; font-family:Courier New; color:black }
<span style="font-size:10.0pt; font-family:Courier New; color:black }
<span style="font-size:10.0pt; font-family:Courier New; color:black
<span style="font-size:10.0pt; font-family:Courier New; color:blue private<span style="font-size:10.0pt; font-family:Courier New; color:black
<span style="font-size:10.0pt; font-family:Courier New; color:blue string<span style="font-size:10.0pt; font-family:Courier New; color:black GetPageText(<span style="font-size:10.0pt; font-family:Courier New; color:blue string<span style="font-size:10.0pt; font-family:Courier New; color:black
url)
<span style="font-size:10.0pt; font-family:Courier New; color:black {
<span style="font-size:10.0pt; font-family:Courier New; color:black
<span style="font-size:10.0pt; font-family:Courier New; color:blue string<span style="font-size:10.0pt; font-family:Courier New; color:black htmlText =
<span style="font-size:10.0pt; font-family:Courier New; color:blue string<span style="font-size:10.0pt; font-family:Courier New; color:black .Empty;
<span style="font-size:10.0pt; font-family:Courier New; color:black
<span style="font-size:10.0pt; font-family:Courier New; color:blue string<span style="font-size:10.0pt; font-family:Courier New; color:black FILE_NAME = Server.MapPath(<span style="font-size:10.0pt; font-family:Courier New; color:#A31515 "test.xml"<span style="font-size:10.0pt; font-family:Courier New; color:black );
<span style="font-size:10.0pt; font-family:Courier New; color:black
<span style="font-size:10.0pt; font-family:Courier New; color:blue try
<span style="font-size:10.0pt; font-family:Courier New; color:black {
<span style="font-size:10.0pt; font-family:Courier New; color:black
<span style="font-size:10.0pt; font-family:Courier New; color:#2B91AF HttpWebRequest<span style="font-size:10.0pt; font-family:Courier New; color:black requestIP = (<span style="font-size:10.0pt; font-family:Courier New; color:#2B91AF HttpWebRequest<span style="font-size:10.0pt; font-family:Courier New; color:black )<span style="font-size:10.0pt; font-family:Courier New; color:#2B91AF WebRequest<span style="font-size:10.0pt; font-family:Courier New; color:black .Create(url);
<span style="font-size:10.0pt; font-family:Courier New; color:black
<span style="font-size:10.0pt; font-family:Courier New; color:#2B91AF CookieContainer<span style="font-size:10.0pt; font-family:Courier New; color:black cc =
<span style="font-size:10.0pt; font-family:Courier New; color:blue new<span style="font-size:10.0pt; font-family:Courier New; color:black
<span style="font-size:10.0pt; font-family:Courier New; color:#2B91AF CookieContainer<span style="font-size:10.0pt; font-family:Courier New; color:black ();
<span style="font-size:10.0pt; font-family:Courier New; color:black requestIP.CookieContainer = cc;
<span style="font-size:10.0pt; font-family:Courier New; color:black requestIP.Timeout = 100000;
<span style="font-size:10.0pt; font-family:Courier New; color:black
<span style="font-size:10.0pt; font-family:Courier New; color:blue using<span style="font-size:10.0pt; font-family:Courier New; color:black (<span style="font-size:10.0pt; font-family:Courier New; color:#2B91AF HttpWebResponse<span style="font-size:10.0pt; font-family:Courier New; color:black
responseIP = (<span style="font-size:10.0pt; font-family:Courier New; color:#2B91AF HttpWebResponse<span style="font-size:10.0pt; font-family:Courier New; color:black )requestIP.GetResponse())
<span style="font-size:10.0pt; font-family:Courier New; color:black {
<span style="font-size:10.0pt; font-family:Courier New; color:black <span style="font-size:10.0pt; font-family:Courier New; color:blue using<span style="font-size:10.0pt; font-family:Courier New; color:black
(<span style="font-size:10.0pt; font-family:Courier New; color:#2B91AF Stream<span style="font-size:10.0pt; font-family:Courier New; color:black streamIP = responseIP.GetResponseStream())
<span style="font-size:10.0pt; font-family:Courier New; color:black {
<span style="font-size:10.0pt; font-family:Courier New; color:black
<span style="font-size:10.0pt; font-family:Courier New; color:blue using<span style="font-size:10.0pt; font-family:Courier New; color:black (<span style="font-size:10.0pt; font-family:Courier New; color:#2B91AF StreamReader<span style="font-size:10.0pt; font-family:Courier New; color:black
readerText = <span style="font-size:10.0pt; font-family:Courier New; color:blue new<span style="font-size:10.0pt; font-family:Courier New; color:black
<span style="font-size:10.0pt; font-family:Courier New; color:#2B91AF StreamReader<span style="font-size:10.0pt; font-family:Courier New; color:black (streamIP))
<span style="font-size:10.0pt; font-family:Courier New; color:black {
<span style="font-size:10.0pt; font-family:Courier New; color:black htmlText = readerText.ReadToEnd();
<span style="font-size:10.0pt; font-family:Courier New; color:black
<span style="font-size:10.0pt; font-family:Courier New; color:blue string<span style="font-size:10.0pt; font-family:Courier New; color:black text = htmlText;
<span style="font-size:10.0pt; font-family:Courier New; color:black
<span style="font-size:10.0pt; font-family:Courier New; color:#2B91AF StreamWriter<span style="font-size:10.0pt; font-family:Courier New; color:black writer =
<span style="font-size:10.0pt; font-family:Courier New; color:blue new<span style="font-size:10.0pt; font-family:Courier New; color:black
<span style="font-size:10.0pt; font-family:Courier New; color:#2B91AF StreamWriter<span style="font-size:10.0pt; font-family:Courier New; color:black (FILE_NAME);
<span style="font-size:10.0pt; font-family:Courier New; color:black writer.Write(text);
<span style="font-size:10.0pt; font-family:Courier New; color:black writer.Close();
<span style="font-size:10.0pt; font-family:Courier New; color:black }
<span style="font-size:10.0pt; font-family:Courier New; color:black }
<span style="font-size:10.0pt; font-family:Courier New; color:black }
<span style="font-size:10.0pt; font-family:Courier New; color:black }
<span style="font-size:10.0pt; font-family:Courier New; color:black
<span style="font-size:10.0pt; font-family:Courier New; color:blue finally
<span style="font-size:10.0pt; font-family:Courier New; color:black {
<span style="font-size:10.0pt; font-family:Courier New; color:black }
<span style="font-size:10.0pt; font-family:Courier New; color:black
<span style="font-size:10.0pt; font-family:Courier New; color:blue return<span style="font-size:10.0pt; font-family:Courier New; color:black htmlText;
<span style="font-size:10.0pt; font-family:Courier New; color:black }
<span style="font-size:10.0pt; font-family:Courier New; color:black
<span style="font-size:10.0pt; font-family:Courier New; color:blue private<span style="font-size:10.0pt; font-family:Courier New; color:black
<span style="font-size:10.0pt; font-family:Courier New; color:blue void<span style="font-size:10.0pt; font-family:Courier New; color:black HTMLToPdf(<span style="font-size:10.0pt; font-family:Courier New; color:blue string<span style="font-size:10.0pt; font-family:Courier New; color:black
HTML, <span style="font-size:10.0pt; font-family:Courier New; color:blue string<span style="font-size:10.0pt; font-family:Courier New; color:black FilePath)
<span style="font-size:10.0pt; font-family:Courier New; color:black {
<span style="font-size:10.0pt; font-family:Courier New; color:black
<span style="font-size:10.0pt; font-family:Courier New; color:#2B91AF Document<span style="font-size:10.0pt; font-family:Courier New; color:black document =
<span style="font-size:10.0pt; font-family:Courier New; color:blue new<span style="font-size:10.0pt; font-family:Courier New; color:black
<span style="font-size:10.0pt; font-family:Courier New; color:#2B91AF Document<span style="font-size:10.0pt; font-family:Courier New; color:black ();
<span style="font-size:10.0pt; font-family:Courier New; color:black
<span style="font-size:10.0pt; font-family:Courier New; color:#2B91AF PdfWriter<span style="font-size:10.0pt; font-family:Courier New; color:black .GetInstance(document,
<span style="font-size:10.0pt; font-family:Courier New; color:blue new<span style="font-size:10.0pt; font-family:Courier New; color:black
<span style="font-size:10.0pt; font-family:Courier New; color:#2B91AF FileStream<span style="font-size:10.0pt; font-family:Courier New; color:black (<span style="font-size:10.0pt; font-family:Courier New; color:#A31515 @"D:PDF"<span style="font-size:10.0pt; font-family:Courier New; color:black
+ FilePath, <span style="font-size:10.0pt; font-family:Courier New; color:#2B91AF FileMode<span style="font-size:10.0pt; font-family:Courier New; color:black .Create));
<span style="font-size:10.0pt; font-family:Courier New; color:black document.Open();
<span style="font-size:10.0pt; font-family:Courier New; color:black iTextSharp.text.html.simpleparser.<span style="font-size:10.0pt; font-family:Courier New; color:#2B91AF StyleSheet<span style="font-size:10.0pt; font-family:Courier New; color:black
styles = <span style="font-size:10.0pt; font-family:Courier New; color:blue new<span style="font-size:10.0pt; font-family:Courier New; color:black iTextSharp.text.html.simpleparser.<span style="font-size:10.0pt; font-family:Courier New; color:#2B91AF StyleSheet<span style="font-size:10.0pt; font-family:Courier New; color:black ();
<span style="font-size:10.0pt; font-family:Courier New; color:black iTextSharp.text.html.simpleparser.<span style="font-size:10.0pt; font-family:Courier New; color:#2B91AF HTMLWorker<span style="font-size:10.0pt; font-family:Courier New; color:black
hw = <span style="font-size:10.0pt; font-family:Courier New; color:blue new<span style="font-size:10.0pt; font-family:Courier New; color:black iTextSharp.text.html.simpleparser.<span style="font-size:10.0pt; font-family:Courier New; color:#2B91AF HTMLWorker<span style="font-size:10.0pt; font-family:Courier New; color:black (document);
<span style="font-size:10.0pt; font-family:Courier New; color:black hw.Parse(<span style="font-size:10.0pt; font-family:Courier New; color:blue new<span style="font-size:10.0pt; font-family:Courier New; color:black
<span style="font-size:10.0pt; font-family:Courier New; color:#2B91AF StringReader<span style="font-size:10.0pt; font-family:Courier New; color:black (HTML));
<span style="font-size:10.0pt; font-family:Courier New; color:black document.Close();
<span style="font-size:10.0pt; font-family:Courier New; color:black
<span style="font-size:10.0pt; font-family:Courier New; color:blue if<span style="font-size:10.0pt; font-family:Courier New; color:black (<span style="font-size:10.0pt; font-family:Courier New; color:#2B91AF File<span style="font-size:10.0pt; font-family:Courier New; color:black .Exists(Server.MapPath(<span style="font-size:10.0pt; font-family:Courier New; color:#A31515 "test.html"<span style="font-size:10.0pt; font-family:Courier New; color:black )))
<span style="font-size:10.0pt; font-family:Courier New; color:black
<span style="font-size:10.0pt; font-family:Courier New; color:#2B91AF File<span style="font-size:10.0pt; font-family:Courier New; color:black .Delete(Server.MapPath(<span style="font-size:10.0pt; font-family:Courier New; color:#A31515 "test.html"<span style="font-size:10.0pt; font-family:Courier New; color:black ));
<span style="font-size:10.0pt; font-family:Courier New; color:black
<span style="font-size:10.0pt; font-family:Courier New; color:blue if<span style="font-size:10.0pt; font-family:Courier New; color:black (<span style="font-size:10.0pt; font-family:Courier New; color:#2B91AF File<span style="font-size:10.0pt; font-family:Courier New; color:black .Exists(Server.MapPath(<span style="font-size:10.0pt; font-family:Courier New; color:#A31515 "test.xml"<span style="font-size:10.0pt; font-family:Courier New; color:black )))
<span style="font-size:10.0pt; font-family:Courier New; color:black
<span style="font-size:10.0pt; font-family:Courier New; color:#2B91AF File<span style="font-size:10.0pt; font-family:Courier New; color:black .Delete(Server.MapPath(<span style="font-size:10.0pt; font-family:Courier New; color:#A31515 "test.xml"<span style="font-size:10.0pt; font-family:Courier New; color:black ));
<span style="font-size:10.0pt; font-family:Courier New; color:black }
<span style="font-size:10.0pt; font-family:Courier New; color:black
<span style="font-size:10.0pt; font-family:Courier New; color:blue private<span style="font-size:10.0pt; font-family:Courier New; color:black
<span style="font-size:10.0pt; font-family:Courier New; color:#2B91AF DataSet<span style="font-size:10.0pt; font-family:Courier New; color:black GetExcelData()
<span style="font-size:10.0pt; font-family:Courier New; color:black {
<span style="font-size:10.0pt; font-family:Courier New; color:black
<span style="font-size:10.0pt; font-family:Courier New; color:blue string<span style="font-size:10.0pt; font-family:Courier New; color:black serverPath =
<span style="font-size:10.0pt; font-family:Courier New; color:#A31515 @"D:ExcelFilesURLList201.xlsx"<span style="font-size:10.0pt; font-family:Courier New; color:black ;
<span style="font-size:10.0pt; font-family:Courier New; color:black
<span style="font-size:10.0pt; font-family:Courier New; color:blue string<span style="font-size:10.0pt; font-family:Courier New; color:black connstr =
<span style="font-size:10.0pt; font-family:Courier New; color:#A31515 " Provider=Microsoft.ACE.OLEDB.12.0;Data Source="<span style="font-size:10.0pt; font-family:Courier New; color:black + serverPath +
<span style="font-size:10.0pt; font-family:Courier New; color:#A31515 ";Excel 12.0 Xml;HDR=YES"<span style="font-size:10.0pt; font-family:Courier New; color:black ;
<span style="font-size:10.0pt; font-family:Courier New; color:black
<span style="font-size:10.0pt; font-family:Courier New; color:#2B91AF OleDbConnection<span style="font-size:10.0pt; font-family:Courier New; color:black conn =
<span style="font-size:10.0pt; font-family:Courier New; color:blue new<span style="font-size:10.0pt; font-family:Courier New; color:black
<span style="font-size:10.0pt; font-family:Courier New; color:#2B91AF OleDbConnection<span style="font-size:10.0pt; font-family:Courier New; color:black (connstr);
<span style="font-size:10.0pt; font-family:Courier New; color:black
<span style="font-size:10.0pt; font-family:Courier New; color:blue string<span style="font-size:10.0pt; font-family:Courier New; color:black strSQL =
<span style="font-size:10.0pt; font-family:Courier New; color:#A31515 "SELECT * FROM [Data$]"<span style="font-size:10.0pt; font-family:Courier New; color:black ;
<span style="font-size:10.0pt; font-family:Courier New; color:black conn.Open();
<span style="font-size:10.0pt; font-family:Courier New; color:black
<span style="font-size:10.0pt; font-family:Courier New; color:#2B91AF OleDbCommand<span style="font-size:10.0pt; font-family:Courier New; color:black cmd =
<span style="font-size:10.0pt; font-family:Courier New; color:blue new<span style="font-size:10.0pt; font-family:Courier New; color:black
<span style="font-size:10.0pt; font-family:Courier New; color:#2B91AF OleDbCommand<span style="font-size:10.0pt; font-family:Courier New; color:black (strSQL, conn);
<span style="font-size:10.0pt; font-family:Courier New; color:black
<span style="font-size:10.0pt; font-family:Courier New; color:#2B91AF DataSet<span style="font-size:10.0pt; font-family:Courier New; color:black ds =
<span style="font-size:10.0pt; font-family:Courier New; color:blue new<span style="font-size:10.0pt; font-family:Courier New; color:black
<span style="font-size:10.0pt; font-family:Courier New; color:#2B91AF DataSet<span style="font-size:10.0pt; font-family:Courier New; color:black ();
<span style="font-size:10.0pt; font-family:Courier New; color:black
<span style="font-size:10.0pt; font-family:Courier New; color:#2B91AF OleDbDataAdapter<span style="font-size:10.0pt; font-family:Courier New; color:black da =
<span style="font-size:10.0pt; font-family:Courier New; color:blue new<span style="font-size:10.0pt; font-family:Courier New; color:black
<span style="font-size:10.0pt; font-family:Courier New; color:#2B91AF OleDbDataAdapter<span style="font-size:10.0pt; font-family:Courier New; color:black (cmd);
<span style="font-size:10.0pt; font-family:Courier New; color:black da.Fill(ds);
<span style="font-size:10.0pt; font-family:Courier New; color:black conn.Close();
<span style="font-size:10.0pt; font-family:Courier New; color:black
<span style="font-size:10.0pt; font-family:Courier New; color:blue return<span style="font-size:10.0pt; font-family:Courier New; color:black ds;
<span style="font-size:10.0pt; font-family:Courier New; color:black }
Can anyone do some modification in this code to convert the ASP pages as defined by FileURL to convert it to WORD using Interop.<br/>


View the full article
 
Back
Top