C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » C# Syntax »

Saving Word document into web page using C#


Posted Date: 10 Oct 2008    Resource Type: Code Snippets    Category: C# Syntax
Author: SathishMember Level: Silver    
Rating: 1 out of 5Points: 10



The below code snippet will convert a wrd document into a .htm web page. This function is ready to use and can be called wherever needed.


private void DocToWebpage()
{
//Creating the instance of Word Application
Microsoft.Office.Interop.Word.Application newApp = new Microsoft.Office.Interop.Word.Application();

// specify the Source & Target file names
object Source = "D:\\Sample.docx";
object Target = "D:\\Sandbox.htm";

// Use for the parameter whose type are not known or
// say Missing
object Unknown =Type.Missing;

// Source document open here
// Additional Parameters are not known so that are
// set as a missing type
newApp.Documents.Open(ref Source,ref Unknown,
ref Unknown,ref Unknown,ref Unknown,
ref Unknown,ref Unknown,ref Unknown,
ref Unknown,ref Unknown,ref Unknown,
ref Unknown,ref Unknown,ref Unknown,ref Unknown, ref Unknown);

// Specifying the format in which you want the output file
object format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatHTML;

//Changing the format of the document
newApp.ActiveDocument.SaveAs(ref Target,ref format,
ref Unknown,ref Unknown,ref Unknown,
ref Unknown,ref Unknown,ref Unknown,
ref Unknown,ref Unknown,ref Unknown,
ref Unknown,ref Unknown,ref Unknown,
ref Unknown,ref Unknown);

// for closing the application
newApp.Quit(ref Unknown,ref Unknown,ref Unknown);
}


Note: The above snippet has been implemented over MS Office 2007. The source document can be .docx or .doc. The target format can be any format supported by MS Word.

To invoke this method, these references must be added.


using Microsoft.Office;
using Microsoft.Office.Interop;


The assemblies to be refered are:

<add assembly="Microsoft.Office.Tools.Word, Version=8.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
<add assembly="Microsoft.Office.Interop.Word, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71E9BCE111E9429C"/> <add assembly="Office, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71E9BCE111E9429C"/>



Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Word to html conversion  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Writing string content to a file
Previous Resource: Interest calculator in C#
Return to Discussion Resource Index
Post New Resource
Category: C# Syntax


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use