Option Explicit On
'Add a reference to MS Outlook xx.0 Object Library
'Project > Add Reference... > COM tab > select MS Outlook 11.0 Object library
Imports Microsoft.Office.Interop
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Outlook Variables "
Private moApp As Outlook.Application
Private moNS As Outlook.NameSpace
Private moFolder As Outlook.MAPIFolder
Private mbTerminate As Boolean
#End Region
#Region " Outlook Constants "
Private Const olMail = 43
Private Const olFormatUnspecified = 0
Private Const olFormatPlain = 1
Private Const olFormatHTML = 2
Private Const olFormatRichText = 3
#End Region
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents btnClose As System.Windows.Forms.Button
Friend WithEvents btnBrowse As System.Windows.Forms.Button
Friend WithEvents txtFolderPath As System.Windows.Forms.TextBox
Friend WithEvents lblFolderPath As System.Windows.Forms.Label
Friend WithEvents btnParse As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.btnClose = New System.Windows.Forms.Button
Me.btnBrowse = New System.Windows.Forms.Button
Me.txtFolderPath = New System.Windows.Forms.TextBox
Me.lblFolderPath = New System.Windows.Forms.Label
Me.btnParse = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'btnClose
'
Me.btnClose.Location = New System.Drawing.Point(208, 96)
Me.btnClose.Name = "btnClose"
Me.btnClose.TabIndex = 0
Me.btnClose.Text = "&Close"
'
'btnBrowse
'
Me.btnBrowse.Location = New System.Drawing.Point(264, 40)
Me.btnBrowse.Name = "btnBrowse"
Me.btnBrowse.Size = New System.Drawing.Size(20, 20)
Me.btnBrowse.TabIndex = 1
Me.btnBrowse.Text = "..."
'
'txtFolderPath
'
Me.txtFolderPath.Location = New System.Drawing.Point(16, 40)
Me.txtFolderPath.Name = "txtFolderPath"
Me.txtFolderPath.Size = New System.Drawing.Size(248, 20)
Me.txtFolderPath.TabIndex = 2
Me.txtFolderPath.Text = ""
'
'lblFolderPath
'
Me.lblFolderPath.Location = New System.Drawing.Point(16, 24)
Me.lblFolderPath.Name = "lblFolderPath"
Me.lblFolderPath.Size = New System.Drawing.Size(100, 16)
Me.lblFolderPath.TabIndex = 3
Me.lblFolderPath.Text = "Folder Path:"
'
'btnParse
'
Me.btnParse.Location = New System.Drawing.Point(124, 96)
Me.btnParse.Name = "btnParse"
Me.btnParse.TabIndex = 4
Me.btnParse.Text = "Parse Links"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 126)
Me.Controls.Add(Me.btnParse)
Me.Controls.Add(Me.lblFolderPath)
Me.Controls.Add(Me.txtFolderPath)
Me.Controls.Add(Me.btnBrowse)
Me.Controls.Add(Me.btnClose)
Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle
Me.MaximizeBox = False
Me.Name = "Form1"
Me.Text = "Outlook Link Parser™"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub btnParse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnParse.Click
Dim oEmail As Outlook.MailItem
Dim i As Integer
For i = 1 To moFolder.Items.Count
'Check for mailitems only and not undeliverable errors, meeting requests, or other non-mail items.
If moFolder.Items(i).Class = olMail Then
oEmail = moFolder.Items.Item(i)
'Check for body format type
Select Case oEmail.BodyFormat
Case Outlook.OlBodyFormat.olFormatUnspecified
'Try to parse using plain text format?
Case Outlook.OlBodyFormat.olFormatPlain
'Try to parse text
If oEmail.Body.IndexOf("http://www.vbforums.com") > 0 Then
Console.WriteLine(oEmail.Body.Substring(oEmail.Body.IndexOf("http://www.vbforums.com"), 23))
End If
Case Outlook.OlBodyFormat.olFormatHTML
'Try to parse html
If oEmail.HTMLBody.IndexOf("http://www.vbforums.com") > 0 Then
'Do other stuff
'...
End If
Case Outlook.OlBodyFormat.olFormatRichText
'Try to parse RTF
End Select
oEmail = Nothing
End If
Next
oEmail = Nothing
Me.Activate()
MessageBox.Show("Done!", "Outlook Link Parser™", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
mbTerminate = False
moApp = GetObject(, "Outlook.Application")
If moApp Is Nothing Then
moApp = New Outlook.Application
mbTerminate = True
End If
moNS = moApp.GetNamespace("MAPI")
End Sub
Private Sub btnBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowse.Click
moFolder = moNS.PickFolder
If Not moFolder Is Nothing Then
txtFolderPath.Text = moFolder.FolderPath
End If
Me.Activate()
End Sub
Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
Me.Dispose()
End Sub
Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
moFolder = Nothing
moNS = Nothing
If Not (moApp Is Nothing) And (mbTerminate = True) Then
moApp.Quit()
End If
moApp = Nothing
End Sub
End Class