CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Dec 2008
    Location
    Step Into(F11)
    Posts
    465

    [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.

  2. #2
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    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")

  3. #3
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    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.

  4. #4
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    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.

  5. #5
    Join Date
    Dec 2008
    Location
    Step Into(F11)
    Posts
    465

    Question 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

  6. #6
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Some Explanation about Difference Between New and Createobject

    Answered above:

    Quote Originally Posted by Shuja Ali View Post
    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.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  7. #7
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    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

  8. #8
    Join Date
    Dec 2008
    Location
    Step Into(F11)
    Posts
    465

    Question 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
  •  





Click Here to Expand Forum to Full Width

Featured