Dim wrdApp As Word.Application
Dim wrdDoc As Word._Document
Private Sub InsertLines(ByVal LineNum As Integer)
Dim iCount As Integer
For iCount = 1 To LineNum
wrdApp.Selection.TypeParagraph()
Next iCount
End Sub
Private Sub FillRow(ByVal Doc As Word.Document, ByVal Row As Integer, ByVal Text1 As String, ByVal Text2 As String, ByVal Text3 As String, ByVal Text4 As String)
With Doc.Tables.Item(1)
.Cell(Row, 1).Range.InsertAfter(Text1)
.Cell(Row, 2).Range.InsertAfter(Text2)
.Cell(Row, 3).Range.InsertAfter(Text3)
.Cell(Row, 4).Range.InsertAfter(Text4)
End With
End Sub
Private Sub CreateMailMergeDataFile()
Dim wrdDataDoc As Word._Document
Dim iCount As Integer
wrdDoc.MailMerge.CreateDataSource(Name:="C:\DataDoc.doc", HeaderRecord:="FirstName, LastName, Address, CityStateZip")
wrdDataDoc = wrdApp.Documents.Open("C:\DataDoc.doc")
For iCount = 1 To 2
wrdDataDoc.Tables.Item(1).Rows.Add()
Next iCount
FillRow(wrdDataDoc, 2, "Jamuna Silks", "1-55, Ist cross", "Thupaakula", "Bagalore")
FillRow(wrdDataDoc, 3, "Mohan Enterpirses", "1234 5th Street", "Seven Street Junction", "Hyderabad")
FillRow(wrdDataDoc, 4, "Imax", "Banjara Hills", "Beside assembly", "Hyderabad")
wrdDataDoc.Save()
wrdDataDoc.Close(False)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim wrdSelection As Word.Selection
Dim wrdMailMerge As Word.MailMerge
Dim wrdMergeFields As Word.MailMergeFields
Dim StrToAdd As String
wrdApp = CreateObject("Word.Application")
wrdApp.Visible = True
wrdDoc = wrdApp.Documents.Add()
wrdDoc.Select()
wrdSelection = wrdApp.Selection()
wrdMailMerge = wrdDoc.MailMerge()
CreateMailMergeDataFile()
StrToAdd = "CompIndia Infotech P LTD" & vbCr & "An Offshore development for IndyZen Inc USA"
wrdSelection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter
wrdSelection.TypeText(StrToAdd)
InsertLines(4)
wrdSelection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft
wrdMergeFields = wrdMailMerge.Fields()
wrdMergeFields.Add(wrdSelection.Range, "FirstName")
wrdSelection.TypeParagraph()
wrdMergeFields.Add(wrdSelection.Range, "LastName")
wrdSelection.TypeParagraph()
wrdMergeFields.Add(wrdSelection.Range, "Address")
wrdSelection.TypeParagraph()
wrdMergeFields.Add(wrdSelection.Range, "CityStateZip")
InsertLines(2)
wrdSelection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight
wrdSelection.InsertDateTime(DateTimeFormat:="dddd, MMMM dd, yyyy", InsertAsField:=False)
InsertLines(2)
wrdSelection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphJustify
wrdSelection.TypeText("Dear Sir / Medam")
wrdSelection.TypeText(",")
InsertLines(2)
StrToAdd = "The Company was incorporated on 24 February 1998 as Seven Hills InfoTech Pvt Ltd., and was later changed to CompIndia Infotech Pvt Ltd., on 21 March 2000. Since then CompIndia has been providing varied IT services to Clients all over the globe. CompIndia is based on an aim to help companies build better processes and systems, to enhance their performance. Our offerings focus on unleashing the potential of integration to meet your current challenges and future opportunities. Built on a foundation of strong core values and a commitment to customer delight, CompIndia offers a wide range of expertise in the following areas of Information Technology: Software Development Services, Product Development, Electronic Commerce and Consulting. CompIndia specializes in customized IT solutions for industries in the areas of Hotel Management, Financial services, Transportation, Healthcare and a lot more. CompIndia is greatly involved in the creation of proprietary software. With a team of highly skilled and experienced professionals, who come from a wide range of business, management and IT disciplines, we have the know-how to successfully address any IT challenge and help business succeed in an ever changing marketplace."
wrdSelection.TypeText(StrToAdd)
InsertLines(2)
wrdDoc.Tables.Add(wrdSelection.Range, NumRows:=9, NumColumns:=4)
With wrdDoc.Tables.Item(1)
.Columns.Item(1).SetWidth(51, Word.WdRulerStyle.wdAdjustNone)
.Columns.Item(2).SetWidth(170, Word.WdRulerStyle.wdAdjustNone)
.Columns.Item(3).SetWidth(100, Word.WdRulerStyle.wdAdjustNone)
.Columns.Item(4).SetWidth(111, Word.WdRulerStyle.wdAdjustNone)
.Rows.Item(1).Cells.Shading.BackgroundPatternColorIndex = Word.WdColorIndex.wdGray25
.Rows.Item(1).Range.Bold = True
.Cell(1, 1).Range.Paragraphs.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter
FillRow(wrdDoc, 1, "Emp ID", "Employee Name", "Designation", "Remarks")
FillRow(wrdDoc, 2, "1024", "Muni Hemadri Babu. Jogi", ".Net Developer", "")
FillRow(wrdDoc, 3, "1025", "Hemadri", "Web Developer", "")
FillRow(wrdDoc, 4, "1026", "Hemu", "PHP Developer", "")
FillRow(wrdDoc, 5, "1027", "Hem", "J2EE Developer", "")
FillRow(wrdDoc, 6, "1028", "Geetha", ".Net Developer", "")
FillRow(wrdDoc, 7, "1029", "Rathi", ".Net Developer", "")
FillRow(wrdDoc, 8, "1030", "Srilatha", ".Net Developer", "")
FillRow(wrdDoc, 9, "1031", "Srilatha", ".Net Developer", "")
End With
wrdApp.Selection.GoTo(Word.WdGoToItem.wdGoToLine, Word.WdGoToDirection.wdGoToLast)
InsertLines(2)
StrToAdd = "For additional information regarding the " & "CompIndia Infotech P LTD, " & "you can visit our Web site at "
wrdSelection.TypeText(StrToAdd)
wrdSelection.Hyperlinks.Add(Anchor:=wrdSelection.Range, Address:="http://www.compindia.comd") ' Create a string and insert it in the
StrToAdd = ". Sincerely," & vbCr & vbCr & "Muni Hemadri Babu .J" & vbCr & ".Net Developer" & vbCr
wrdSelection.TypeText(StrToAdd)
wrdMailMerge.Destination = Word.WdMailMergeDestination.wdSendToNewDocument
wrdMailMerge.Execute(False)
wrdDoc.Saved = True
wrdDoc.Close(False)
wrdSelection = Nothing
wrdMailMerge = Nothing
wrdMergeFields = Nothing
wrdDoc = Nothing
wrdApp = Nothing
System.IO.File.Delete("C:\DataDoc.doc")
End Sub