Introduction: In this article I will explain how to read the
content of a .doc file and show this in a Rich Textbox. And further we will
filter the Email IDs form the content of the file.
NameSpace :
First of all you need to
add a reference for ‘Microsoft.Office.Interop.Word’
And the use this namespace on your page.
using System.Reflection;
using Microsoft.Office.Interop.Word;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
Code :
Here is the complete code:
private void
button1_Click(object sender, EventArgs e)
{
OpenFileDialog op1 = new
OpenFileDialog();
op1.Multiselect = false;
op1.ShowDialog();
int count = 0;
string[] FName;
foreach (string s in op1.FileNames)
{
FName = s.Split('\\');
//createting the object of application
class
Microsoft.Office.Interop.Word.Application
Objword = new Microsoft.Office.Interop.Word.Application();
//creating the object of document class
Document objdoc = new Document();
//get the uploaded file full path
dynamic FilePath = s;
//pass the optional (missing) parameter to
API
dynamic NA = System.Type.Missing;
//open Word file document
objdoc = Objword.Documents.Open
(ref FilePath, ref NA,
ref NA, ref NA,
ref NA,
ref NA, ref NA, ref NA, ref NA,
ref NA, ref NA, ref NA, ref NA,
ref NA, ref NA, ref NA
);
//creating the object of string builder
class
StringBuilder sb = new StringBuilder();
for (int
Line = 0; Line < objdoc.Paragraphs.Count; Line++)
{
string Filedata = objdoc.Paragraphs[Line
+ 1].Range.Text.Trim();
if (Filedata != string.Empty)
{
//Append word files data to stringbuilder
sb.AppendLine(Filedata);
}
}
//closing document object
((_Document)objdoc).Close();
//Quit application object to end process
((_Application)Objword).Quit();
//assign stringbuilder object to show text in
textbox
richTextBox1.Text = Convert.ToString(sb);
}
// Code to Find Email Ids from the file content.
MatchCollection mc;
mc = Regex.Matches(richTextBox1.Text,
"([a-zA-Z0-9_\\-\\.]+)@([a-zA-Z0-9_\\-\\.]+)\\.([a-zA-Z]{2,5})");
string res = (mc.Count - 1).ToString();
for (int i = 0; i
< mc.Count; i++)
{
MessageBox.Show (mc[i].Value);
}
}
No comments:
Post a Comment