0

I am trying to read/parse a word document for information. Inside the word document, it contains the name, email address, subject, message (let's leave out attachments for now if it complicates things). I have each info on a separate line to make things simple. After it has been read, it should dispatch the email to the email address with the corresponding subject, message and attachment (leave attachment if complicated) to the email address.

This is also a console application where processWord.dll will handle the processing and dispatching.

For starters, this is what I have so far inside my Program.cs that loops through all the words in the document and prints it to the console:

using Microsoft.Office.Interop.Word;
using Microsoft.Office.Interop.Excel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Project
{
    class Program
    {
        static void Main(string[] args)
        {
            Microsoft.Office.Interop.Word.Application application = new Microsoft.Office.Interop.Word.Application();
            Document document = application.Documents.Open("C:\\Users\\name\\Desktop\\word.docx");

            int count = document.Words.Count;
            for (int i = 1; i <= count; i++)
            {
                string text = document.Words[i].Text;
                Console.WriteLine("Word {0} = {1}", i, text);
            }
        }
    }
}
Muhammed Shevil KP
  • 1,404
  • 1
  • 16
  • 21
canucksfan96
  • 49
  • 1
  • 7

2 Answers2

1

If your data in separate lines you can try this code.

This is spliting lines and saving to list. So you can use this list for send mail.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mail;
using Microsoft.Office.Interop.Word;

namespace Project
{
class Program
{
    static void Main(string[] args)
    {
        Microsoft.Office.Interop.Word.Application application = new Microsoft.Office.Interop.Word.Application();
        Document document = application.Documents.Open("C:\\Users\\name\\Desktop\\word.docx");

        int count = document.Paragraphs.Count;

        string totaltext = "";
        List<string> rows = new List<string>();

        for (int i = 1; i <= count; i++)
        {
             string temp = doc.Paragraphs[i + 1].Range.Text.Trim();
            if (temp != string.Empty)
                rows.Add(temp);
        }
        //WE HAVE ROWS IN WORD DOCUMENT. NOW WE CAN SEND.
        string mailTo= rows[0];
        string name= rows[1];
        string subject= rows[2];
        string messageBody = rows[3];
        string attachmentPath=rows[4];
         try 
            {
                MailMessage oMsg = new MailMessage();
                // TODO: Replace with sender e-mail address.
                oMsg.From = "sender@somewhere.com";
                // TODO: Replace with recipient e-mail address.
                oMsg.To = name+"<"+mailTo+">";
                oMsg.Subject = subject;

                // SEND IN HTML FORMAT (comment this line to send plain text).
                oMsg.BodyFormat = MailFormat.Html;

                // HTML Body (remove HTML tags for plain text).
                oMsg.Body = messageBody;

                // ADD AN ATTACHMENT.
                // TODO: Replace with path to attachment.
                String sFile = attachmentPath;  
                MailAttachment oAttch = new MailAttachment(sFile, MailEncoding.Base64);

                oMsg.Attachments.Add(oAttch);

                // TODO: Replace with the name of your remote SMTP server.
                SmtpMail.SmtpServer = "MySMTPServer";
                SmtpMail.Send(oMsg);

                oMsg = null;
                oAttch = null;
            }
            catch (Exception e)
            {
                Console.WriteLine("{0} Exception caught.", e);
            }

    }
}
}
canucksfan96
  • 49
  • 1
  • 7
  • When I try to do it your way, I get a 'COMException was unhandled' exception. Full message is: 'An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in Project.exe'. It's complaining on this line: string temp = document.Paragraphs[i + 1].Range.Text.Trim(); – canucksfan96 Feb 16 '17 at 06:10
1

So, you want us to write code fore you ?

First of all, i think using Interop is quite bad idea. Try to use Open XML. There is a lot of links and tutorials in the internet. Your code must analyze document for some anchor elements (such as name, email and so far) and, when finding it fetch nearby information. Of Course it will be very wise to place work with Word document in separate library behind some interface, that will provide functionality.

After that, check some links like this for understanding work with e-mail in C#:

Stackoverflow is not the place where people working for you. it is a place where people helping you to handle difficult situations and sharing experience with you. In all other situations, seek assistance of freelancers websites.

Community
  • 1
  • 1
Imme Pak
  • 160
  • 1
  • 8