Work Samples ► Source Code ► VB.Net
A.R.FRY.
1. Introduction
This document provides a number of examples of VB.Net code that I designed and developed.
It is structured as follows:
Section
2
3
4
5
6
Title
Example(s): Introduction
Example(s): Code
Example(s): Explanation
About the Applications
Notes and Caveats
Content
An explanation of the code in Section 3.
Background information on the applications the code is taken from.
2. Example(s): Introduction
This document contains 2 examples:
Example 1. This example is part of an application (network application) for a smart device (Pocket
PC). The application uses the .Net Compact Framework (CF).
Example 2. This example is part of an application (desktop application) to extract email addresses
from Microsoft Outlook 2007 folders. It uses automation to interact with Outlook.
3. Example(s): Code
Example 1:
' Function: Event handler for 'Submit' button. (Submit collection details)
Private Sub FR_ButSub_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FR_ButSub.Click
Dim curColl As Collection
If Not FR_House.Text = "" Then
If Not FR_Street.Text = "" Then
If Not FR_Weight.Text = "" Then
curColl = New Collection(FR_House.Text, FR_Street.Text, FR_PostCode.Text, FR_Weight.Text)
oCollections.AddEntry(curColl)
FX_NCollRec.Text = oCollections.CountEntries
FR_MsgOut.Text = "Collection details recorded."
oCollections.Send()
FX_NCollSent.Text = oCollections.CountSent
ShowOutfile()
Else
FR_MsgOut.Text = "Error: Weight not specified."
End If
Else
FR_MsgOut.Text = "Error: Street/road not specified."
End If
Else
FR_MsgOut.Text = "Error: House not specified."
End If
End Sub
' Function: ReadIn. Purposes: Reads in work schedule from main office system.
' Description: The work schedule is provided in the form of an XML file.
' This function (a) reads in the file, (b) searches for the relevent
' schedule (for this vehicle), and (c) stores the schedule in this object.
Public Function ReadIn() As Boolean
Dim bFound As Boolean : bFound = False
Dim sVehNo, sDate, sStreetName, sPostcode As String
Dim sCurElement As String
sClassErrMsg = "No schedule for this vehicle."
' first, clear out the existing work schedule...
scheditems.Clear()
' Read and parse XML data...
Try
Dim xr As New XmlTextReader(sSchedulesFile)
While xr.Read()
Select Case xr.NodeType
Case XmlNodeType.Element
If xr.Name = "schedule" Then
sVehNo = ""
sStreetName = ""
sPostcode = ""
End If
If xr.Name = "veh_no" Then
sCurElement = xr.Name
End If
If xr.Name = "date" Then
sCurElement = xr.Name
End If
If xr.Name = "street" Then
End If
If xr.Name = "name" Then
sCurElement = xr.Name
End If
If xr.Name = "postcode" Then
sCurElement = xr.Name
End If
Case XmlNodeType.Text
If sCurElement = "veh_no" Then
sVehNo = xr.Value
End If
If sCurElement = "date" Then
sDate = xr.Value
End If
If sCurElement = "name" Then
sStreetName = xr.Value
End If
If sCurElement = "postcode" Then
sPostcode = xr.Value
End If
Case XmlNodeType.CDATA
Case XmlNodeType.ProcessingInstruction
Case XmlNodeType.Comment
Case XmlNodeType.Document
Case XmlNodeType.Whitespace
Case XmlNodeType.SignificantWhitespace
Case XmlNodeType.EndElement
If xr.Name = "street" Then
If sVehNo = sVehicleNo And Not sStreetName = "" And Not sPostcode = "" Then
' Add schedule item (street) to WorkSchedule object
curSchedItem = New SchedItem(sStreetName, sPostcode)
AddEntry(curSchedItem)
' Update of list view not done here
bFound = True
End If
End If
End Select
End While
xr.Close()
Catch e As System.IO.FileNotFoundException
sClassErrMsg = "Work schedule file not found."
Catch e As XmlException
sClassErrMsg = "XmlException occured"
End Try
Return bFound
End Function
Example 2:
'****************************************
' Program: WEED - (Windows) Extract Email Details
' Program type: Windows application (.exe)
' Version: 0.8
' Date: 21 Feb 2009
'---------------------------------------' Author: Andrew Fry
' Copyright: Copyright (c) 2009 A.R.Fry. All rights reserved.
'****************************************
Imports System.IO
Imports System.Text.RegularExpressions
Public Class Form1
Public Addrs1 As New ArrayList() 'Array of addresses
Public aIgnores() As String ' Array of ignore strings
Public Ignores As String
Public SkipJunk As Boolean
Public CheckSubFolders As Boolean
'****************************************
' Function: ButGo_Click
' Purpose: Handles Go button, on click
'****************************************
Private Sub ButGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButGo.Click
' Initialisation...
Addrs1.Clear()
SkipJunk = True
CheckSubFolders = True
If Not Me.CBIncSF.Checked Then
CheckSubFolders = False
End If
' Get addresses...
GetEmailAddresses()
MsgBox("Done!")
Application.Exit()
End Sub
'****************************************
' Function: GetEmailAddresses
'****************************************
Private Sub GetEmailAddresses()
Dim oOLApp As Microsoft.Office.Interop.Outlook.Application
Dim oOLNS As Microsoft.Office.Interop.Outlook.NameSpace
Dim oOLFolders As Microsoft.Office.Interop.Outlook.Folders
Dim oOLFolder As Microsoft.Office.Interop.Outlook.MAPIFolder ' Top level folder
Dim InFileIgn As String : InFileIgn = Me.TBInF.Text
Dim OutFile As String : OutFile = Me.TBOutF.Text
Dim DoSort As Boolean : DoSort = True
Dim fIn As IO.StreamReader
Dim fOut As IO.StreamWriter
Dim N As Integer
oOLApp = CType(CreateObject("Outlook.Application"), Microsoft.Office.Interop.Outlook.Application)
' Setup Namespace
oOLNS = oOLApp.GetNamespace("MAPI")
'oOLNS = ThisOutlookSession.Session
If InFileIgn <> "" Then
If System.IO.File.Exists(InFileIgn) Then
If FileLen(InFileIgn) > 0 Then
fIn = File.OpenText(InFileIgn)
Ignores = fIn.ReadToEnd
fIn.Close()
aIgnores = Split(Ignores, vbCrLf)
End If
End If
End If
' All folders or specific folder ?
If Me.RBFoldA.Checked Then
oOLFolders = oOLNS.Folders
For Each oOLFolder In oOLFolders
'FolderName = oOLFolder.Name
'Addrs1.Add("Folder: " + FolderName)
'Addrs1.Add("DIT: " + CStr(oOLFolderL0.DefaultItemType))
ProcessFolder(oOLFolder)
Next
Else
oOLFolder = oOLNS.PickFolder
ProcessFolder(oOLFolder)
End If
oOLApp = Nothing
If Me.CBSort.Checked Then
Me.TBStat.Text = "Sorting results..."
Application.DoEvents()
Addrs1.Sort()
End If
' Write results to output file...
fOut = File.CreateText(OutFile)
For N = 0 To Addrs1.Count - 1
fOut.WriteLine(CStr(Addrs1(N)))
Next
fOut.Close()
Addrs1.Clear()
End Sub
'****************************************
' Function: ProcessFolder
' Purpose: ...
' Returns: Array of addresses
'****************************************
Private Sub ProcessFolder(ByVal StartFolder As Microsoft.Office.Interop.Outlook.MAPIFolder)
Dim oFolder As Microsoft.Office.Interop.Outlook.MAPIFolder
Dim oItem As Object
Dim FolderName As String
Dim FolderPath As String
Dim bProceed As Boolean : bProceed = True
On Error Resume Next
FolderName = StartFolder.Name
FolderPath = StartFolder.FolderPath
Me.TBStat.Text = FolderPath
Application.DoEvents()
If SkipJunk = True And (FolderName = "Junk E-mail" Or FolderName = "Spam") Then
bProceed = False
End If
If bProceed Then
' process all the items in this folder...
If StartFolder.Items.Count > 0 Then
For Each oItem In StartFolder.Items
'ItemName = oItem.FullName
'Addrs1.Add("Item: " + ItemName)
' Is it a mail item ?
If TypeOf oItem Is Microsoft.Office.Interop.Outlook.MailItem Then
GetAddrs1(CType(oItem, Microsoft.Office.Interop.Outlook.MailItem))
Me.TBStat.Text = FolderPath + " (" + CStr(Addrs1.Count) + ")"
Application.DoEvents()
End If
Next
End If
' process all the subfolders in this folder...
If CheckSubFolders Then
If StartFolder.Folders.Count > 0 Then
For Each oFolder In StartFolder.Folders
'FolderName = objFolder.Name
'Addrs1.Add("Folder: " + oFolder.FolderPath)
'Addrs1.Add("Folder: " + FolderName)
'Addrs1.Add("DIT: " + CStr(objFolder.DefaultItemType))
ProcessFolder(oFolder)
Next
End If
End If
End If
End Sub
(NB. Remainder of code not shown)
4. Example(s): Explanation
Example 1.
The function FR_ButSub_Click is an event handler. It is tied to the ‘Submit’ button on a form on the
portable device, and is executed when the dustcart team has completed a collection, entered details
of it on the device and then pressed ‘Submit’ to record the details and send them to HQ.
It performs the following steps:
 It checks that all appropriate details are provided
 It creates a new Collection object, stores details of the collection and adds the object to the set of
