تقييم الموضوع :
  • 0 أصوات - بمعدل 0
  • 1
  • 2
  • 3
  • 4
  • 5
تحويل ملف الورد الي Rtf- Html- Txt- Xml
#1
كاتب الموضوع : AhmedEssawy

Add Reference.
ثم
the COM tab
ثم
Microsoft Word 11.0 Object Library.


كود :
[FONT=Courier New, Courier, monospace][SIZE=2][color=#0000ff]using[/color][/SIZE][/FONT][FONT=Courier New, Courier, monospace][SIZE=2] System;
[color=#0000ff]using[/color] System.Collections.Generic;
[color=#0000ff]using[/color] System.ComponentModel;
[color=#0000ff]using[/color] System.Data;
[color=#0000ff]using[/color] System.Drawing;
[color=#0000ff]using[/color] System.Text;
[color=#0000ff]using[/color] System.Windows.Forms;[/SIZE][/FONT]
[FONT=Courier New, Courier, monospace][SIZE=2][color=#0000ff]namespace[/color][/SIZE][/FONT][FONT=Courier New, Courier, monospace][SIZE=2] OpenWord
{
[color=#0000ff]public partial class[/color] [color=#0099cc]Form1[/color] : [color=#0099cc]Form[/color]
{
[color=#0000ff]private[/color] Word.[color=#0099cc]ApplicationClass[/color] WordApp;
[color=#0000ff]private[/color] Word.[color=#0099cc]Document[/color] WordDoc;
[color=#0000ff]private object[/color] DocNoParam = [color=#0099cc]Type[/color].Missing;[/SIZE][/FONT]
[FONT=Courier New, Courier, monospace][SIZE=2][color=#0000ff]public[/color] Form1()
{
InitializeComponent();
}[/SIZE][/FONT]
[FONT=Courier New, Courier, monospace][SIZE=2][color=#0000ff]private void[/color] btnOpen_Click([color=#0000ff]object[/color] sender, [color=#0099cc]EventArgs[/color] e)
{
[color=#009900]// Create an instance of the Word Application[/color]
WordApp = [color=#0000ff]new[/color] Word.[color=#0099cc]ApplicationClass[/color]();
[color=#009900]// We don't want to display the Microsoft Word window[/color]
WordApp.Visible = [color=#0000ff]false[/color];[/SIZE][/FONT]
[FONT=Courier New, Courier, monospace][SIZE=2][color=#009900]// If the user choosed a path of the file to open[/color]
[color=#0000ff]if[/color] ([color=#0000ff]this[/color].openDoc.ShowDialog() == [color=#0099cc]DialogResult[/color].OK)
{
[color=#009900]// Set the label to the new file path[/color]
lblFilePath.Text = openDoc.FileName;
[color=#009900]// Enable the convert and close buttons, since now we have a document opened[/color]
btnToHtml.Enabled = [color=#0000ff]true[/color];
btnToRTF.Enabled = [color=#0000ff]true[/color];
btnToText.Enabled = [color=#0000ff]true[/color];
btnToXml.Enabled = [color=#0000ff]true[/color];
btnClose.Enabled = [color=#0000ff]true[/color];[/SIZE][/FONT]
[FONT=Courier New, Courier, monospace][SIZE=2][color=#009900]// Create and set the objects we're going to pass to the Open() function[/color]
[color=#0000ff]object[/color] DocFileName = openDoc.FileName;
[color=#0000ff]object[/color] DocReadOnly = [color=#0000ff]false[/color];
[color=#0000ff]object[/color] DocVisible = [color=#0000ff]true[/color];[/SIZE][/FONT]
[FONT=Courier New, Courier, monospace][SIZE=2][color=#009900]// Open the document by passing the path[/color]
WordDoc = WordApp.Documents.Open([color=#0000ff]ref[/color] DocFileName, [color=#0000ff]ref[/color] DocNoParam, [color=#0000ff]ref[/color] DocReadOnly, [color=#0000ff]ref[/color] DocNoParam, [color=#0000ff]ref[/color] DocNoParam, [color=#0000ff]ref[/color] DocNoParam, [color=#0000ff]ref[/color] DocNoParam, [color=#0000ff]ref[/color] DocNoParam, [color=#0000ff]ref[/color] DocNoParam, [color=#0000ff]ref[/color] DocNoParam, [color=#0000ff]ref[/color] DocVisible, [color=#0000ff]ref[/color] DocNoParam, [color=#0000ff]ref[/color] DocNoParam, [color=#0000ff]ref[/color] DocNoParam, [color=#0000ff]ref[/color] DocNoParam, [color=#0000ff]ref[/color] DocNoParam);
WordDoc.Activate();
}
}[/SIZE][/FONT]
[FONT=Courier New, Courier, monospace][SIZE=2][color=#0000ff]private void[/color] btnToHtml_Click([color=#0000ff]object[/color] sender, EventArgs e)
{
[color=#009900]// Suggest a path for saving[/color]
saveDoc.FileName = [color=#990000]@"C:\Test Document.html"[/color];
[color=#009900]// The file extension to which we want to save[/color]
saveDoc.Filter = [color=#990000]"HTML Files|*.html"[/color];[/SIZE][/FONT]
[FONT=Courier New, Courier, monospace][SIZE=2][color=#009900]// If the user choosed a path where to save the file[/color]
[color=#0000ff]if[/color] ([color=#0000ff]this[/color].saveDoc.ShowDialog() == [color=#009999]DialogResult[/color].OK)
{
[color=#009900]// Set the save path object[/color]
[color=#0000ff]object[/color] SaveToPath = saveDoc.FileName;
[color=#009900]// Set the format type to HTML (wdFormatHTML)[/color]
[color=#0000ff]object[/color] SaveToFormat = SaveToFormat = Word.[color=#0099cc]WdSaveFormat[/color].wdFormatHTML;
[color=#009900]// Save the document to the specified path and format[/color]
WordDoc.SaveAs([color=#0000ff]ref[/color] SaveToPath, [color=#0000ff]ref[/color] SaveToFormat, [color=#0000ff]ref[/color] DocNoParam, [color=#0000ff]ref[/color] DocNoParam, [color=#0000ff]ref[/color] DocNoParam, [color=#0000ff]ref[/color] DocNoParam, [color=#0000ff]ref[/color] DocNoParam, [color=#0000ff]ref[/color] DocNoParam, [color=#0000ff]ref[/color] DocNoParam, [color=#0000ff]ref[/color] DocNoParam, [color=#0000ff]ref[/color] DocNoParam, [color=#0000ff]ref[/color] DocNoParam, [color=#0000ff]ref[/color] DocNoParam, [color=#0000ff]ref[/color] DocNoParam, [color=#0000ff]ref[/color] DocNoParam, [color=#0000ff]ref[/color] DocNoParam);
}
}[/SIZE][/FONT]
[FONT=Courier New, Courier, monospace][SIZE=2][color=#0000ff]private void[/color] btnToRTF_Click([color=#0000ff]object[/color] sender, EventArgs e)
{
[color=#009900]// Suggest a path for saving[/color]
saveDoc.FileName = [color=#990000]@"C:\Test Document.rtf"[/color];
[color=#009900]// The file extension to which we want to save[/color]
saveDoc.Filter = [color=#990000]"RTF Files|*.rtf"[/color];[/SIZE][/FONT]
[FONT=Courier New, Courier, monospace][SIZE=2][color=#009900]// If the user choosed a path where to save the file[/color]
[color=#0000ff]if[/color] ([color=#0000ff]this[/color].saveDoc.ShowDialog() == [color=#0099cc]DialogResult[/color].OK)
{
[color=#009900]// Set the save path object[/color]
[color=#0000ff]object[/color] SaveToPath = saveDoc.FileName;
[color=#009900]// Set the format type to RTF (wdFormatRTF)[/color]
[color=#0000ff]object[/color] SaveToFormat = SaveToFormat = Word.[color=#0099cc]WdSaveFormat[/color].wdFormatRTF;
[color=#009900]// Save the document to the specified path and format[/color]
WordDoc.SaveAs([color=#0000ff]ref[/color] SaveToPath, [color=#0000ff]ref[/color] SaveToFormat, [color=#0000ff]ref[/color] DocNoParam, [color=#0000ff]ref[/color] DocNoParam, [color=#0000ff]ref[/color] DocNoParam, [color=#0000ff]ref[/color] DocNoParam, [color=#0000ff]ref[/color] DocNoParam, [color=#0000ff]ref[/color] DocNoParam, [color=#0000ff]ref[/color] DocNoParam, [color=#0000ff]ref[/color] DocNoParam, [color=#0000ff]ref[/color] DocNoParam, [color=#0000ff]ref[/color] DocNoParam, [color=#0000ff]ref[/color] DocNoParam, [color=#0000ff]ref[/color] DocNoParam, [color=#0000ff]ref[/color] DocNoParam, [color=#0000ff]ref[/color] DocNoParam);
}
}[/SIZE][/FONT]
[FONT=Courier New, Courier, monospace][SIZE=2][color=#0000ff]private void[/color] btnToText_Click([color=#0000ff]object[/color] sender, EventArgs e)
{
[color=#009900]// Suggest a path for saving[/color]
saveDoc.FileName = [color=#990000]@"C:\Test Document.txt"[/color];
[color=#009900]// The file extension to which we want to save[/color]
saveDoc.Filter = [color=#990000]"Text Files|*.txt"[/color];[/SIZE][/FONT]
[FONT=Courier New, Courier, monospace][SIZE=2][color=#009900]// If the user choosed a path where to save the file[/color]
[color=#0000ff]if[/color] ([color=#0000ff]this[/color].saveDoc.ShowDialog() == [color=#0099cc]DialogResult[/color].OK)
{
[color=#009900]// Set the save path object[/color]
[color=#0000ff]object[/color] SaveToPath = saveDoc.FileName;
[color=#009900]// Set the format type to TXT (wdFormatText)[/color]
[color=#0000ff]object[/color] SaveToFormat = SaveToFormat = Word.[color=#0099cc]WdSaveFormat[/color].wdFormatText;
[color=#009900]// Save the document to the specified path and format[/color]
WordDoc.SaveAs([color=#0000ff]ref[/color] SaveToPath, [color=#0000ff]ref[/color] SaveToFormat, [color=#0000ff]ref[/color] DocNoParam, [color=#0000ff]ref[/color] DocNoParam, [color=#0000ff]ref[/color] DocNoParam, [color=#0000ff]ref[/color] DocNoParam, [color=#0000ff]ref[/color] DocNoParam, [color=#0000ff]ref[/color] DocNoParam, [color=#0000ff]ref[/color] DocNoParam, [color=#0000ff]ref[/color] DocNoParam, [color=#0000ff]ref[/color] DocNoParam, [color=#0000ff]ref[/color] DocNoParam, [color=#0000ff]ref[/color] DocNoParam, [color=#0000ff]ref[/color] DocNoParam, [color=#0000ff]ref[/color] DocNoParam, [color=#0000ff]ref[/color] DocNoParam);
}
}[/SIZE][/FONT]
[FONT=Courier New, Courier, monospace][SIZE=2][color=#0000ff]private void[/color] btnToXml_Click([color=#0000ff]object[/color] sender, EventArgs e)
{
[color=#009900]// Suggest a path for saving[/color]
saveDoc.FileName = [color=#990000]@"C:\Test Document.xml"[/color];
[color=#009900]// The file extension to which we want to save[/color]
saveDoc.Filter = [color=#990000]"XML Files|*.xml"[/color];[/SIZE][/FONT]
[FONT=Courier New, Courier, monospace][SIZE=2][color=#009900]// If the user choosed a path where to save the file[/color]
[color=#0000ff]if[/color] ([color=#0000ff]this[/color].saveDoc.ShowDialog() == [color=#0099cc]DialogResult[/color].OK)
{
[color=#009900]// Set the save path object[/color]
[color=#0000ff]object[/color] SaveToPath = saveDoc.FileName;
[color=#009900]// Set the format type to XML (wdFormatXML)[/color]
[color=#0000ff]object[/color] SaveToFormat = SaveToFormat = Word.[color=#0099cc]WdSaveFormat[/color].wdFormatXML;
[color=#009900]// Save the document to the specified path and format[/color]
WordDoc.SaveAs([color=#0000ff]ref[/color] SaveToPath, [color=#0000ff]ref[/color] SaveToFormat, [color=#0000ff]ref[/color] DocNoParam, [color=#0000ff]ref[/color] DocNoParam, [color=#0000ff]ref[/color] DocNoParam, [color=#0000ff]ref[/color] DocNoParam, [color=#0000ff]ref[/color] DocNoParam, [color=#0000ff]ref[/color] DocNoParam, [color=#0000ff]ref[/color] DocNoParam, [color=#0000ff]ref[/color] DocNoParam, [color=#0000ff]ref[/color] DocNoParam, [color=#0000ff]ref[/color] DocNoParam, [color=#0000ff]ref[/color] DocNoParam, [color=#0000ff]ref[/color] DocNoParam, [color=#0000ff]ref[/color] DocNoParam, [color=#0000ff]ref[/color] DocNoParam);
}
}

[color=#0000ff]private void[/color] btnClose_Click([color=#0000ff]object[/color] sender, EventArgs e)
{
[color=#009900]// Since we don't want to save changes to the original document[/color]
[color=#0000ff]object[/color] SaveChanges = [color=#0000ff]false[/color];
[color=#009900]// Close the document, save no changes[/color]
WordDoc.Close([color=#0000ff]ref[/color] SaveChanges, [color=#0000ff]ref[/color] DocNoParam, [color=#0000ff]ref[/color] DocNoParam);
}
}
}[/SIZE][/FONT]
}}}
تم الشكر بواسطة:



التنقل السريع :


يقوم بقرائة الموضوع: بالاضافة الى ( 1 ) ضيف كريم