q1w2e3r4t7
Active member
- Joined
- Nov 15, 2005
- Messages
- 30
Trying to launch excel from within my application, and have any changes (registered from the changed event) feed directly back into my application.
Excel is opened and whatever text that is in the textbox is entered into cell A1.
On the cell change event, i want to pick the value from A1 and put it back into my application, however i get the message:
I have tried the use of delages etc as per other multi-threading technics, however still have the same error.
If anyone can help it would be greatly appreciated
Excel is opened and whatever text that is in the textbox is entered into cell A1.
On the cell change event, i want to pick the value from A1 and put it back into my application, however i get the message:
Cross-thread operation not valid: Control TextBox1 accessed from a thread other than the thread it was created on.
Code:
Public Class Form1
Dim WithEvents excel As Microsoft.Office.Interop.Excel.Application
Dim WithEvents wb As Microsoft.Office.Interop.Excel.Workbook
Dim WithEvents ws As Microsoft.Office.Interop.Excel.Worksheet
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
excel = New Microsoft.Office.Interop.Excel.Application
excel.Visible = True
wb = excel.Workbooks.Add
ws = wb.ActiveSheet
ws.Range("A1").Value = TextBox1.Text
End Sub
Private Sub excel_WorkbookActivate(ByVal Wb As Microsoft.Office.Interop.Excel.Workbook) Handles excel.WorkbookActivate
Me.wb = Wb
End Sub
Private Sub wb_SheetChange(ByVal Sh As Object, ByVal Target As Microsoft.Office.Interop.Excel.Range) Handles wb.SheetChange
Me.ws = Sh
End Sub
Private Sub ws_Change(ByVal Target As Microsoft.Office.Interop.Excel.Range) Handles ws.Change
Try
TextBox1.Text = ws.Range("A1").Value
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
End Class
I have tried the use of delages etc as per other multi-threading technics, however still have the same error.
If anyone can help it would be greatly appreciated