Public Class apcWordDB

    Private Sub SuppliersBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Me.Validate()
        Me.SuppliersBindingSource.EndEdit()
        Me.TableAdapterManager.UpdateAll(Me.NORTHWNDDataSet)

    End Sub

    Private Sub ActionsControl_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Me.SuppliersTableAdapter.Fill(Me.NORTHWNDDataSet.Suppliers)
        Me.ProductsTableAdapter.Fill(Me.NORTHWNDDataSet.Products)
    End Sub

    Shared Sub SetHeadings(ByVal tblCell As Word.Cell, ByVal text As String)
        With tblCell.Range
            .Text = text
            .Font.Bold = True
            .ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter
        End With
    End Sub

    Private Sub AddData(ByVal row As DataRow, ByVal companyName As String)
        If Globals.ThisDocument.Tables.Count = 0 Then
            Try
                Dim tbl As Word.Table = Globals.ThisDocument.Tables.Add(Globals.ThisDocument.Application.Selection.Range, 1, 4)

                SetHeadings(tbl.Cell(1, 1), "Firmanavn")
                SetHeadings(tbl.Cell(1, 2), "Produktnavn")
                SetHeadings(tbl.Cell(1, 3), "Kvantitet")
                SetHeadings(tbl.Cell(1, 4), "Enhedspris")

            Catch ex As Exception
                MessageBox.Show("Problem creating Products table: " & ex.Message, "Action Pane", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
        End If

        Dim selection As Word.Selection = Globals.ThisDocument.Application.Selection

        If selection.Tables.Count > 0 Then
            Dim newRow As Word.Row = Globals.ThisDocument.Tables(1).Rows.Add()
            With newRow
                .Range.Font.Bold = False
                .Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter
                .Cells(4).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight
                .Cells(1).Range.Text = companyName
                .Cells(2).Range.Text = row.Item("ProductName").ToString
                .Cells(3).Range.Text = row.Item("").ToString
                .Cells(4).Range.Text = Math.Round(row.Item(""), 2)
            End With

        Else
            MessageBox.Show("Cursor must be within a table.", "Actions Pane", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If
    End Sub

    Private Sub btnInsert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInsert.Click
        Dim tbl As DataTable = NORTHWNDDataSet.Products
        Dim rows() As DataRow

        If Not Me.ProductNameListBox.SelectedIndex < 0 Then
            Dim product As String = Me.ProductNameListBox.SelectedItem.Row.Item("ProductName")
            Dim company As String = Me.CompanyNameComboBox.Text

            rows = tbl.Select("ProductName = '" & product.Replace("'", "''") & "'")
            AddData(rows(0), company)
        Else
            MessageBox.Show("Pleae select a product.", "Action Pane", MessageBoxButtons.OK)
        End If
    End Sub
End Class
