The following is code on how to read email using VB.NET.
Dim otkApp As Outlook.Application = New Outlook.Application Dim otkMailItem = "IPM.Note" Dim otkNameSpace As Outlook.NameSpace = otkApp.GetNamespace("MAPI") Dim otkInboxFolder As Outlook.MAPIFolder = otkNameSpace.GetDefaultFolder _ (Outlook.OlDefaultFolders.olFolderInbox) Dim otkMailItems As Outlook.Items = otkInboxFolder.Items
Dim otkMessage As Outlook.MailItem Dim iCntr As Integer For iCntr = 1 To otkMailItems.Count If otkMailItems.Item(iCntr).MessageClass = otkMailItem Then otkMessage = otkMailItems.Item(iCntr) Console.WriteLine(iCntr) Console.WriteLine(otkMessage.SenderName) Console.WriteLine(otkMessage.Subject) Console.WriteLine(otkMessage.ReceivedTime) Console.WriteLine(otkMessage.Body) Console.WriteLine("______________________________") End If Next
Dim XLSApp As New Excel.Application XLSApp.Visible = True Dim XLSWbks As Excel.Workbooks = XLSApp.Workbooks Dim XLSWbk As Excel.Workbook = XLSWbks.Add Dim XLSShts As Excel.Sheets = XLSWbk.Worksheets Dim XLSSht As Excel.Worksheet = XLSShts("Sheet1")
XLSSht.Range("A1").Value = "This code was added using the Excel Object Model in VB.NET" XLSSht.SaveAs("G:my2007ExcelWorkbook.xlsx")
Dim myItems As Object, myItem As Object, myAttachments As Object, myAttachment As Object Dim myOrt As String, myOlApp As New Outlook.Application, myOlExp As Outlook.Explorer Dim Selecttion_ As Outlook.Selection Dim bodyarray() As String Dim timearray() As String
On Error Resume Next
Set myOlExp = myOlApp.ActiveExplorer Set Selecttion_ = myOlExp.Selection
Dim sh As Object, NextRow As Object Set sh = xlApp.ActiveSheet ' ???????? ???? Excel
'for all items do... For Each myItem In Selecttion_
Set NextRow = sh.Range("B" & sh.Rows.Count).End(-4162).Offset(1) ' ?????? ????????????? ?????? ? ??????? C
Dim body, time, fullbody As String
body = bodyarray(0) time = timearray(1) fullbody = body & " " & "??????: " & myItem.To & " " & myItem.CC
NextRow.Resize(, 4).Value = Array(time, time, myItem.Subject, fullbody) body = Nothing time = Nothing Next
Set myItems = Nothing: Set myItem = Nothing Set myAttachments = Nothing: Set myAttachment = Nothing Set myOlApp = Nothing: Set myOlExp = Nothing: Set Selecttion_ = Nothing End Sub
Comments
Dim otkApp As Outlook.Application = New Outlook.Application
Dim otkMailItem = "IPM.Note"
Dim otkNameSpace As Outlook.NameSpace = otkApp.GetNamespace("MAPI")
Dim otkInboxFolder As Outlook.MAPIFolder = otkNameSpace.GetDefaultFolder _
(Outlook.OlDefaultFolders.olFolderInbox)
Dim otkMailItems As Outlook.Items = otkInboxFolder.Items
Dim otkMessage As Outlook.MailItem
Dim iCntr As Integer
For iCntr = 1 To otkMailItems.Count
If otkMailItems.Item(iCntr).MessageClass = otkMailItem Then
otkMessage = otkMailItems.Item(iCntr)
Console.WriteLine(iCntr)
Console.WriteLine(otkMessage.SenderName)
Console.WriteLine(otkMessage.Subject)
Console.WriteLine(otkMessage.ReceivedTime)
Console.WriteLine(otkMessage.Body)
Console.WriteLine("______________________________")
End If
Next
otkApp = Nothing
otkNameSpace = Nothing
otkMailItems = Nothing
otkMessage = Nothing
Here is the code to export data to Excel:
Imports Microsoft.Office.Interop
Dim XLSApp As New Excel.Application
XLSApp.Visible = True
Dim XLSWbks As Excel.Workbooks = XLSApp.Workbooks
Dim XLSWbk As Excel.Workbook = XLSWbks.Add
Dim XLSShts As Excel.Sheets = XLSWbk.Worksheets
Dim XLSSht As Excel.Worksheet = XLSShts("Sheet1")
XLSSht.Range("A1").Value = "This code was added using the Excel Object Model in VB.NET"
XLSSht.SaveAs("G:my2007ExcelWorkbook.xlsx")
Hope this helps!
-Jaime
thx anyway
Sub MyFirstMacros()
Set xlApp = GetObject(, "Excel.Application")
Dim myItems As Object, myItem As Object, myAttachments As Object, myAttachment As Object
Dim myOrt As String, myOlApp As New Outlook.Application, myOlExp As Outlook.Explorer
Dim Selecttion_ As Outlook.Selection
Dim bodyarray() As String
Dim timearray() As String
On Error Resume Next
Set myOlExp = myOlApp.ActiveExplorer
Set Selecttion_ = myOlExp.Selection
Dim sh As Object, NextRow As Object
Set sh = xlApp.ActiveSheet ' ???????? ???? Excel
'for all items do...
For Each myItem In Selecttion_
bodyarray = Split(myItem.body, vbNewLine)
timearray = Split(myItem.SentOn, " ")
Set NextRow = sh.Range("B" & sh.Rows.Count).End(-4162).Offset(1) ' ?????? ????????????? ?????? ? ??????? C
Dim body, time, fullbody As String
body = bodyarray(0)
time = timearray(1)
fullbody = body & " " & "??????: " & myItem.To & " " & myItem.CC
NextRow.Resize(, 4).Value = Array(time, time, myItem.Subject, fullbody)
body = Nothing
time = Nothing
Next
Set myItems = Nothing: Set myItem = Nothing
Set myAttachments = Nothing: Set myAttachment = Nothing
Set myOlApp = Nothing: Set myOlExp = Nothing: Set Selecttion_ = Nothing
End Sub