Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using VB - GetObject and Excel

Status
Not open for further replies.

kutibo

MIS
Dec 19, 2000
22
US
I want to use GetObject to edit an Excel file, and I don't want it to be visible. But if I don't include:
objExcel.Application.Visible = True
objExcel.Parent.Windows(1).Visible = True
when I open up test.xls, Excel opens but does not load the workbook. Please Help, Thanks.

Sub EditExcel()
Dim appExcel As Excel.Application, objExcel As Object
Set appExcel = CreateObject("Excel.Application")
Set objExcel = GetObject("c:\Excel Files\test.xls")

objExcel.Application.Visible = True
objExcel.Parent.Windows(1).Visible = True
For Each ws In objExcel.Worksheets
' Edit excel file here.
Next ws
objExcel.Save

appExcel.Quit
Set appExcel = Nothing
Set objExcel = Nothing
End Sub
 
[bugeyed] The GetObject() syntax is rather outdated and does cause the problems you are describing. Try using the cleaner, newer syntax:

Public Function OpenExcel()
On Error GoTo ErrHandler

Dim xl As Excel.Application
Dim wb As Excel.Workbook
Dim sht As Excel.Worksheet

Set xl = New Excel.Application
Set wb = xl.Workbooks.Open("C:\Test.xls")
Set sht = wb.Sheets("Sheet1")

With sht
.Cells(1, 1) = "Hello"
.Cells(1, 2) = "World"
End With

sht.PrintOut

wb.Close True

xl.Quit

ExitHere:
On Error Resume Next
Set sht = Nothing
Set wb = Nothing
Set xl = Nothing
Exit Function
ErrHandler:
MsgBox "Error: " & Err & " - " & Err.Description
Resume ExitHere
End Function VBSlammer
redinvader3walking.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top