Imports System.IO
Imports System.Net
Imports System.Text.RegularExpressions
Imports Microsoft.Office.Interop
Public Class Form1
Inherits System.Windows.Forms.Form
Private oApp As Excel.Application
#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 txtRes As System.Windows.Forms.TextBox
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents txtFor As System.Windows.Forms.TextBox
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.txtRes = New System.Windows.Forms.TextBox
Me.txtFor = New System.Windows.Forms.TextBox
Me.Button1 = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'txtRes
'
Me.txtRes.Location = New System.Drawing.Point(136, 136)
Me.txtRes.Name = "txtRes"
Me.txtRes.TabIndex = 0
Me.txtRes.Text = ""
'
'txtFor
'
Me.txtFor.Location = New System.Drawing.Point(72, 56)
Me.txtFor.Name = "txtFor"
Me.txtFor.TabIndex = 1
Me.txtFor.Text = ""
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(176, 216)
Me.Button1.Name = "Button1"
Me.Button1.TabIndex = 2
Me.Button1.Text = "Button1"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 266)
Me.Controls.Add(Me.Button1)
Me.Controls.Add(Me.txtFor)
Me.Controls.Add(Me.txtRes)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
#End Region
Sub AppendResults(ByVal msg As String)
txtRes.AppendText(msg & ControlChars.CrLf)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim r As Regex
Dim m As Match
r = New Regex("\d+")
Dim mc As MatchCollection = r.Matches(txtFor.Text)
For Each m In mc
AppendResults(m.Value)
Next
Dim oWB As Excel.Workbook
Dim rLastRow As Excel.Range
Dim rLastCol As Excel.Range
Const xlCellTypeLastCell = 11
Dim i As Integer
Dim iRow As Integer
Dim iCol As Integer
Dim bOpen As Boolean
Try
oApp = GetObject(, "Excel.Application")
If oApp Is Nothing Then oApp = New Excel.Application
For Each oWB In oApp.Workbooks
If oWB.Name = "MyExcelDemo.xls" Then
bOpen = True
Exit For
Else
bOpen = False
End If
Next
If bOpen = True Then
'Already open
oWB = oApp.Workbooks("MyExcelDemo.xls")
Else
'Open your wb
oWB = oApp.Workbooks.Open("C:\MyExcelDemo.xls")
End If
oApp.Visible = True
rLastRow = oWB.Sheets(1).UsedRange.SpecialCells(xlCellTypeLastCell)
iRow = rLastRow.Row + 1
rLastCol = oWB.Sheets(1).usedrange.specialcells(xlCellTypeLastCell)
iCol = rLastCol.Column
For i = 1 To mc.Count
oWB.Sheets(1).Cells(iRow, i).Value = mc(i - 1).Value
Next
oWB.Save()
Catch exp As Exception
MessageBox.Show(exp.Message, exp.Source, MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
End Class