Is there any other way to do this using VB.Net? The problem is the COM Exceptions that arise when using COM objects.
Thanks in advance.
EDIT: Here's the code from VB.Net that opens an excel file during the startup of the form:
_excelApp = CreateObject("Excel.Application")
_excelApp.Visible = visible
_workbook = _excelApp.Workbooks.Open(_sysFileName)
And then this is the code that runs a macro inside the excel file:
_excelApp.Run("GetEmail", EmailId, SubjectName)
Here's the VBA code of the macro GetEmail:
Option Explicit
Public Sub GetEmail(ByVal EmailId As String, ByVal SubjectName As String)
Dim folderItems As Object
Dim strTmp As String
Dim resultItem As Object
Dim oApp As Object
On Error GoTo ErrorHandler
Set oApp = CreateObject("Outlook.Application")
Dim SourceFolder As Object
Dim DestinationFolder As Object
Dim searchCriteria As String
Set SourceFolder = oApp.Session.Folders("test@email.com").Folders("testFolderName").Folders("testFolderName2").Folders("testFolderName3").Folders("ASSIGNED")
Set DestinationFolder = oApp.Session.Folders("test@email.com").Folders("testFolderName").Folders("testFolderName2").Folders("testFolderName3").Folders("USERSFOLDER").Folders(Environ("USERNAME"))
searchCriteria = "@SQL=" & Chr(34) & "http://schemas.microsoft.com/mapi/proptag/0x1035001F" & Chr(34) & " = '" & EmailId & "'"
Set folderItems = SourceFolder.Items.Restrict(searchCriteria)
If folderItems.Count > 0 Then
For Each resultItem In folderItems
If TypeName(resultItem) = "MailItem" Then
resultItem.Move DestinationFolder
End If
Set resultItem = Nothing
Next
End If
Done:
If folderItems Is Nothing Then
Set oApp = Nothing
Set SourceFolder = Nothing
Set folderItems = Nothing
End If
Exit Sub
ErrorHandler:
strTmp = strTmp & vbCrLf & " Description " & Err.Description & " Subject Name: " & SubjectName
MsgBox strTmp
On Error GoTo 0
GoTo Done
End Sub
The code sometimes throws exceptions at
Set oApp = CreateObject("Outlook.Application")
System.Runtime.InteropServices.ComException(0x800A03EC): Exception from HRESULT: 0x800A03EC)
Sometimes this one:
System.Runtime.InteropServices.ComException(0x80010001): Call was rejected by callee. (Exception from HRESULT: 0x80010001 (RPC_E_CALL_REJECTED))
Our client is using Outlook 2010.
User contributions licensed under CC BY-SA 3.0