hello..i want to read only plain text form this type of word file...and save in to database..plz help
Loading
hello..i want to read only plain text form this type of word file...and save in to database..plz help
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Dipankar BiswasPosted May 26, 2015, 10:39 AM
As you can read in the code, for the path of the Word document, the full path is required. If you just write
test.docx, then you'll actually try to convertC:\Windows\system32\test.docxinstead of thetest.docxfile in the folder of the converter. For the file path of the text file, it is OK to writetest.txt, because then it will create thetest.txtfile in the folder of the converter. It is also necessary that the path to the Word file is anobject, not astring, because when we're going to open the Word file, the parameters should beobjects. Now, we'll open the Word file and retrieve the text using the following code:@"c:\Users\Public\Public Documents\Word12.docx");PrakashPosted May 26, 2015, 9:19 AM
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using Word = Microsoft.Office.Interop.Word;
namespace ReadText
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Please enter the full file path of your Word document (without quotes):");
object path = Console.ReadLine();
Console.WriteLine("Please enter the file path of the text document in which you want to store the text of your word document (without quotes):");
string txtPath = Console.ReadLine();
Word.Application app = new Word.Application();
Word.Document doc;
object missing = Type.Missing;
object readOnly = true;
try
{
doc = app.Documents.Open(ref path, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
string text = doc.Content.Text;
File.WriteAllText(txtPath, text);
Console.WriteLine("Converted!");
}
catch
{
Console.WriteLine("An error occured. Please check the file path to your word document, and whether the word document is valid.");
}
finally
{
object saveChanges = Word.WdSaveOptions.wdDoNotSaveChanges;
app.Quit(ref saveChanges, ref missing, ref missing);
}
}
}
}
Dipankar BiswasPosted May 26, 2015, 3:26 AM