Bartek
Borczyk
Operator liczb i
słów
Temat: OBJECT REQUIRED?
Się samouczę i niestety napotkałem głupi problem.Mam klasę COM, która wygląda tak:
Imports Microsoft.Office.Interop
<ComClass(CTest.ClassId, CTest.InterfaceId, CTest.EventsId)> _
Public Class CTest
#Region "COM GUIDs"
' These GUIDs provide the COM identity for this class
' and its COM interfaces. If you change them, existing
' clients will no longer be able to access the class.
Public Const ClassId As String = "d6e057cf-3b7f-4315-8d4e-69085600b4ee"
Public Const InterfaceId As String = "beaadcfb-5ff0-4006-9cc6-36959372d547"
Public Const EventsId As String = "b9ed6d38-cd19-4eca-8a62-6f599d86fd5a"
#End Region
Dim mXlc As Excel.Application
Dim mWkb As Excel.Workbook
Dim mWks As Excel.Worksheet
' A creatable COM class must have a Public Sub New()
' with no parameters, otherwise, the class will not be
' registered in the COM registry and cannot be created
' via CreateObject.
Public Sub New()
mXlc = GetObject(, "Excel.Application")
mWkb = mXlc.ActiveWorkbook
mWks = mWkb.ActiveSheet
End Sub
Public Sub Testuj()
Dim i As Integer
Do
i += 1
With mWks.Cells(i, 1)
.Value = i
.Interior.ColorIndex = 5
End With
Loop Until i = 5
End Sub
Sub Testuj2(ByVal rng As Excel.Range)
MsgBox(rng.Address)
End Sub
End Class
Referencje COM bez copy local do:
- Microsoft Excel 12.0 Object Library
- Microsoft Office 12.0 Object Library
- Microsoft Visual Basic for Applications Extensibility 5.3
W testowym arkuszu jest tak:
Sub sTest()
Dim obj As DrugaDLL.CTest
Set obj = New DrugaDLL.CTest
With obj
.Testuj
End With
End Sub
Sub sTest2()
Dim obj As DrugaDLL.CTest
Set obj = New DrugaDLL.CTest
'przykład 1 z błędem
Dim rng As Range
Set rng = ThisWorkbook.Worksheets(1).Range("A1:C10")
obj.Testuj2 (rng)
'przykład 2 bez błędu
obj.Testuj2 (ThisWorkbook.Worksheets(1).Range("A1:C10"))
End Sub
Chodzi mi o metodę Testuj2, która w przykładzie 1 wyrzuca mi błąd "OBJECT REQUIRED". Gdy robię to samo w przykładzie 2 wszystko działa jak należy. Ktoś może mi powiedzieć dlaczego?