|
-
November 10th, 2009, 07:13 AM
#1
[RESOLVED] Some Explanation about Difference Between New and Createobject
Can anybody tell me .Why i am getting Error ActiveX Component Cannot CreateObject.here is the following Code.
Code:
Private Sub btPrint_Click()
Dim excl As Excel.Application
Dim wBook As Excel.Workbook
Dim ExlSheet As Excel.Worksheet
Dim m_SCompanyName As String
m_SCompanyName = "AL ARABI FACTORY FOR STEEL WORKS "
' Set excl = New Excel.Application
Set excl = CreateObject(Excel.Application)
Set wBook = excl.Workbooks.Add
Set ExlSheet = wBook.Worksheets(1)
excl.Visible = True
ExlSheet.Cells(2, 1).Value = m_SCompanyName
ExlSheet.Cells(3, 1).Value = "P.O. BOX 14044 DAMMAM 31424 "
ExlSheet.Cells(4, 1).Value = "KINGDOM OF SAUDI ARABIA "
ExlSheet.Cells(5, 1).Value = "PHONE : (+9663) 812-3070 "
ExlSheet.Cells(6, 1).Value = "FAX : (+9663) 812-3339 "
ExlSheet.Range("A1:A2").Font.Bold = True
ExlSheet.Range("A1:A2").Font.Italic = True
ExlSheet.Range("A1:A5").Font.Underline = True
' ActiveSheet.Pictures.Insert (App.path & "FirmSignation.jpg"), Range("B5:D10")
' Call PlacePicture
wBook.SaveAs App.path & "PurchaseOrder.Xls"
' wBook.Close savechanges:=True
Set ExlSheet = Nothing
Set wBook = Nothing
' excl.Quit
Set excl = Nothing
End Sub
But when i write a code Set excl = CreateObject(Excel.Application) instead of Set Set excl = New Excel.Application. i got error ActiveX Component Cannot Create Object
i got error ACtiveX Component Cannot create Object .Kindly let me know the Idea.if Possible Kindly Explain .These two Specific Line.
Code:
'Set excl = New Excel.Application
Set excl = CreateObject(Excel.Application)
Last edited by firoz.raj; November 10th, 2009 at 07:32 AM.
-
November 10th, 2009, 12:53 PM
#2
Re: Some Explanation about Difference Between New and Createobject
The first thing jumping to my eyes is: The CreateObject() needs a string as a parameter like
x = CreateObject("Lib.Objectname")
-
November 10th, 2009, 02:05 PM
#3
Re: Some Explanation about Difference Between New and Createobject
You question is already answered in Post #2. Just to add, New is used for early binding where in during design time itself your code knows which object is being created and what all properties/methods are available.
With CreateObject, you are using Late binding which means at design time the intellisense does not work and compiler does not know which methods/properties an object has. This can only be identified when the code is actually executed.
-
November 10th, 2009, 08:03 PM
#4
Re: Some Explanation about Difference Between New and Createobject
Hm... If there is a reference to the excel objects and if you declare
Dim excl as Excel.Application, all intellisense should work and show all properties and methods, because the type of the variable is not simply Object, but definitive Excel.Application.
As long as a reference to Excel objects exists, (which must, otherwise you couldn't declare this variable), also all intellisense should be aware of the methods and properties, no matter how you instantiate the object.
-
November 10th, 2009, 11:44 PM
#5
Re: Some Explanation about Difference Between New and Createobject
But my additioanal Question was. Why we cannot use Set sup=Createobject("Supplier") in the Following case.When i write instead of Set sup=New Supplier .then it says ActiveX Componet cannot CreateObject.or we can use CreateObject() in the case of Ole Server Appplication only ?.Kindly let me know the Idea.Any help would be highly appreciated.
Code:
Do Until rs.EOF
' Set sup = New Supplier
Set Sup=Createobject(Supplier)
sup.Supid = rs.Fields("sup_id")
sup.SupName = rs.Fields("Sup_name")
sup.ContactPerson = rs.Fields("contact_person") & vbNullString
sup.FaxNo = rs.Fields("fax_no") & vbNullString
sup.OfficeAddress = rs.Fields("office_address") & vbNullString
-
November 11th, 2009, 01:09 AM
#6
Re: Some Explanation about Difference Between New and Createobject
Answered above:
 Originally Posted by Shuja Ali
With CreateObject, you are using Late binding which means at design time the intellisense does not work and compiler does not know which methods/properties an object has.
-
November 11th, 2009, 07:02 AM
#7
Re: Some Explanation about Difference Between New and Createobject
As I said before: CreateObject() expects a STRING as an argument. As can be seen in your attachment, your argument to CreateObject() is lacking any quote, looking like:
Set sup = createobject(excel.app...
Try CreateObject("Excel.Application") in quotes
-
November 11th, 2009, 07:20 AM
#8
Re: Some Explanation about Difference Between New and Createobject
Thank You Sir.Now it is Working .here is the following Code.
Code:
Private Sub btPrint_Click()
Dim excl As excel.Application
Dim wBook As excel.Workbook
Dim ExlSheet As excel.Worksheet
Dim m_SCompanyName As String
m_SCompanyName = "AL ARABI FACTORY FOR STEEL WORKS "
'Set excl = New excel.Application
Set excl = CreateObject("excel.Application")
Set wBook = excl.Workbooks.Add
Set ExlSheet = wBook.Worksheets(1)
excl.Visible = True
ExlSheet.Cells(2, 1).Value = m_SCompanyName
ExlSheet.Cells(3, 1).Value = "P.O. BOX 14044 DAMMAM 31424 "
ExlSheet.Cells(4, 1).Value = "KINGDOM OF SAUDI ARABIA "
ExlSheet.Cells(5, 1).Value = "PHONE : (+9663) 812-3070 "
ExlSheet.Cells(6, 1).Value = "FAX : (+9663) 812-3339 "
ExlSheet.Range("A1:A2").Font.Bold = True
ExlSheet.Range("A1:A2").Font.Italic = True
ExlSheet.Range("A1:A5").Font.Underline = True
' ActiveSheet.Pictures.Insert (App.path & "FirmSignation.jpg"), Range("B5:D10")
' Call PlacePicture
wBook.SaveAs App.path & "PurchaseOrder.Xls"
' wBook.Close savechanges:=True
Set ExlSheet = Nothing
Set wBook = Nothing
' excl.Quit
Set excl = Nothing
End Sub
But i think we cannot use everytime CreateObject().we can use only in the case of oleServer Application case.But in the following condition Create object Does Not work.
Kindly give some Explanation on this topic.
Code:
dim m_sup as supplier
'Set m_sup = New Supplier
Set m_sup=CreateObject("Supplier") .i think this is not possible.?
m_sup.Supid = TxtSupid.Text
m_sup.SupName = txtSupplierName.Text
m_sup.OfficeAddress = TxtOfficeaddress.Text
Last edited by firoz.raj; November 16th, 2009 at 03:38 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|