VB.Net and Outlook problem

crypto85
07-29-2004, 10:23 AM
I'm trying to work with another mailbox (not the default one) in Outlook 2000. I've constructed a way to read in a path from the visible folder in outlook. It reads in the path fine, does a string replace then an array split. The array split is the last spot where the program works. First, here is my code:

Imports System.IO
Imports System.Reflection
Imports System.Math

Module Module1

Sub Main()

Dim strFolderPath As String
Dim oApp As Outlook.Application
Dim oNS As Outlook.NameSpace
Dim colFolders As Outlook.Folders
Dim oFolder As Outlook.MAPIFolder
Dim arrFolders() As String
Dim i As Long

strFolderPath = "Mailbox CQ - Notify/Sent Items"

strFolderPath = Replace(strFolderPath, "/", "\")
arrFolders = Split(strFolderPath, "\")

Console.WriteLine(arrFolders(0))
Console.WriteLine(arrFolders(1))

oApp = New Outlook.Application
oNS = oApp.GetNamespace("MAPI")
oFolder = oNS.Folders.Item(arrFolders(0))

If Not oFolder Is Nothing Then
For i = 1 To UBound(arrFolders)
colFolders = oFolder.Folders
oFolder = Nothing
oFolder = colFolders.Item(arrFolders(i))
If oFolder Is Nothing Then
Exit For
End If
Next
End If

colFolders = Nothing
oNS = Nothing
oApp = Nothing
End Sub

End Module

The fatal error occurs on the oFolder = oNS.Folders.Item(arrFolders(0)). When I try to run the program, a "Unhandled exception of type System.Runtime.InteropServices.COMException occured in Console Application1.exe. Additional information: the operation failed. An object could not be found."

I checked Locals and arrFolders has a length of 2 with element 0 = Mailbox - CQ Notify and element 1 = Sent Items. I've tried taking out the spaces thinking thay may cause it to break but it isn't. oFolder has a value of Nothing before the statement, and since the statement is never executed it has a value of Nothing when the program breaks.

I tried substituting strFolderPath for arrFolders(0)...no dice. Next I tried pasting in the text of the string in quotes for the value. The line still breaks. The code above is the project in its entireity, I made a seperate project to test this out.

Is there something else I need to import? Some additional COM module besides the one for Outlook I need to attach?

I attached a file of what the mailbox and folder look like in outlook.

herilane
07-29-2004, 11:19 AM
Welcome to the forum. :)

If you try the same code directly in Outlook VBA, what happens? Does it work? Does it give a more helpful error message? (Let me know if you need help converting this to VBA code - but it looks like you know what you're doing there.)

Also, note how you can use the [vb] tags to format the code you post. :)

crypto85
07-29-2004, 01:25 PM
Welcome to the forum. :)

If you try the same code directly in Outlook VBA, what happens? Does it work? Does it give a more helpful error message? (Let me know if you need help converting this to VBA code - but it looks like you know what you're doing there.)

Also, note how you can use the tags to format the code you post. :)


No problem. Here is the VBA code:


Sub GetFold()

Dim objApp As Outlook.Application
Dim objNS As Outlook.NameSpace
Dim colFolders As Outlook.Folders
Dim objFolder As Outlook.MAPIFolder
Dim arrFolders() As String
Dim I As Long
On Error Resume Next

strFolderPath = "Mailbox - CQ Notify/Sent Items"

strFolderPath = Replace(strFolderPath, "/", "\")
arrFolders() = Split(strFolderPath, "\")
Set objApp = CreateObject("Outlook.Application")
Set objNS = objApp.GetNamespace("MAPI")
Set objFolder = objNS.Folders.Item(arrFolders(0))
If Not objFolder Is Nothing Then
For I = 1 To UBound(arrFolders)
Set colFolders = objFolder.Folders
Set objFolder = Nothing
Set objFolder = colFolders.Item(arrFolders(I))
If objFolder Is Nothing Then
Exit For
End If
Next
End If

Set GetFolder = objFolder
Set colFolders = Nothing
Set objNS = Nothing
Set objApp = Nothing

End Sub

So the killer is that this code works flawlessly. There is only one additional line, the Set GetFolder = objFolder. The .NET app never got this far, so that can't be the problem. I fooled around a little with the oNS.Folders.GetFirst and GetNext methods, and the code will run but I don't think it is doing what I need it to do. Is all this some deficiency in .NET? I've been told to either choose VB.NET or VB script for this app, and VB script simply is not robust enough to do everything that I need the app to do. Is it time to make the case for VB6? Is VBA and VB6 code cross-compatible? If so, I can convert the .NET app into VB6, add the working VB6 code and basically be done with the app in a day.

Thanks for your advice.

Mike Rosenblum
07-29-2004, 01:43 PM
VB6 and VBA are far more cross-compatible than is VB.Net. So, yes, if you have the skill-set, you'll find this a lot smoother using VB6.

That said, there's no good reason that this should not work in .Net. I'm not an Outlook guy, so I can't really give any specific advice, but have you tried using 'Option Strict On'? It will likely force you to add a number of 'CType()' functions at various locations (the compiler will tell you where), but in doing so, it may actually identify and/or fix your problem.

But if this is a large App doing a lot of Automation of Legacy COM applications, you can expect to hit a significant number of snags like this in .Net that would never occur in VB6 or VBA. You can almost always work through them, but VB6/VBA is a lot smoother to work with...

crypto85
07-29-2004, 02:30 PM
VB6 and VBA are far more cross-compatible than is VB.Net. So, yes, if you have the skill-set, you'll find this a lot smoother using VB6.

That said, there's no good reason that this should not work in .Net. I'm not an Outlook guy, so I can't really give any specific advice, but have you tried using 'Option Strict On'? It will likely force you to add a number of 'CType()' functions at various locations (the compiler will tell you where), but in doing so, it may actually identify and/or fix your problem.

But if this is a large App doing a lot of Automation of Legacy COM applications, you can expect to hit a significant number of snags like this in .Net that would never occur in VB6 or VBA. You can almost always work through them, but VB6/VBA is a lot smoother to work with...

Mike-

Thanks so much for your help and advice. It is looking more like this should be done in VB6 and here is why.

I added Option Strict On at the very top of my code before the import statements. This required that I change i to an integer instead of a long...no problem. Interestingly, it broke on oFolder = colFolders.Item(arrFolders(i)). So with i as an int...guess what happens?!? You got it! Same error as before, on the same line! This is so simple, and works in VB6, so can you think of any advantages to .NET at this point?

Thanks so much!

Mike Rosenblum
07-29-2004, 03:32 PM
No, absolutely no advantages to using .Net to Automate Legacy COM. Actually, only headaches are involved. What .Net is great at is, well, ".Net Stuff". For example, where possible, I would use ASP.Net, ADO.Net, etc (but, again, not if communicating with Legacy COM). The language advantages of .Net include a true OOP structure, strong-typing under Option Strict, superior Forms & Controls, etc...

These are all very nice and I really look forward to the day when Automating MS Office in .Net is a more viable undertaking, for I love the .Net language... But for now, I think that Automating MS Office from .Net is generally just asking for headaches, particularly for a large App.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum