VB.Net write to excel that is already open?

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I have the vb.net app that opens multiple copies of excel that I read and then write but need to sort out how to set the workbook activate that is already open. example code below I open three excel files "Export.xls" and Import1 and
2 that have a date. I read them ButtonRead_Click but when I ButtonWrite_Click I need a way to tell what workbook Im wanting to write to. and cant open it again. thoughts?


<pre class="prettyprint lang-vb Private Sub ButtonRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonRead.Click

Try
Dim MyExcel As New Excel.Application
MyExcel.Workbooks.Open("C:TempExport.xls")
MyExcel.Visible = False
MyExcel.Sheets("Sheet1").activate()

Dim sPath As String, sFile As String, sName As String
sPath = "C:Temp"
sFile = "Import1*.xls"
sName = Dir(sPath & sFile)
If sName <> "" Then
MyExcel.Workbooks.Open(sPath & sName).Activate()
MyExcel.Visible = True
MyExcel.Sheets("Sheet1").activate()
Else
MsgBox("Could Not find, Import1-mmddyy.xls", vbExclamation, "Oops!")
End If
Dim s2Path As String, s2File As String, s2Name As String
s2Path = "C:TEMP"
s2File = "Import2*.xls"
s2Name = Dir(s2Path & s2File)
If s2Name <> "" Then
MyExcel.Workbooks.Open(s2Path & s2Name).Activate()
MyExcel.Visible = True
MyExcel.Sheets("Sheet1").activate()
Else
MsgBox("Could Not find, Import2-mmddyy.xls", vbExclamation, "Oops!")
End If
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Private Sub ButtonWrite_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonWrite.Click
Dim sPath As String, sFile As String, sName As String
sPath = "C:TEMP"
sFile = "Import*.xls"
sName = Dir(sPath & sFile)
MyExcel.Workbooks(sPath & sName).Activate()
MyExcel.Sheets("Sheet1").activate()

End Sub<img alt="" height="188" src="http://social.msdn.microsoft.com/Forums/getfile/121004" width="457 [/code]
<br/>

View the full article
 
Back
Top