“for”中的错误循环,无法通过outlook发送邮件

编程入门 行业动态 更新时间:2024-10-17 02:54:45
本文介绍了“for”中的错误循环,无法通过outlook发送邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我已经实施了我的工作,但邮件没有收到。事实上,编译器无法到达for循环。我必须提取存在字符d的行(A-H)。假设M6单元格中的M栏中是否存在d,那么我需要提取A6-H6并通过Outlook邮件发送。 请帮助我! 这是我到目前为止所做的: 我有什么试过:

使用System; 使用System.IO; 使用System.Collections.Generic; 使用System.Linq; 使用System.Text; 使用Excel = Microsoft.Office.Interop.Excel; 使用Outlook = Microsoft.Office.Interop.Outlook;使用System.Collections ; 命名空间XYZ { class Program { //向outlook发送电子邮件的方法 public static void sendEMailThroughOUTLOOK() { try { //创建Outlook应用程序。 Outlook.Application oApp = new Outlook.Application(); //创建一个新邮件。 Outlook.MailItem oMsg = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem); //设置HTMLBody。 //添加电子邮件的正文 oMsg.HTMLBody =查看详细信息; //主题行oMsg.Subject =Doc Info; //添加收件人。 Outlook.Recipients oRecips =(Outlook.Recipients)oMsg.Recipients; //如有必要,在下一行更改收件人。 Outlook.Recipient oRecip =(Outlook.Recipient)oRecips.Add(abc@xyz),oRecip.Resolve(); //发送。 oMsg.Send(); //清理。 oRecip = null; oRecips = null; oMsg = null; oApp = null; } catch(exception ex) {} } 私有静态字符串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(让我们开始工作!); Console.WriteLine(按任意键继续); Console.ReadLine(); } 其他 { 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(你好); 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; 对象缺失= System.Type.Missing; for(int i = AXSheet.Cells.get_Range(d,missing).Row + 1; i< = last.Row; i ++) { Console .WriteLine( 你好); sendEMailThroughOUTLOOK(); } } } }

解决方案

Quote:

事实上,编译器无法到达for循环。

没有意义,错误的假设。 建议:在调试代码之前避免尝试/捕获,它只会导致问题并使调试变得更复杂。 当你不理解你的代码在做什么或为什么它做它的作用时,答案是调试器。 使用调试器查看你的代码是什么这样做。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量,这是一个令人难以置信的学习工具。 调试器 - 维基百科,免费的百科全书 [ ^ ] 掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ] 使用Visual Studio 2010进行基本调试 - YouTube [ ^ ] 调试器在这里显示你的代码是什么正在做,你的任务是与它应该做的事情进行比较。 调试器中没有魔法,它没有发现错误,它只是帮助你。当代码没有达到预期的效果时,你就接近了一个bug。

引用:

甚至以后删除try catch错误来了

哪个错误? 给出确切的错误信息和位置。

即使删除后尝试遇到错误

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"), 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(); } } } }

解决方案

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.

Even after removing try catch error comes

更多推荐

“for”中的错误循环,无法通过outlook发送邮件

本文发布于:2023-11-27 22:00:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1639759.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:发送邮件   错误   outlook

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!