Collection objects
 It outputs (displays) a message confirming that the collection details have been recorded, and the
number of collections recorded so far
 It sends details of the Collection back to HQ/main office (along with any others awaiting
transmission)
 It outputs the number of collections sent so far
The function ReadIn reads in a work schedule (file) from HQ/main office, then process the message
and extracts the information. The file is an XML document, and the function traverses all of the
nodes in the document in order to locate the work schedule, and then extracts and stores the work
schedule.
Example 2.
The example shows 3 functions.
Function ButGo_Click is an event handler. It is tied to the ‘Go’ button on the form, and is executed
when the user presses this button. It performs some initialization and then calls GetEmailAddresses.
Function GetEmailAddresses is the main function. It performs the following steps:
 It starts Outlook (if not already running).
 It opens and reads the input file (which contains a set of strings to be ignored).
 It determines the starting folder – by checking whether the user has chosen all folders or a
selected folder – and then calls ProcessFolder in order to process the current folder (and
subfolders). This process builds an array of email addresses (into Addrs1()).
 It then sorts the results.
 It then writes the results to the output file.
Function ProcessFolder processes the ‘current’ folder (or subfolder). It performs the following steps:
 Determine if current folder/subfolder should be processed or not.
 Process all items in this folder. For each mail item, call GetAddrs1() to extract email addresses.
 Process all subfolders in this folder. Here, ProcessFolder calls itself, and so operates recursively.
