Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
Dim wrdSelection As Word.Selection
Dim wrdMailMerge As Word.MailMerge
Dim wrdMergeFields As Word.MailMergeFields
Dim strToAddSaleNo
Dim oTable As Word.Table
Dim StrToAdd As String
Me.Cursor = System.Windows.Forms.Cursors.WaitCursor
' Create an instance of Word and make it visible.
wrdApp = CreateObject("Word.Application")
wrdApp.Visible = True
' Add a new document.
wrdDoc = wrdApp.Documents.Add()
wrdDoc.Select()
wrdSelection = wrdApp.Selection()
wrdMailMerge = wrdDoc.MailMerge()
' Create MailMerge Data file.
CreateMailMergeDataFile()
With wrdDoc.PageSetup
.TopMargin = 90
.LeftMargin = 90
.RightMargin = 22
.BottomMargin = 20
End With
InsertLines(4)
' Insert merge data.
wrdSelection.ParagraphFormat.Alignment = _
Word.WdParagraphAlignment.wdAlignParagraphLeft
wrdMergeFields = wrdMailMerge.Fields()
wrdMergeFields.Add(wrdSelection.Range, "Title")
wrdSelection.TypeText(" ")
wrdMergeFields.Add(wrdSelection.Range, "FirstName")
wrdSelection.TypeText(" ")
wrdMergeFields.Add(wrdSelection.Range, "Surname")
wrdSelection.TypeParagraph()
wrdMergeFields.Add(wrdSelection.Range, "Street")
wrdSelection.TypeParagraph()
wrdMergeFields.Add(wrdSelection.Range, "Village")
wrdSelection.TypeParagraph()
wrdMergeFields.Add(wrdSelection.Range, "Town")
wrdSelection.TypeParagraph()
wrdMergeFields.Add(wrdSelection.Range, "County")
wrdSelection.TypeParagraph()
wrdMergeFields.Add(wrdSelection.Range, "Postcode")
'////Initial cod eto get to bottom of page///////
'InsertLines(32)
''wrdSelection.Font.Bold = Word.Font
'wrdSelection.ParagraphFormat.LineSpacing = 7.5
'InsertLines(3)
'wrdSelection.ParagraphFormat.LineSpacing = 10
''InsertLines(2)
'wrdSelection.Paragraphs.TabIndent(0.5)
'wrdSelection.TypeText("RCD")
''wrdMergeFields.Add(wrdSelection.Range, "SaleNo")
''wrdSelection.TypeText(" RCD")
'wrdMergeFields.Add(wrdSelection.Range, "SaleNo")
' Go to the end of the document.
'////////////////////////////////////////////////////
'///Failed attempt at using a table to arrange data layout ///////////
'oTable = wrdDoc.Tables.Add(wrdSelection.Range, NumRows:=2, _
' NumColumns:=2)
'With oTable
' ' ' Set the column widths.
' .Columns.Item(1).SetWidth(100, Word.WdRulerStyle.wdAdjustNone)
' .Columns.Item(2).SetWidth(100, Word.WdRulerStyle.wdAdjustNone)
' .Rows.Item(1).SetHeight(20, Word.WdRowHeightRule.wdRowHeightExactly)
' .Rows.Item(2).SetHeight(40, Word.WdRowHeightRule.wdRowHeightExactly)
' ' ' Set the shading on the first row to light gray.
' .Rows.Item(1).Cells.Shading.BackgroundPatternColorIndex = _
' Word.WdColorIndex.wdAuto
' ' Bold the first row.
' .Rows.Item(1).Range.Bold = False
' ' Center the text in Cell (1,1).
' .Cell(1, 1).Range.Paragraphs.Alignment = _
' Word.WdParagraphAlignment.wdAlignParagraphRight
' ' Fill each row of the table with data.
' .Cell(1, 2).Range.Paragraphs.Alignment = _
' Word.WdParagraphAlignment.wdAlignParagraphRight
' '.Cell(1, 1).Range.InsertAfter(wrdSelection.Range, "SaleNo")
' '.Cell(1, 2).Range.InsertAfter(wrdMergeFields.Add(wrdSelection.Range, "SaleNo"))
'End With
'////////////////////////////////////////////////////////////////////
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Perform mail merge.
wrdMailMerge.Destination = _
Word.WdMailMergeDestination.wdSendToNewDocument
wrdMailMerge.Execute(False)
' Close the original form document.
wrdDoc.Saved = True
wrdDoc.Close(False)
' Release References.
wrdSelection = Nothing
wrdMailMerge = Nothing
wrdMergeFields = Nothing
wrdDoc = Nothing
wrdApp = Nothing
' Clean up temp file.
System.IO.File.Delete("C:\DataDoc.doc")
Catch ex As Exception
MsgBox(ex.ToString & " Contact IT on x5005 Now ", MsgBoxStyle.OKOnly, "Error")
End Try
Me.Cursor = System.Windows.Forms.Cursors.Default
End Sub