0
votes

Suppose I have a custom library (.dll) called; Library.dll

Within the library, there is a class called; class

I generate an object called testObject:

Dim testObject As New Library.Class

There is a function called testFunction:

Function testFunction(var1 As Double, var2 As Double, var3 As Double, var4 As Double, _
var5 As Double, var6 As Integer, var7 As Double, var8 As Double, var9 As Double)
    Call testSetup(var1, var2, var3, var4, var6, var7, var8, var9)
    testFunction = testObject.Field(var5)
End Function

This function calls testSetup:

Sub testSetup(var1 As Double, var2 As Double, var3 As Double, var4 As Double, _
var5 As Double, var6 As Integer, var7 As Double, var8 As Double, var9 As Double)
    testObject.Lat1 = var1
    testObject.Lon1 = var2
    testObject.Lat2 = var3
    testObject.Lon2 = var4
    testObject.mth = var6
    testObject.GMT = var7
    testObject.ssn = var8
    testObject.icf = var9
End Sub

The problem I have is that upon running, if I place a break point at the location

testObject.Lat1 = var1

Upon running and stepping over to the next line, if I hover over the object

testObject.Lat1

I see the error

object variable or With block variable not set

There is NO Crash and no explicit error message causing a fail.

Anyone have any ideas?

I'm not sure if the error is within the library or the code?

Also it's run from excel by typing into a cell =testFunction(var1, var2, var3....)

Edit: More info in comments

2
Whats the scope of testObject ?user2063626
what do you mean by scope? Thxuser2111939
Please declare testObject at moudle level or public.user2063626
sorry, I don't really understand what you mean? I've declared testObject As Library.Class up the top of the code and then set testObject = New Library.Class in the sub testSetupuser2111939
link for you : cpearson.com/excel/scope.aspxuser2063626

2 Answers

2
votes

In VBA you can initiate an object via two methods:

  1. Dim o as New Object
  2. Dim o as Object: Set o = New Object

The difference is that in version one the object is initiated the first time it is access in the code! (Note: This is different to VB.NET). In the second case it gets initiated explicitly when your run the Set o = New Object.

Therefore, when you set your breakpoint in the line testObject.Lat1 = var1, testObject is actually not yet allocated and the debugger can therefore not access anything! Only after executing this line will you see a result.

If this bothers you, you simply need to add

Set testObject = new Library.Class
0
votes

OK, solution has been found. It wasn't due to errors in the code, the

Dim testObject As New Library.Class

worked fine.

As does

Dim testObject As Library.Class

followed by

Set testObject = New Library.Class

The problem was that in VB, I registered the library dll file through

Tools > References

I assumed this would be enough as I didn't have admin rights.

I was wrong.

I needed to log in as Administrator and thern register the dll using

regsvr32 ".dll path"

Now i'm seeing sensible outputs in the spreadsheet.