EDN Admin
Well-known member
Hi,
There seemed to be a very good code example that would do just what I needed to do:
"How to use the Microsoft Outlook Object Library to retrieve a message from the Inbox by using Visual C#"
{ http://support.microsoft.com/default.aspx?id=310258 http://support.microsoft.com/default.aspx?id=310258 }
I had then applied a small modification to that code in order to read and show all messages from the inbox. The first message was read with GetFirst(), every following message with GetNext().
But that does not work well for all of the messages in my inbox. With approx every second or third message, the code
<font size=1>oMsg = (Outlook.</font><font color="#2b91af" size=1><font color="#2b91af" size=1>MailItem</font></font><font size=1>)oItems.GetNext();
throws an exception with the following message:
"Unable to cast COM object of type System.__ComObject to interface type Microsoft.Office.Interop.Outlook.MailItem. This operation failed because the QueryInterface call on the COM component for the interface with IID {00063034-0000-0000-C000-000000000046} failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE))."
When I manually move the execution pointer to read the next message I ight be successful again but then fail one or two messages later. Always the same messages are affected. Looking at the messages from inside Outlook I cannot see any specific reason.
Does anybody have an idea why my code shows this strange behaviour?
Here is the code I am trying:
<div style="border-right:#7f9db9 1px solid;border-top:#7f9db9 1px solid;font-size:11px;overflow:auto;border-left:#7f9db9 1px solid;line-height:100%! important;border-bottom:#7f9db9 1px solid;font-family:Courier New;background-color:white
<table style="border-top-width:0px;border-left-width:0px;margin:2px 0px;width:99%;border-bottom:#eee 0px solid;border-collapse:collapse;background-color:#fff;border-right-width:0px" cellspacing=0 cellpadding=0>
<colgroup>
<col style="border-right:gray 1px solid;padding-right:5px;padding-left:10px;font-size:11px;vertical-align:top;width:5px;color:gray;font-family:Courier New;background-color:#eee;text-align:right
<col style="padding-left:10px;font-size:11px;border-bottom:#f7f7f7 1px solid;font-family:Courier New;white-space:nowrap
<tbody>
<tr>
<td><nobr>1</nobr></td>
<td><font style="font-size:11px public static int Main(string[] args) </font></td></tr>
<tr>
<td><nobr>2</nobr></td>
<td style="background-color:#f7f7f7 { </td></tr>
<tr>
<td><nobr>3</nobr></td>
<td> Outlook.Application </font><font style="color:red oApp</font><font style="font-size:11px = </font><font style="color:blue null</font><font style="font-size:11px ; </font></td></tr>
<tr>
<td><nobr>4</nobr></td>
<td style="background-color:#f7f7f7 Outlook.NameSpace </font><font style="color:red oNS</font><font style="font-size:11px = </font><font style="color:blue null</font><font style="font-size:11px ; </font></td></tr>
<tr>
<td><nobr>5</nobr></td>
<td> Outlook.MAPIFolder </font><font style="color:red oInbox</font><font style="font-size:11px = </font><font style="color:blue null</font><font style="font-size:11px ; </font></td></tr>
<tr>
<td><nobr>6</nobr></td>
<td style="background-color:#f7f7f7 Outlook.Items </font><font style="color:red oItems</font><font style="font-size:11px = </font><font style="color:blue null</font><font style="font-size:11px ; </font></td></tr>
<tr>
<td><nobr>7</nobr></td>
<td> Outlook.MailItem </font><font style="color:red oMsg</font><font style="font-size:11px = </font><font style="color:blue null</font><font style="font-size:11px ; </font></td></tr>
<tr>
<td><nobr>8</nobr></td>
<td style="background-color:#f7f7f7 </td></tr>
<tr>
<td><nobr>9</nobr></td>
<td> try </td></tr>
<tr>
<td><nobr>10</nobr></td>
<td style="background-color:#f7f7f7 { </td></tr>
<tr>
<td><nobr>11</nobr></td>
<td> // Create the Outlook application. </td></tr>
<tr>
<td><nobr>12</nobr></td>
<td style="background-color:#f7f7f7 // in-line initialization </td></tr>
<tr>
<td><nobr>13</nobr></td>
<td> </font><font style="color:red oApp</font><font style="font-size:11px = </font><font style="color:blue new</font><font style="font-size:11px Outlook.Application(); </font></td></tr>
<tr>
<td><nobr>14</nobr></td>
<td style="background-color:#f7f7f7 </td></tr>
<tr>
<td><nobr>15</nobr></td>
<td> // Get the MAPI namespace. </td></tr>
<tr>
<td><nobr>16</nobr></td>
<td style="background-color:#f7f7f7 </font><font style="color:red oNS</font><font style="font-size:11px = </font><font style="color:blue oApp</font><font style="font-size:11px .GetNamespace("mapi"); </font></td></tr>
<tr>
<td><nobr>17</nobr></td>
<td> </td></tr>
<tr>
<td><nobr>18</nobr></td>
<td style="background-color:#f7f7f7 // Log on by using the default profile or existing session (no dialog box). </td></tr>
<tr>
<td><nobr>19</nobr></td>
<td> oNS.Logon(Missing.Value, Missing.Value, false, true); </td></tr>
<tr>
<td><nobr>20</nobr></td>
<td style="background-color:#f7f7f7 </td></tr>
<tr>
<td><nobr>21</nobr></td>
<td> //Get the Inbox folder. </td></tr>
<tr>
<td><nobr>22</nobr></td>
<td style="background-color:#f7f7f7 </font><font style="color:red oInbox</font><font style="font-size:11px = </font><font style="color:blue oNS</font><font style="font-size:11px .GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox); </font></td></tr>
<tr>
<td><nobr>23</nobr></td>
<td> </td></tr>
<tr>
<td><nobr>24</nobr></td>
<td style="background-color:#f7f7f7 //Get the Items collection in the Inbox folder. </td></tr>
<tr>
<td><nobr>25</nobr></td>
<td> </font><font style="color:red oItems</font><font style="font-size:11px = </font><font style="color:blue oInbox</font><font style="font-size:11px .Items; </font></td></tr>
<tr>
<td><nobr>26</nobr></td>
<td style="background-color:#f7f7f7 </td></tr>
<tr>
<td><nobr>27</nobr></td>
<td> // Get the first message. </td></tr>
<tr>
<td><nobr>28</nobr></td>
<td style="background-color:#f7f7f7 // Because the Items folder may contain different item types, </td></tr>
<tr>
<td><nobr>29</nobr></td>
<td> // use explicit typecasting with the assignment. </td></tr>
<tr>
<td><nobr>30</nobr></td>
<td style="background-color:#f7f7f7 </font><font style="color:red oMsg</font><font style="font-size:11px = (Outlook.MailItem)oItems.GetFirst(); </font></td></tr>
<tr>
<td><nobr>31</nobr></td>
<td> </td></tr>
<tr>
<td><nobr>32</nobr></td>
<td style="background-color:#f7f7f7 RLoop: </td></tr>
<tr>
<td><nobr>33</nobr></td>
<td> //Output some common properties. </td></tr>
<tr>
<td><nobr>34</nobr></td>
<td style="background-color:#f7f7f7 Console.WriteLine("Received: "+oMsg.ReceivedTime.ToString()); </td></tr>
<tr>
<td><nobr>35</nobr></td>
<td> //Console.WriteLine(oMsg.Subject); </td></tr>
<tr>
<td><nobr>36</nobr></td>
<td style="background-color:#f7f7f7 //Console.WriteLine(oMsg.SenderName); </td></tr>
<tr>
<td><nobr>37</nobr></td>
<td> //Console.WriteLine(oMsg.Body); </td></tr>
<tr>
<td><nobr>38</nobr></td>
<td style="background-color:#f7f7f7 </td></tr>
<tr>
<td><nobr>39</nobr></td>
<td> </font><font style="color:red oMsg</font><font style="font-size:11px = (Outlook.MailItem)oItems.GetNext(); </font></td></tr>
<tr>
<td><nobr>40</nobr></td>
<td style="background-color:#f7f7f7 goto RLoop; </td></tr>
<tr>
<td><nobr>41</nobr></td>
<td> } </td></tr>
<tr>
<td><nobr>42</nobr></td>
<td style="background-color:#f7f7f7 catch (Exception e) </td></tr>
<tr>
<td><nobr>43</nobr></td>
<td> { </td></tr>
<tr>
<td><nobr>44</nobr></td>
<td style="background-color:#f7f7f7 Console.WriteLine("{0} Exception caught: ", e); </td></tr>
<tr>
<td><nobr>45</nobr></td>
<td> } </td></tr>
<tr>
<td><nobr>46</nobr></td>
<td style="background-color:#f7f7f7 </td></tr></tbody></table> </font>
View the full article
There seemed to be a very good code example that would do just what I needed to do:
"How to use the Microsoft Outlook Object Library to retrieve a message from the Inbox by using Visual C#"
{ http://support.microsoft.com/default.aspx?id=310258 http://support.microsoft.com/default.aspx?id=310258 }
I had then applied a small modification to that code in order to read and show all messages from the inbox. The first message was read with GetFirst(), every following message with GetNext().
But that does not work well for all of the messages in my inbox. With approx every second or third message, the code
<font size=1>oMsg = (Outlook.</font><font color="#2b91af" size=1><font color="#2b91af" size=1>MailItem</font></font><font size=1>)oItems.GetNext();
throws an exception with the following message:
"Unable to cast COM object of type System.__ComObject to interface type Microsoft.Office.Interop.Outlook.MailItem. This operation failed because the QueryInterface call on the COM component for the interface with IID {00063034-0000-0000-C000-000000000046} failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE))."
When I manually move the execution pointer to read the next message I ight be successful again but then fail one or two messages later. Always the same messages are affected. Looking at the messages from inside Outlook I cannot see any specific reason.
Does anybody have an idea why my code shows this strange behaviour?
Here is the code I am trying:
<div style="border-right:#7f9db9 1px solid;border-top:#7f9db9 1px solid;font-size:11px;overflow:auto;border-left:#7f9db9 1px solid;line-height:100%! important;border-bottom:#7f9db9 1px solid;font-family:Courier New;background-color:white
<table style="border-top-width:0px;border-left-width:0px;margin:2px 0px;width:99%;border-bottom:#eee 0px solid;border-collapse:collapse;background-color:#fff;border-right-width:0px" cellspacing=0 cellpadding=0>
<colgroup>
<col style="border-right:gray 1px solid;padding-right:5px;padding-left:10px;font-size:11px;vertical-align:top;width:5px;color:gray;font-family:Courier New;background-color:#eee;text-align:right
<col style="padding-left:10px;font-size:11px;border-bottom:#f7f7f7 1px solid;font-family:Courier New;white-space:nowrap
<tbody>
<tr>
<td><nobr>1</nobr></td>
<td><font style="font-size:11px public static int Main(string[] args) </font></td></tr>
<tr>
<td><nobr>2</nobr></td>
<td style="background-color:#f7f7f7 { </td></tr>
<tr>
<td><nobr>3</nobr></td>
<td> Outlook.Application </font><font style="color:red oApp</font><font style="font-size:11px = </font><font style="color:blue null</font><font style="font-size:11px ; </font></td></tr>
<tr>
<td><nobr>4</nobr></td>
<td style="background-color:#f7f7f7 Outlook.NameSpace </font><font style="color:red oNS</font><font style="font-size:11px = </font><font style="color:blue null</font><font style="font-size:11px ; </font></td></tr>
<tr>
<td><nobr>5</nobr></td>
<td> Outlook.MAPIFolder </font><font style="color:red oInbox</font><font style="font-size:11px = </font><font style="color:blue null</font><font style="font-size:11px ; </font></td></tr>
<tr>
<td><nobr>6</nobr></td>
<td style="background-color:#f7f7f7 Outlook.Items </font><font style="color:red oItems</font><font style="font-size:11px = </font><font style="color:blue null</font><font style="font-size:11px ; </font></td></tr>
<tr>
<td><nobr>7</nobr></td>
<td> Outlook.MailItem </font><font style="color:red oMsg</font><font style="font-size:11px = </font><font style="color:blue null</font><font style="font-size:11px ; </font></td></tr>
<tr>
<td><nobr>8</nobr></td>
<td style="background-color:#f7f7f7 </td></tr>
<tr>
<td><nobr>9</nobr></td>
<td> try </td></tr>
<tr>
<td><nobr>10</nobr></td>
<td style="background-color:#f7f7f7 { </td></tr>
<tr>
<td><nobr>11</nobr></td>
<td> // Create the Outlook application. </td></tr>
<tr>
<td><nobr>12</nobr></td>
<td style="background-color:#f7f7f7 // in-line initialization </td></tr>
<tr>
<td><nobr>13</nobr></td>
<td> </font><font style="color:red oApp</font><font style="font-size:11px = </font><font style="color:blue new</font><font style="font-size:11px Outlook.Application(); </font></td></tr>
<tr>
<td><nobr>14</nobr></td>
<td style="background-color:#f7f7f7 </td></tr>
<tr>
<td><nobr>15</nobr></td>
<td> // Get the MAPI namespace. </td></tr>
<tr>
<td><nobr>16</nobr></td>
<td style="background-color:#f7f7f7 </font><font style="color:red oNS</font><font style="font-size:11px = </font><font style="color:blue oApp</font><font style="font-size:11px .GetNamespace("mapi"); </font></td></tr>
<tr>
<td><nobr>17</nobr></td>
<td> </td></tr>
<tr>
<td><nobr>18</nobr></td>
<td style="background-color:#f7f7f7 // Log on by using the default profile or existing session (no dialog box). </td></tr>
<tr>
<td><nobr>19</nobr></td>
<td> oNS.Logon(Missing.Value, Missing.Value, false, true); </td></tr>
<tr>
<td><nobr>20</nobr></td>
<td style="background-color:#f7f7f7 </td></tr>
<tr>
<td><nobr>21</nobr></td>
<td> //Get the Inbox folder. </td></tr>
<tr>
<td><nobr>22</nobr></td>
<td style="background-color:#f7f7f7 </font><font style="color:red oInbox</font><font style="font-size:11px = </font><font style="color:blue oNS</font><font style="font-size:11px .GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox); </font></td></tr>
<tr>
<td><nobr>23</nobr></td>
<td> </td></tr>
<tr>
<td><nobr>24</nobr></td>
<td style="background-color:#f7f7f7 //Get the Items collection in the Inbox folder. </td></tr>
<tr>
<td><nobr>25</nobr></td>
<td> </font><font style="color:red oItems</font><font style="font-size:11px = </font><font style="color:blue oInbox</font><font style="font-size:11px .Items; </font></td></tr>
<tr>
<td><nobr>26</nobr></td>
<td style="background-color:#f7f7f7 </td></tr>
<tr>
<td><nobr>27</nobr></td>
<td> // Get the first message. </td></tr>
<tr>
<td><nobr>28</nobr></td>
<td style="background-color:#f7f7f7 // Because the Items folder may contain different item types, </td></tr>
<tr>
<td><nobr>29</nobr></td>
<td> // use explicit typecasting with the assignment. </td></tr>
<tr>
<td><nobr>30</nobr></td>
<td style="background-color:#f7f7f7 </font><font style="color:red oMsg</font><font style="font-size:11px = (Outlook.MailItem)oItems.GetFirst(); </font></td></tr>
<tr>
<td><nobr>31</nobr></td>
<td> </td></tr>
<tr>
<td><nobr>32</nobr></td>
<td style="background-color:#f7f7f7 RLoop: </td></tr>
<tr>
<td><nobr>33</nobr></td>
<td> //Output some common properties. </td></tr>
<tr>
<td><nobr>34</nobr></td>
<td style="background-color:#f7f7f7 Console.WriteLine("Received: "+oMsg.ReceivedTime.ToString()); </td></tr>
<tr>
<td><nobr>35</nobr></td>
<td> //Console.WriteLine(oMsg.Subject); </td></tr>
<tr>
<td><nobr>36</nobr></td>
<td style="background-color:#f7f7f7 //Console.WriteLine(oMsg.SenderName); </td></tr>
<tr>
<td><nobr>37</nobr></td>
<td> //Console.WriteLine(oMsg.Body); </td></tr>
<tr>
<td><nobr>38</nobr></td>
<td style="background-color:#f7f7f7 </td></tr>
<tr>
<td><nobr>39</nobr></td>
<td> </font><font style="color:red oMsg</font><font style="font-size:11px = (Outlook.MailItem)oItems.GetNext(); </font></td></tr>
<tr>
<td><nobr>40</nobr></td>
<td style="background-color:#f7f7f7 goto RLoop; </td></tr>
<tr>
<td><nobr>41</nobr></td>
<td> } </td></tr>
<tr>
<td><nobr>42</nobr></td>
<td style="background-color:#f7f7f7 catch (Exception e) </td></tr>
<tr>
<td><nobr>43</nobr></td>
<td> { </td></tr>
<tr>
<td><nobr>44</nobr></td>
<td style="background-color:#f7f7f7 Console.WriteLine("{0} Exception caught: ", e); </td></tr>
<tr>
<td><nobr>45</nobr></td>
<td> } </td></tr>
<tr>
<td><nobr>46</nobr></td>
<td style="background-color:#f7f7f7 </td></tr></tbody></table> </font>
View the full article