Error in "for" loop, can't send mail via outlook

I have implemented my work, but the mail is not getting sent. Infact, the compiler can't reach the "for" loop. I have to extract the rows(A-H) where character "d" is present. Suppose if "d" is present in column "M" at "M6" cell, then I need to extract A6-H6 and send it via outlook mail.

Please help me!

I am attaching my work.
Send.txt
Raushan RajAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

it_saigeDeveloperCommented:
Just taking the code at face value, you set a static string named EX_PATH; but then set your argument to use AX_PATH (which is undefined) -
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Excel = Microsoft.Office.Interop.Excel;
using Outlook = Microsoft.Office.Interop.Outlook;

namespace EE_Q29016159
{
    class Program
    {
        //method to send email to outlook
        public static void sendEMailThroughOUTLOOK()
        {
            try
            {
                // Create the Outlook application.
                Outlook.Application oApp = new Outlook.Application();

                // Create a new mail item.
                Outlook.MailItem oMsg = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);

                // Set HTMLBody. 
                //add the body of the email
                oMsg.HTMLBody = "See the details";

                //Subject line
                oMsg.Subject = "Doc Info";

                // Add a recipient.
                Outlook.Recipients oRecips = (Outlook.Recipients)oMsg.Recipients;

                // Change the recipient in the next line if necessary.
                Outlook.Recipient oRecip = (Outlook.Recipient)oRecips.Add("abc@xyz.com");
                oRecip.Resolve();

                // Send.
                oMsg.Send();

                // Clean up.
                oRecip = null;
                oRecips = null;
                oMsg = null;
                oApp = null;
            }
            catch (Exception ex)
            {
            }
        }

        // EX_PATH here...
        private static string EX_PATH = @"F:\Document_Excel.xlsm";
        private static Excel.Workbook AXBook = null;
        private static Excel.Application AXApp = null;
        private static Excel.Worksheet AXSheet = null;
        public static string exception = "";

        static void Main(string[] args)
        {
            if (args == null || args.Length < 4)
            {
                Console.WriteLine("Let's start the work!");
                Console.WriteLine("press any key to continue");
                Console.ReadLine();
            }
            else
            {
                // AX_PATH here???
                AX_PATH = args[0];
            }

            AXApp = new Excel.Application();
            AXApp.Visible = true;

            exception = "";
            try
            {
                // AX_PATH here???
                AXBook = AXApp.Workbooks.Open(AX_PATH, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
            }
            catch (System.Exception ex)
            {
                exception = ex.Message;
            }

            exception = "";
            try
            {
                AXSheet = (Excel.Worksheet)AXBook.Sheets["Sheet First"];
            }
            catch (Exception ex)
            {
                exception = ex.Message;
            }

            Console.WriteLine("Hello");

            Excel.Range last = AXSheet.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell, Type.Missing);
            Excel.Range range = AXSheet.get_Range("A1", last);
            int lastUsedRow = last.Row;
            int lastUsedColumn = last.Column;

            Object missing = System.Type.Missing;
            for (int i = AXSheet.Cells.get_Range("d", missing).Row + 1; i <= last.Row; i++)
            {

                Console.WriteLine("Hello");
                sendEMailThroughOUTLOOK();
            }
        }
    }
}

Open in new window

Changing this allows for your code to compile and run on my computer (however) I get an exception because I do not have the excel workbook/worksheet (this could be corrected by doing a null check); e.g. -
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Excel = Microsoft.Office.Interop.Excel;
using Outlook = Microsoft.Office.Interop.Outlook;

namespace EE_Q29016159
{
    class Program
    {
        //method to send email to outlook
        public static void sendEMailThroughOUTLOOK()
        {
            try
            {
                // Create the Outlook application.
                Outlook.Application oApp = new Outlook.Application();

                // Create a new mail item.
                Outlook.MailItem oMsg = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);

                // Set HTMLBody. 
                //add the body of the email
                oMsg.HTMLBody = "See the details";

                //Subject line
                oMsg.Subject = "Doc Info";

                // Add a recipient.
                Outlook.Recipients oRecips = (Outlook.Recipients)oMsg.Recipients;

                // Change the recipient in the next line if necessary.
                Outlook.Recipient oRecip = (Outlook.Recipient)oRecips.Add("abc@xyz.com");
                oRecip.Resolve();

                // Send.
                oMsg.Send();

                // Clean up.
                oRecip = null;
                oRecips = null;
                oMsg = null;
                oApp = null;
            }
            catch (Exception ex)
            {
            }
        }


        private static string EX_PATH = @"F:\Document_Excel.xlsm";
        private static Excel.Workbook AXBook = null;
        private static Excel.Application AXApp = null;
        private static Excel.Worksheet AXSheet = null;
        public static string exception = "";

        static void Main(string[] args)
        {
            if (args == null || args.Length < 4)
            {
                Console.WriteLine("Let's start the work!");
                Console.WriteLine("press any key to continue");
                Console.ReadLine();
            }
            else
            {
                EX_PATH = args[0];
            }

            AXApp = new Excel.Application();
            AXApp.Visible = true;

            exception = "";
            try
            {
                AXBook = AXApp.Workbooks.Open(EX_PATH, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
            }
            catch (System.Exception ex)
            {
                exception = ex.Message;
            }

            exception = "";
            try
            {
                AXSheet = (Excel.Worksheet)AXBook.Sheets["Sheet First"];
            }
            catch (Exception ex)
            {
                exception = ex.Message;
            }

            Console.WriteLine("Hello");

            if (AXSheet != null && AXSheet.Cells != null)
            {
                Excel.Range last = AXSheet.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell, Type.Missing);
                if (last != null)
                {
                    Excel.Range range = AXSheet.get_Range("A1", last);
                    int lastUsedRow = last.Row;
                    int lastUsedColumn = last.Column;

                    Object missing = System.Type.Missing;
                    for (int i = AXSheet.Cells.get_Range("d", missing).Row + 1; i <= last.Row; i++)
                    {

                        Console.WriteLine("Hello");
                        sendEMailThroughOUTLOOK();
                    }
                }
            }
        }
    }
}

Open in new window


-saige-
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Outlook

From novice to tech pro — start learning today.