Mailing word document in VB.net

Z

zxc

hi
i want a code for mailing a word document created by Vb.net through
VB.net ... below is my code of creating word document, retrieving
database values from MS SQL server and generat report using word
document .. now document has been created and saved but i dont know
how can i mail this document through VB.net .. can anyone plz help me
thanks in advance


Imports Word.ApplicationClass
Imports Excel.ApplicationClass
Imports Microsoft.Office.Core
'Imports Microsoft.Office.Interop
Imports System.IO.Path
Imports Microsoft.VisualBasic
Imports System.Data.SqlClient

Public Class Form1
Inherits System.Windows.Forms.Form

#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 Button1 As System.Windows.Forms.Button
Friend WithEvents Button2 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.Button1 = New System.Windows.Forms.Button
Me.Button2 = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(64, 32)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(152, 56)
Me.Button1.TabIndex = 0
Me.Button1.Text = "Automate Word"
'
'Button2
'
Me.Button2.Location = New System.Drawing.Point(70, 108)
Me.Button2.Name = "Button2"
Me.Button2.Size = New System.Drawing.Size(152, 56)
Me.Button2.TabIndex = 1
Me.Button2.Text = "Exit"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 273)
Me.Controls.Add(Me.Button2)
Me.Controls.Add(Me.Button1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)

End Sub

#End Region
Private WithEvents oWord As Word.ApplicationClass
Private WithEvents ThisApplication As Word.Application
Private WithEvents ThisDocument As Word.Application
Private WithEvents WordApp As Word.ApplicationClass
Private WithEvents openFileDialog1 As OpenFileDialog
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
CreateWordTable()
End Sub

Private Sub CreateWordTable()
oWord = CreateObject("Word.Application")
oWord.Visible = True
oWord.Activate()
oWord.Documents.Add()
Dim rng As Word.Range = oWord.ActiveDocument.Range(Start:=0,
End:=0)
rng.InsertBefore("Author's Contact")
rng.Font.Name = "Verdana"
rng.Font.Size = 16
rng.InsertParagraphAfter()
rng.InsertParagraphAfter()
rng.SetRange(rng.End, rng.End)
rng.Tables.Add( _
Range:=oWord.ActiveDocument.Paragraphs(2).Range, _
NumRows:=1, NumColumns:=3)
Dim tbl As Word.Table = oWord.ActiveDocument.Tables(1)
tbl.Range.Font.Size = 12
tbl.Range.Font.Name = "Verdana"
tbl.Borders.InsideLineStyle = _
Word.WdLineStyle.wdLineStyleSingle
tbl.Borders.OutsideLineStyle = _
Word.WdLineStyle.wdLineStyleDouble
tbl.Columns(1).SetWidth( _
oWord.Application.InchesToPoints(1.5), _
Word.WdRulerStyle.wdAdjustNone)
tbl.Columns(2).SetWidth( _
oWord.Application.InchesToPoints(2.25), _
Word.WdRulerStyle.wdAdjustNone)
tbl.Columns(3).SetWidth( _
oWord.Application.InchesToPoints(3.25), _
Word.WdRulerStyle.wdAdjustNone)
tbl.Cell(1, 1).Range.Text = "Author"
tbl.Cell(1, 2).Range.Text = "Contact#"
Dim rngCell As Word.Range = tbl.Cell(1, 3).Range
rngCell.Text = "Address"
rngCell.ParagraphFormat.Alignment = _
Word.WdParagraphAlignment.wdAlignParagraphRight
Dim strSQL As String = _
"SELECT au_lname, phone, address " & _
"FROM authors"

Dim cnn As SqlConnection
Dim sdr As SqlDataReader
Dim cmd As SqlCommand
cnn = New SqlConnection( _
"workstation id='DEV-INTERN';packet size=4096;integrated
security=SSPI;data source='DEV-INTERN';persist security
info=False;initial catalog=pubs")
cnn.Open()
cmd = New SqlCommand(strSQL, cnn)
sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
Dim intRow As Integer = 2
While sdr.Read()
tbl.Rows.Add()
tbl.Cell(intRow, 1).Range.Text = sdr(0).ToString
tbl.Cell(intRow, 2).Range.Text = sdr(1).ToString
tbl.Cell(intRow, 3).Range.Text = sdr(2).ToString
intRow += 1
End While
tbl.Rows(1).Range.Bold = 1

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
oWord.ActiveDocument.SaveAs("C:\Documents and
Settings\zafzal\Desktop\word.doc")
oWord.ActiveDocument.Close()
Close()
End Sub
End Class
 
P

Pieter Coucke

Public Sub NewMail(ByVal strTo As String, Optional ByVal strSubject As
String = "", Optional ByVal strMessage As String = "", Optional ByVal
colAttachments As Collection = Nothing)
Try
Dim oApp As Outlook.Application
oApp = New Outlook.Application
MailObject = oApp.CreateItem(Outlook.OlItemType.olMailItem)

With MailObject
'ATTACHMENTS!!!!
Dim intX As Integer
If colAttachments IsNot Nothing Then
For intX = 1 To colAttachments.Count
.Attachments.Add(colAttachments(intX),
Outlook.OlAttachmentType.olByValue)
Next
End If

.Subject = strSubject
.To = strTo

.Body =strMessage

'Send it directly!
'.Send()
'Open it!
.Display()

End With
oApp = Nothing
Catch ex As Exception
Throw ex
Finally
End Try
End Sub


A reference must be added to Outlook...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top