Friday, June 16, 2006

Using .NET Outlook to send formatted email

Imports Microsoft.Office
Imports Microsoft.Office.Core
Imports System.Runtime.InteropServices
Imports Outlook = Microsoft.Office.Interop.Outlook

Public Class TestOutlookMail
Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
Private Sub InitializeComponent()

End Sub
Protected WithEvents Button1 As System.Web.UI.WebControls.Button
Private app As Outlook.ApplicationClass

'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
app = New Outlook.ApplicationClass
End Sub

'Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' CreateEmailMessage()
'End Sub 'button1_Click

'Private Sub CreateEmailMessage()
' 'Initialize the envelope values.
' Dim strTo As String = ""
' Dim strBCC As String = ""
' Dim strCC As String = ""
' Dim strSubject As String = "Outlook Automation"
' Dim strBody As String = "Hello World"

' 'Automate the Outlook mail item.
' app = New Outlook.ApplicationClass
' Dim mItem As Outlook.MailItemClass = CType(app.CreateItem(Outlook.OlItemType.olMailItem), Outlook.MailItemClass)
' 'Dim As Outlook.MailItemClass = CType(doc.MailEnvelope.Item, Outlook.MailItemClass)
' 'Outlook.MailItemClass mi = (Outlook.MailItemClass)app.CreateItem
' mItem.To = strTo
' mItem.BCC = strBCC
' mItem.CC = strCC
' mItem.Subject = strSubject
' mItem.BodyFormat = Outlook.OlBodyFormat.olFormatHTML
' mItem.HTMLBody = strBody
' 'mItem.ItemEvents_Event_Close += New Outlook.ItemEvents_CloseEventHandler(Me.wApp_Close)

' 'wApp.Visible = True

' ' Loop until there are no more references to release.
' While Marshal.ReleaseComObject(mItem) > 0
' End While
' mItem = Nothing

' ' Invoke the .NET garbage collector.
' GC.Collect()
' GC.WaitForPendingFinalizers()
'End Sub 'CreateEmailMessage

Private Sub button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Subject As String = "This is my Subject"
Dim Body As String = "This is my Body"

Dim mi As Outlook.MailItemClass = CType(app.CreateItem(Outlook.OlItemType.olMailItem), Outlook.MailItemClass)
mi.BodyFormat = Outlook.OlBodyFormat.olFormatHTML
mi.HTMLBody = Body
mi.Subject = Subject
mi.Display(New Object)

Dim ni As Outlook.NoteItemClass = CType(app.CreateItem(Outlook.OlItemType.olNoteItem), Outlook.NoteItemClass)
ni.Body = "This is my Body again"
ni.Display(New Object)

Dim pi As Outlook.PostItemClass = CType(app.CreateItem(Outlook.OlItemType.olPostItem), Outlook.PostItemClass)
pi.Subject = Subject
pi.BodyFormat = Outlook.OlBodyFormat.olFormatHTML
pi.Body = Body

pi.Display(New [Object])
End Sub 'button1_Click

End Class

Thursday, June 15, 2006

DropDown in DataGrid, How to Select

This is a two parter. First we will discuss on to add a "Select Value" to the Drop Down List after we populate the DropDown with Data from the Database then I will discuss how to Select that value using the OnItemDataBound from the DataGrid.

Inside the DataGrid you must use a Template like so

Thursday, June 08, 2006

SQL Time Converter

Private Sub BuildSQLTime(ByVal strProdDate, ByVal strProdTime)
Dim SQLDateTime As DateTime
Dim strDate As String = strProdDate
Dim strTime As String = strProdTime

Dim DateArray(2) As String
DateArray(0) = strDate.Substring(0, 2)
DateArray(1) = strDate.Substring(2, 2)
DateArray(2) = strDate.Substring(4, 2)

Dim tempDateTime As String
Dim newDateTime As DateTime

tempDateTime = DateArray(1) & "/" & DateArray(2) & "/" & DateArray(0)
newDateTime = Convert.ToDateTime(tempDateTime & " " & strTime)
'newDateTime = DateTime.Now

lblResults.Text = newDateTime
End Sub