sidewayz
03-24-2004, 06:26 AM
can somebody please tell me how to modify my code so that it will open up an existing document in excel and modify that instead of creating a new one? my code looks like this right now
Module Module1
Sub Main()
Dim oExcel As Excel.Application
Dim oWorkbooks As Excel.Workbooks
Dim oSheets As Excel.Sheets ' <-- Quirk in XL requires Excel.Sheets
Dim oWB As Excel.Workbook
Dim oWS As Excel.Worksheet
Dim oRng1, oRng2 As Excel.Range
oExcel = New Excel.Application() ' <-- No 'Set' Keyword in .Net.
oExcel.Visible = True ' <-- Using .Visible = True is optional.
oWorkbooks = oExcel.Workbooks ' <-- oExcel.Workbooks.Add cannot be accessed in
oWB = oWorkbooks.Add ' in one line, or it will cause Excel to Hang.
oSheets = oWB.Worksheets ' < -- Again, oWB.Worksheets.Item("Sheets1") cannot
oWS = oSheets.Item(1) ' be accessed in one line.
oRng1 = oWS.Range("A1") ' <-- oWS.Range("A1").Value is to be avoided,
oRng1.Value = "Hello World!" ' for it would cause Excel to Hang.
oRng2 = oWS.Range("B2:E5") ' <-- oRng1.Copy(oWS.Range("B2:E5")) would again
oRng1.Copy(oRng2) ' hang the running instance of Excel.
Cleanup:
' Cleanup is more extensive for COM Automation than is typical for
' other areas of VB.Net. To be 100% certain that your Application
' and other related references are released properly, you should
' use a structure similar to the following:
NAR(oRng1) ' <-- See the Sub NAR(), below.
NAR(oRng2)
NAR(oWS)
NAR(oSheets)
NAR(oWorkbooks)
oWB.Close(SaveChanges:=True)
NAR(oWB)
oWB.Close(False)
NAR(oWB)
oExcel.Quit()
NAR(oExcel)
End Sub
Private Sub NAR(ByVal o As Object)
' This sub cleanly releases the COM Automation Object.
' Source: http://support.microsoft.com/?kbid=317109
Try
System.Runtime.InteropServices.Marshal.ReleaseComObject(o)
Catch
Finally
o = Nothing
End Try
End Sub
End Module
any help would be appreciated thank you.
Module Module1
Sub Main()
Dim oExcel As Excel.Application
Dim oWorkbooks As Excel.Workbooks
Dim oSheets As Excel.Sheets ' <-- Quirk in XL requires Excel.Sheets
Dim oWB As Excel.Workbook
Dim oWS As Excel.Worksheet
Dim oRng1, oRng2 As Excel.Range
oExcel = New Excel.Application() ' <-- No 'Set' Keyword in .Net.
oExcel.Visible = True ' <-- Using .Visible = True is optional.
oWorkbooks = oExcel.Workbooks ' <-- oExcel.Workbooks.Add cannot be accessed in
oWB = oWorkbooks.Add ' in one line, or it will cause Excel to Hang.
oSheets = oWB.Worksheets ' < -- Again, oWB.Worksheets.Item("Sheets1") cannot
oWS = oSheets.Item(1) ' be accessed in one line.
oRng1 = oWS.Range("A1") ' <-- oWS.Range("A1").Value is to be avoided,
oRng1.Value = "Hello World!" ' for it would cause Excel to Hang.
oRng2 = oWS.Range("B2:E5") ' <-- oRng1.Copy(oWS.Range("B2:E5")) would again
oRng1.Copy(oRng2) ' hang the running instance of Excel.
Cleanup:
' Cleanup is more extensive for COM Automation than is typical for
' other areas of VB.Net. To be 100% certain that your Application
' and other related references are released properly, you should
' use a structure similar to the following:
NAR(oRng1) ' <-- See the Sub NAR(), below.
NAR(oRng2)
NAR(oWS)
NAR(oSheets)
NAR(oWorkbooks)
oWB.Close(SaveChanges:=True)
NAR(oWB)
oWB.Close(False)
NAR(oWB)
oExcel.Quit()
NAR(oExcel)
End Sub
Private Sub NAR(ByVal o As Object)
' This sub cleanly releases the COM Automation Object.
' Source: http://support.microsoft.com/?kbid=317109
Try
System.Runtime.InteropServices.Marshal.ReleaseComObject(o)
Catch
Finally
o = Nothing
End Try
End Sub
End Module
any help would be appreciated thank you.