View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Jim Cone Jim Cone is offline
external usenet poster
 
Posts: 3,290
Default Very Basic Excel Object Questions

BJB,

I cannot quickly find what error 437 is.
It is not in the list of trappable errors.
However, you could experiment with...

If xSheet.Cells(i, j).Value = ""
or
If Len(xSheet.Cells(i, j).Value) =0

Of course the above assumes that i and j are valid variables.

'-------------------------------------------------------------
Here are some general guidelines to use when automating Excel...

1. Set a reference to the primary Excel objects used in your program.
Dim xlApp As Excel.Application
Dim WB As Excel.Workbook
Dim WS As Excel.Worksheet

Set xlApp = New Excel.Application
Set WB = xlApp.Workbooks.Add
Set WS = WB.Sheets(1)

Use the appropriate reference Every Time you make reference to a spreadsheet.
Do not use Range(xx) - use WS.Range(xx)
Cells should be WS.Cells(10, 20) or _
WS.Range(WS.Cells(10, 20), WS.Cells(20, 40))

2. Avoid the use of ActiveSheet, ActiveWorkbook, Selection etc.
Use your object references.

3. Avoid the use of the "With" construct.

4. Set all objects to Nothing in the proper order - child then parent.
Set WS = Nothing
WB.Close SaveChanges:=True 'your choice
Set WB = Nothing
xlApp.Quit
Set xlApp = Nothing

Violating any of these guidelines can leave "orphans" that still refer
to Excel and prevent the application from closing.

'------------------------------------------------------------

Jim Cone
San Francisco, USA



"BiilyJoeBob" wrote in message
...
An old Cobol guy playing around with VB 3.0 and Excel (Office 2K) needs help
on his first OO program, which is simply reading-only the contents of an
..xls file. I have two simple issues:

1) Wrap-up - I see code examples for closing workbooks, quitting
objects and setting objects to nothing. What is the proper way to wrap up
everything when the I'm done with my objects/files? In using the following
code, I cannot get the Close workbook to execute without either a syntax
error or Method not appl for this object. An instance of Excel is left
running when my program is done (I see it doing ctrl-alt-del and it's got a
hold of the cfe.xls file, too).

Dim xExcelApp As object
Dim xWorkbook As object
Dim xSheet As object
Set xExcelApp = GetObject("", "Excel.Application")
Set xWorkbook = xExcelApp.Workbooks.Open("c:\cfe.xls")
Set xSheet = xWorkbook.Worksheets(form1.Text1) (Text1 has worksheet name)
DO MY THING. . .
WrapUp:
' Release resources
'xExcelApp.Workbooks(1).Close(), tried it w/o the (1) and/or w/o the
(), tried as xWorkbook.Close(), w/o the () and even with the () filled in
with SaveChanges:=False -- hurumph!! -- nothing works.
Set xExcelApp = Nothing
Set xWorkbook = Nothing
Set xSheet = Nothing -- what does Quit do for me? Will
doing something at the xExcelApp level take care of everything underneath?

2) During the DO MY THING above, the 'If xSheet.Cells(i, j) = "" Then'
statement errors with err=437 whenever the cell is empty. Since I have on
error resume next, when the error occurs it falls to the next statement -
which is what I want it to do in this example anyway, but coding based on
receiving this error for empty cells seems ridiculous to me, especially if
what I really want to code is 'If xSheet.Cells(i, j) = "something besides
nulls". Any ideas?

Otherwise, I'm having fun again and I've used the worksheet values to
create an Outlook email item and send it! Any help for these elementary
issues with Excel and in general, wrapping up, is greatly appreciated.