5. About the Applications
WAMS (Waste Management System) is a system or application intended for use by a waste
management company. The system is a distributed or networked system, so that one component
resides on the mainframe at headquarters or main offices, and the other resides on portable devices
used by the mobile dustcart teams. The system allows the two parties to communicate … for
instance, it allows headquarters to send a work schedule to each team (telling it which streets to visit)
and it allows each team to record details of collections and return them to HQ/main office. This latter
function allows real-time tracking of dustcart teams.
This is an MSc project and involves the development of a prototype application, which means that…
 It is concerned with the part of the application which runs on the portable devices only
 It is to be developed and tested on a Pocket PC emulator, not a real portable device
 It is to be developed using Visual Studio.Net 2003/2005, the .Net Compact Framework and
VB.Net
 The mainframe and communications to/from the mainframe are simulated only
The work schedules (HQ => team) and collection data (HQ <= team) take the form of XML
documents/files.
6. Notes and Caveats
 All examples require some understanding of the language concerned. Some examples may require some
understanding of other technologies/mechanisms/interfaces.
 All code has been tested and is known to work.
 Some/all code is taken from commercial product(s) and is provided on a confidential basis.
 To minimize explanation and aid understanding, I have selected pieces of code which are reasonably simple and selfcontained. (If you want something more complex, contact me!).
 Unauthorized (re-)use is not permitted without the author’s explicit permission.