المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : تحويل ملف الورد الي Rtf- Html- Txt- Xml



Raggi Tech
10-17-2012, 04:05 PM
كاتب الموضوع : AhmedEssawy


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



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

private void btnClose_Click(object sender, EventArgs e)
{
// Since we don't want to save changes to the original document
object SaveChanges = false;
// Close the document, save no changes
WordDoc.Close(ref SaveChanges, ref DocNoParam, ref DocNoParam);
}
}
}