What is a View?


The Microsoft Developer Tools Roadmap 2004 – 2005 lays out a good description of Microsoft Visual Studio Tools for the Microsoft Office System, Version 2.0. It describes how the new version separates data from view, so that you can program against the data in a document without having to code against the Microsoft Office Excel and Microsoft Office Word object models. But what is the view, exactly?


In the most generic sense, the view is the Microsoft Office Word or Microsoft Office Excel document. But view is also used more specifically to mean a container object that can contain controls. (It also contains a data cache, but more about that in a later post.)


There are new controls in Visual Studio Tools for Office, Version 2.0 that are called view controls. View controls are intrinsic Word or Excel objects that have been enhanced by the addition of events and data binding capability. View controls can be added to a Word or Excel document in much the same way that you would add Windows Forms controls to a Windows Form.


An example of a view control for Excel is the named range object, which is an Excel cell range that you identify with a name. Once you add a named range to a worksheet, Visual Studio Tools for Office, Version 2.0 creates a first class object that you can program against directly without having to traverse the Excel object model. For example, instead of writing Application.Worksheets(“Sheet1”).Cells(2, 2).Value2 = salesTotal, you could simply write TotalNamedRange.Value2 = salesTotal.


An example of a view control for Word is the bookmark, which you can program against in a similar way.


In other words, a view is a container (Word document or Excel worksheet) for view controls (NamedRange, Bookmark, and so on) that exposes events and can be bound to data.



  • View – container

  • View controls – contained inside view

  • Excel: view = worksheet, view control = range

  • Word: view = document, view control = bookmark

— Harry Miller


—–
This posting is provided “AS IS” with no warranties, and confers no rights.


Comments (7)

  1. Julie Kremer says:

    If the View is bound to a Word Bookmark, can the bookmark already have data in it? If the bookmark has data in it, will it overwrite the data in the bookmark? This sounds so useful… and sad that I won’t have it for a very long time given the project I’m doing now.

  2. Stephane Rodriguez says:

    In Excel, a range object is rather versatile and can be an union of ranges, or some other combination. How do you manage the implicit object model with imbricated objects, for instance if I programmatically want to change the individual border of each portion of a multi-selection based range?

    And how do you control charts if the view control is a range? (May be I got all wrong, but does it mean by the way, as in SpreadsheetML, that charts are persona non grata?)

  3. Eric Lippert says:

    > If the View is bound to a Word Bookmark, can the bookmark already have data in it? If the bookmark has data in it, will it overwrite the data in the bookmark?

    Whoa! Slow down, Julie Kremer! :-)

    I’m sure that a detailed discussion of our view control data binding code is coming. To briefly answer your questions: it depends. Databinding necessarily involves maintaining two copies of the data — one in the view, one in the dataset. When they differ, there must be some conflict resolution algorithm. In general, the conflict resolutiont is "whichever changed last wins."

    Obviously we want to avoid the situation where a bookmark or range or whatever gets databound and suddenly all the data in it disappears because it was bound to an empty dataset. We’re working on getting the semantics of that binding operation right.

  4. Eric Lippert says:

    > In Excel, a range object is rather versatile and can be an union of ranges,

    Just to be precise, a range is a union of "areas", not ranges.

    > How do you manage the implicit object model with imbricated objects,

    > for instance if I programmatically want to change the individual border

    > of each portion of a multi-selection based range?

    The view control model is quite simple — if it is a named range, we generate a view control. If a named range contains some non-named areas, then those are accessible via the Areas collection just like they always were.

    I’m not sure if that answers your question or not. Please clarify if it does not.

    >how do you control charts if the view control is a range?

    Charts and chart sheets are also view controls themselves.

  5. It’s been a while since I wrote any Office code. But this puzzles me:

    Application.Worksheets("Sheet1").Cells(2, 2).Value2 = salesTotal, you could simply write TotalNamedRange.Value2 = salesTotal

    Can’t you do SomeObject = Application.Worksheets("Sheet1").Cells(2, 2) and get SomeObject.Value2 syntax already?

  6. Kathleen McGrath says:

    Yes, you can assign the range to a variable and then set its value, but you would still need to know how to traverse the object model to get to that range. With the NamedRange view control, you don’t have know where it is located on the sheet or how to access it via Excel’s object model, you can simply program against the properties of the object directly. Also, the NamedRange control is more feature-rich then the Range object.