Class Module Question

A

Andrew Yates

Hi there,

I've received some software that I am trying to automate and by the
looks of it, I need to use Class Modules.

So after reading up about what Class modules are, I finally have come
to a point where I have a very slight understanding what is happening.

Anyway, I've got to a point where the code looks like it is almost
working (I think). Could somebody please have a look at the below code
and let me know what the logic flaw is.

===============
Module code
===============
Public Class1 As New classmodule1

sub TransferFile()


With Class1
.send
End With
End Sub

===============================================
Class Module code - the class is called classmodule1
===============================================
Dim WithEvents mSession As BS.Session
Dim mCancelCommand As Boolean
Dim mFileName As String

Public Sub send()
mCancelCommand = False
mFileName = "file1234"

mSession.SENDFROMFILE mFileName ' this is where it is crashing

End Sub

I am receiving the following error:
"Object variable or With block variable not set" and it is crashing at
the " mSession.SENDFROMFILE mFileName" line. The value of mSession =
nothing which I assume is the problem but as would be quite apparent to
anybody reading this far, I don't have a clue.



Any assistance is much appreciated.

Regards,
Andrew
 
J

John Gunther

Try replacing your line:

Dim WithEvents mSession As BS.Session

With the line:

Dim WithEvents mSession As New BS.Session

John
 
R

Rob Bovey

Hi Andrew,

If the code you've shown is complete, the error is being caused because
you never assign an instance of the BS.Session class to your mSession
variable. Try adding the following event procedure to your class module.

Private Sub Class_Initialize()
Set mSession = New BS.Session
''' If the above gives an error, try this instead:
'Set mSession = CreateObject("BS.Session")
End Sub

This will run automatically as soon as an instance of the class is created.
If there are any other things you need to do to your mSession object in
order to get it into shape for using in your code, add them to this event.

--
Rob Bovey, Excel MVP
Application Professionals
http://www.appspro.com/

* Take your Excel development skills to the next level.
* Professional Excel Development
http://www.appspro.com/Books/Books.htm
 
A

Andrew Yates

Hi John,

When I try this, I receive an error "invalid use of new keyword".
Thanks for your suggestion though.

Regards,
Andrew
 
A

Andrew Yates

Hi Rob,

"Set mSession = New BS.Session " caused an invalid use of new keyword
error.
"Set mSession = CreateObject("BS.Session") " causes an "automation
error, unspecified error".

Your suggestions are very much appreciated.

Regards,
Andrew
 
R

Rob Bovey

Andrew Yates said:
"Set mSession = New BS.Session " caused an invalid use of new keyword
error.
"Set mSession = CreateObject("BS.Session") " causes an "automation
error, unspecified error".

Hi Andrew,

Are you sure this BS.Session object is designed to be used from VBA? Are
you able to reference the BS object library (or whatever its friendly name
is) using the Tools/References menu?

I don't know anything about what the BS.Session object is, but if it is
designed to work with VBA then either it doesn't expose events that you can
trap using the WithEvents keyword in your variable declaration or it's not a
creatable class, which means you need to go through some other object in the
BS object library in order to get a reference to it.

--
Rob Bovey, Excel MVP
Application Professionals
http://www.appspro.com/

* Take your Excel development skills to the next level.
* Professional Excel Development
http://www.appspro.com/Books/Books.htm
 
A

Andrew Yates

Rob,

I am able to reference the BS object library and am pretty sure that it
is designed to used from VBA but could be wrong. In the manual, they
make the comment "The BSAPI is exposed as a set of Component Object
Model (COM) objects that are accessible from other COM compatible
applications".
By the looks of it, bs.ession object is not a createable class and I
will need to go to the BS object library in order to reference it some
other way. Is there a way that I can tell via the object library
whether something is a creatable class.

Regards,
Andrew
 
A

Andrew Yates

Andrew said:
Rob,

I am able to reference the BS object library and am pretty sure that it
is designed to used from VBA but could be wrong. In the manual, they
make the comment "The BSAPI is exposed as a set of Component Object
Model (COM) objects that are accessible from other COM compatible
applications".
By the looks of it, bs.ession object is not a createable class and I
will need to go to the BS object library in order to reference it some
other way. Is there a way that I can tell via the object library
whether something is a creatable class.

Regards,
Andrew

Rob (or anybody else who may be able to point an amateur in the right
direction),

Through trial an error and interpreting your comments, I was able to
get through the previous error point:

Dim mSession As BS.Session ' changed this in line with your prior
comments

Public Sub send()
Set mSession = CreateObject("BS.Session")
mFileName = "12345"
mCancelCommand = False
mSession.SendFromFile mFileName ' it is now crashing here
End Sub

So the script is now crashing at the "mSession.SendFromFile mFileName"
line with the error being java.lang.NullPointerException. I did a bit
of research on this error but it seems to be java related. I didn't
even know I was doing anything relating to Java. Is there a simple
answer to the above or is it too specific to the application that I'm
trying to work with.

Regards,
Andrew
 
R

Rob Bovey

Andrew Yates said:
Public Sub send()
Set mSession = CreateObject("BS.Session")
mFileName = "12345"
mCancelCommand = False
mSession.SendFromFile mFileName ' it is now crashing here
End Sub

So the script is now crashing at the "mSession.SendFromFile mFileName"
line with the error being java.lang.NullPointerException. I did a bit
of research on this error but it seems to be java related. I didn't
even know I was doing anything relating to Java. Is there a simple
answer to the above or is it too specific to the application that I'm
trying to work with.

Hi Andrew,

The error message indicates the component you're working with was
written in Java. Without knowing anything about this component, my first
guess is that mFileName must contain a valid file name, probably including
the full path to where that file is located.

--
Rob Bovey, Excel MVP
Application Professionals
http://www.appspro.com/

* Take your Excel development skills to the next level.
* Professional Excel Development
http://www.appspro.com/Books/Books.htm
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top