Public Class frmF7

    Dim WordApp As Word.Application = CreateObject("Word.Application")

    Private Sub btnCheck_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub btnTjek_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTjek.Click

        Dim rngRange As Word.Range
        Me.Text = "Starter Word..."

        WordApp.Documents.Add()
        Me.Text = "Tjekker teksten..."
        rngRange = WordApp.ActiveDocument.Range
        rngRange.InsertAfter(txtTekst.Text)
        Dim SpellCollection As Word.ProofreadingErrors
        SpellCollection = rngRange.SpellingErrors
        If SpellCollection.Count > 0 Then
            lstFejl.Items.Clear()
            lstForslag.Items.Clear()
            Dim iword As Integer
            Dim newWord As String
            For iword = 1 To SpellCollection.Count
                If SpellCollection.Item(iword).Text <> TextBox2.Text Then
                    newWord = SpellCollection.Item(iword).Text
                    If lstFejl.FindStringExact(newWord) < 0 Then
                        lstFejl.Items.Add(newWord)
                    End If
                End If
            Next
        End If
        Me.Text = "Word stavekontrol"
    End Sub

    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstFejl.SelectedIndexChanged

        Dim ListOfAlternateWords As Word.SpellingSuggestions
        ListOfAlternateWords = _
             WordApp.GetSpellingSuggestions(lstFejl.Text)
        lstForslag.Items.Clear()
        If ListOfAlternateWords.Count > 0 Then
            Dim iWord As Integer
            For iWord = 1 To ListOfAlternateWords.Count
                lstForslag.Items.Add(ListOfAlternateWords.Item(iWord).Name)
            Next
        Else
            lstForslag.Items.Add("Ingen forslag.")
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnErstat.Click
        If lstFejl.SelectedIndex >= 0 And lstForslag.SelectedIndex >= 0 Then
            txtTekst.Text = Replace(txtTekst.Text, lstFejl.SelectedItem, lstForslag.SelectedItem)
            lstFejl.Items.Remove(lstFejl.SelectedIndex)
            lstForslag.Items.Clear()
        End If
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNulstil.Click
        lstFejl.Items.Clear()
        lstForslag.Items.Clear()
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLuk.Click
        WordApp.Quit(False)
        Me.Close()
    End Sub

    Private Sub frmFSyv_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        WordApp.Quit(False)
    End Sub
End Class