CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Hybrid View

  1. #1
    Join Date
    Nov 2010
    Posts
    3

    Post Reading Object Hierarchy in a Object Model

    Hello Folks,

    I am quite new to the world of Object Oriented Programming. But, it does not mean that I am completely novice. I understand all the concepts of OOPLs. I work with VB, VBScript, JavaScript and Visual C++. I am trying to Automate an application using its Object Model provided by the developer. I am also trying to understand objects in,Word, Excel and other common Windows applications. I have a good programming skills, but when it comes to using existing objects or library, I do not understand the objects hierarchical concept. I know objects have parent-child relationships and some of them are inherited from another objects. And, what I understood from programming books that, an object contain methods and properties. But, when I use Excel Object model for retrieving values, the line of codes looks like this in VBscript;

    Set oExcelApp = CreateObject("Excel.Application")
    Set oWorkbook = oExcelApp.Workbooks.Open("G:\Documents and Settings\Arjun\My Documents\VBScripts\Practices\Excel Data\usernames.xlsx")

    Set mySheet = oWorkbook.Sheets.Item("Sheet1")

    As you see in the above code, I instantiated Excel Application Object and then Started using new object for opening an new spreadsheet, and new sheet.

    oExcelApp is an Object reference as I created. Workbooks is a property of oExcelApp object and Open() is a method of oExcelApp.

    Here is the part that I don't understand. How come a property will have a method, which is Open() in this case. Is workbook an Object? can object contain another object? Can Workbook be an object as well as property. I am totally confused.

    My understanding based on what I read, if Excel Application is an object, then it has all the objects, new, open, close, save, edit, tools, options, just like what I see in GUI. And then each of these items will have methods and properties. I am not sure if I am thinking in right direction. but I need very good understanding of how these objects are organized and how I can refer them to use them in my programming.

    Please help me if you know how to understand these objects.

    Thank you.

    Arjun

  2. #2
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: Reading Object Hierarchy in a Object Model

    Quote Originally Posted by arjunken View Post
    Workbooks is a property of oExcelApp object [...].
    Yes.

    [...] and Open() is a method of oExcelApp.
    No, it's a method of oExcelApp.Workbooks.

    Here is the part that I don't understand. How come a property will have a method, which is Open() in this case. Is workbook an Object? can object contain another object? Can Workbook be an object as well as property. I am totally confused.
    While the most common notion of the term "property" is an object of a simple type like a POD (or at least nothing much more complex), it actually can be almost any kind of object of arbitrary complexity. A property is a member variable (of any type) as opposed to a method, which is also known as member function.

    So yes, Workbook is an object, an object can contain another object and Workbook is a property of some object type, which is no contradiction.

    You know that a property can of course be a string, which is an object with methods and properties, at least in C++ and many other languages. But it's an example for an object that is "not very much" more complex than a POD like int.

    My understanding based on what I read, if Excel Application is an object, then it has all the objects, new, open, close, save, edit, tools, options, just like what I see in GUI.
    In fact, the menus you list are objects from the point of view of the application Excel itself. But from the point of view of the object model exposed by Excel, the menu items are methods of various object model objects.

    HTH

    Although it wasn't much of a problem with the tiny snippet in your post, please use code tags when posting code in the future.

    Ah, and... welcome to CodeGuru!
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  3. #3
    Join Date
    Nov 2010
    Posts
    3

    Re: Reading Object Hierarchy in a Object Model

    Hello Eri523,

    Thanks for answering my question. It really cleared my notion that property can only be value or an attribute which describe the object. I never imagined that a property of the object can also be considered as another object. When I was going through a book, it explained OOP concept with a Car example. Author says, Car object can have different properties such as door, steering wheel, wheels etc., and methods can be drive, stop, honk etc. He never mentioned door is another object.. but anyway..I am happy that I have climbed another step in OOP. Thank you.

    One other thing in my Excel example, How are these objects( Excel's thousands of objects) are organized. I believe most object models are organized in Hierarchical format. If so, how do programmers remember all the objects and their path. If I have to say, they are organized just like files and folders in windows explorer, How do I know which path I should take to access an object I want. For instance, in Excel, If I want to insert a symbol in one of the cells. How do I access this symbol object. I can just imagine that, the symbol object might be located in "insert" ->"Symbol". But that does not work always. I need to have a proper path knowledge of all the objects. How do programmers handle this? I am asking this question because, I have an object model from an application which I need to automate.

    Thank you for welcoming to CodeGuru..Its great forum for budding and professional programmers.

    By the way, How do you use Quote tags?

    Thanks
    Arjun

  4. #4
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: Reading Object Hierarchy in a Object Model

    Quote Originally Posted by arjunken View Post
    When I was going through a book, it explained OOP concept with a Car example. Author says, Car object can have different properties such as door, steering wheel, wheels etc., and methods can be drive, stop, honk etc. He never mentioned door is another object..
    Sounds like one of these ubiquotious OOP examples. And although it fact this isn't mentioned in most of these examples, the car picture can eaisly be used to describe the use of member objects as well. Take a Car object Car myCar;. Then it's quite reasonable to assume that you can do something like myCar.door.open();.

    One other thing in my Excel example, How are these objects( Excel's thousands of objects) are organized. I believe most object models are organized in Hierarchical format.
    Yes, the Excel object model is indeed hierachical.

    If so, how do programmers remember all the objects and their path. If I have to say, they are organized just like files and folders in windows explorer, How do I know which path I should take to access an object I want.
    Well, I assume most programmers can't enumerate all the Excel objects (maybe even with all their properties and methods) off the top of their head. If you click the Help button in the Excel VBA editor, you'll first get a TOC that contains three entries. Expand the first one of them, "MS Excel Visual Basic Reference" and you'll see another entry labelled "MS Excel Object Model". Clicking this gives you a nice chart of the object model, where each of the boxes is clickable and leads you to the documentation on that object. (Description based on German Excel 2003. The translation of some items back to English might not be completely exact.)

    For instance, in Excel, If I want to insert a symbol in one of the cells. How do I access this symbol object. I can just imagine that, the symbol object might be located in "insert" ->"Symbol". But that does not work always.
    "Insert -> Symbol" is no good example, because it's one of the menu commands that does not have a direct VBA equivalent. Recording the insertion of a Euro symbol as a macro produces this instruction:

    Code:
        ActiveCell.FormulaR1C1 = "€"
    A command like this (not necessarily exact, but at least similar) would be used as well to insert any arbitrary text, value or formula. But this answers another question: No, you don't need to always specify the entire object path to reach a particular object. For some commonly accessed objects there are shortcuts like the above. A fully qualified object access to insert a text could look like this:

    Code:
      Application.ActiveWorkbook.ActiveSheet.Cells(1, 1).Formula = "Bla"
    They are not exactly equivalent: The first one inserts into the currently selected cell, the second one always into A1. Sorry, I didn't take the effort to find the exact equivalent , but you should get the idea behind it. You also can store intermediate objects on a path in an object variable and use them multiple times.

    All this is VBA, thuogh, and as I understand your question, you want to automate Excel from another application. I suppose that is to some degree less convenient. I haven't ever done that myself, so I can't tell you much abut that, but certainly others around here can.

    But if you should have questions regarding VBA, they are handled in the VB6 forum here on the site.

    By the way, How do you use Quote tags?
    Quote tags are inserted using the "Bubble" button in the toolbar of any flavour of post editor here. The entire message you reply to is also quoted automatically if you begin replying by clicking the "Quote" button at the end of each post.

    If you meant the code tags that I recommended, they are inserted using the button labeled "#" in the standard or advanced editor.

    You can also enter any tag manually of course, and there are many more than you can insert using buttons. You find their description at http://www.codeguru.com/forum/misc.php?do=bbcode.
    Last edited by Eri523; November 23rd, 2010 at 09:36 AM.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  5. #5
    Join Date
    Nov 2010
    Posts
    3

    Re: Reading Object Hierarchy in a Object Model

    Eri523,

    Thank you for the detailed response. As you said, I also think all programmers refer to some kind of reference. Otherwise, it is tough to identify all the objects and their usage. But, one thing I did not know that excel has inbuilt objects which actually prevent using entire path like you mentioned about 'ActiveCell'.

    My application is a bit complicated one. I have a model reference. I am trying to understand how they are put together. I also check the instant hint when I type anything in the editor.

    Thank you again for clarifying my doubt.

    -Arjun

  6. #6
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: Reading Object Hierarchy in a Object Model

    Quote Originally Posted by arjunken View Post
    I also check the instant hint when I type anything in the editor.
    You mean these pop-up list boxes that offer you the choice between the available methods and properties as well as function parameters? This feature is called intellisense and it is really helpful, but IMO only in combination with the documentation, in order to actually know what you select there.

    Good luck with your app! If you encounter problems while writing it, you'll certainly find many people in here who are willing to help.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

Tags for this Thread

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