Click here to Skip to main content
15,887,083 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
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!

This is what I have done so far :

What I have tried:

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;

using System.Collections;

namespace XYZ 
{ 
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 
{ 
AX_PATH = args[0]; 
}

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

exception = ""; 
try 
{ 
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(); 
}

} 
}

}
Posted
Updated 13-Apr-17 20:49pm

Quote:
Infact, the compiler can't reach the "for" loop.

non sense, wrong assumption.
Advice: avoid try/catch until your code is debugged, it just hude problems and make debugging more complicated.

When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
Quote:
Even after removing try catch error comes

Which error ?
give exact error message and position.
 
Share this answer
 
v2
Even after removing try catch error comes
 
Share this answer
 
Comments
Patrice T 14-Apr-17 3:01am    
Not a solution !
please delete and update your question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900