From: ryguy7272 on
I get a tooltip telling me that: Name 'EXL' is not declared and EXL has a
squiggle line under it. I went to Project > Add Reference > COM > Microsoft
10.0 Object Library and still get that tooltip popping up.

Code below:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim WSheet As New Excel.Worksheet()
WSheet = EXL.Workbooks.Open("C:\TEST.XLS").Worksheets.Item(1)
EXL.Range("A2:E3").Select()
Dim CData As Excel.Range
CData = EXL.Selection
CData.Sort(Key1:=CData.Range("A2"),
Order1:=Excel.XlSortOrder.xlAscending)
TextBox1.Clear()
Dim iCol, iRow As Integer
For iCol = 1 To 5
For iRow = 0 To 1
TextBox1.AppendText(CData(iRow, iCol).value & vbTab)
Next
TextBox1.AppendText(vbCrLf)
Next
EXL.Workbooks.Close()
End Sub

I'm reading a book with a few of these Office Automation examples; I just
did this with a reference to a MS Word Object so I thought I knew what to do
for this, but apparently not! What am I doing wrong?

Thanks!
Ryan--

--
Ryan---
If this information was helpful, please indicate this by clicking ''Yes''.
From: Patrice on
The code you show us doesn't declare EXL so for I would say that the error
message could say exactly what is the problem (that is EXL is just not
declared).

--
Patrice


From: ryguy7272 on
Thanks Patrice. I added this:
Dim EXL As Excel.Workbook

Then, when I run the project, I get this message: 'object variable or with
block variable not set'

This line i syellow:
WSheet = EXL.Workbooks.Open("C:\TEST.XLS").Worksheets.Item(1)

Any ideas?

Thanks!
Ryan---

--
Ryan---
If this information was helpful, please indicate this by clicking ''Yes''.


"Patrice" wrote:

> The code you show us doesn't declare EXL so for I would say that the error
> message could say exactly what is the problem (that is EXL is just not
> declared).
>
> --
> Patrice
>
>
> .
>
From: Patrice on
You need also to instantiate the object using new :
Dim EXL As New Excel.Workbook

Else the variable is defined but never initialized.

I would recommend trying a programming tutorial such as
http://ondotnet.com/pub/a/dotnet/2001/09/25/oop_vb.html...

--
Patrice

From: Captain Jack on
"ryguy7272" <ryguy7272(a)discussions.microsoft.com> wrote in message
news:D2122F9D-BC07-4397-BB2F-E5F79117BB89(a)microsoft.com...
> Thanks Patrice. I added this:
> Dim EXL As Excel.Workbook
>
> Then, when I run the project, I get this message: 'object variable or with
> block variable not set'
>
> This line i syellow:
> WSheet = EXL.Workbooks.Open("C:\TEST.XLS").Worksheets.Item(1)
>
> Any ideas?
>
> Thanks!
> Ryan---
>

Workbooks is a property of the Excel Application object, so your EXL
variable would need to be of that type. Also, you're declaring it, but
you're not assigning it a value, so that's why you're getting the error.

To declare EXL so it will work, you'll need something like this:

Dim EXL As New Excel.Application

This will start an Excel process on your system. If you don't want to see a
window for it, you may want to set the Visible property of EXL to False.
When you're done processing your file, you'll probably want to call the Quit
method of EXL to stop the running application.

--
Jack