A Graphics Component For Microsoft ® Office Applications Programming In Visio ® – Part 1 Mr....

24
A Graphics Component For A Graphics Component For Microsoft Microsoft ® Office Office Applications Applications Programming In Visio Programming In Visio ® – Part 1 – Part 1 Mr. David A. Edson, M.Arch. Mr. David A. Edson, M.Arch. Technical Product Manager Technical Product Manager Developer Tools Developer Tools Microsoft Corporation Microsoft Corporation 4-301 4-301

Transcript of A Graphics Component For Microsoft ® Office Applications Programming In Visio ® – Part 1 Mr....

Page 1: A Graphics Component For Microsoft ® Office Applications Programming In Visio ® – Part 1 Mr. David A. Edson, M.Arch. Technical Product Manager Developer.

A Graphics Component For A Graphics Component For MicrosoftMicrosoft®® Office Applications Office ApplicationsProgramming In VisioProgramming In Visio®® – Part 1 – Part 1

Mr. David A. Edson, M.Arch.Mr. David A. Edson, M.Arch.Technical Product ManagerTechnical Product ManagerDeveloper ToolsDeveloper ToolsMicrosoft CorporationMicrosoft Corporation

4-3014-301

Page 2: A Graphics Component For Microsoft ® Office Applications Programming In Visio ® – Part 1 Mr. David A. Edson, M.Arch. Technical Product Manager Developer.
Page 3: A Graphics Component For Microsoft ® Office Applications Programming In Visio ® – Part 1 Mr. David A. Edson, M.Arch. Technical Product Manager Developer.

A Graphics Component A Graphics Component For Microsoft OfficeFor Microsoft Office The key is DATAThe key is DATA Visio is a means to visualize your dataVisio is a means to visualize your data

in graphical as well as textual formin graphical as well as textual form Businesses generate vast amounts Businesses generate vast amounts

of data dailyof data daily Various functional areas within businesses Various functional areas within businesses

need to share that dataneed to share that data Not everyone comprehends data presented Not everyone comprehends data presented

in its native formatin its native format A picture is indeed worth a thousand wordsA picture is indeed worth a thousand words

Page 4: A Graphics Component For Microsoft ® Office Applications Programming In Visio ® – Part 1 Mr. David A. Edson, M.Arch. Technical Product Manager Developer.

Data Sharing Data Sharing Enabled ApplicationsEnabled Applications Access stores data as records comprised of fieldsAccess stores data as records comprised of fields Excel stores data as arrays of inter-related cellsExcel stores data as arrays of inter-related cells Word stores data as streams of text with Word stores data as streams of text with

embedded graphicsembedded graphics PowerPointPowerPoint®® stores data as basic images of text stores data as basic images of text

and picturesand pictures OutlookOutlook®® stores data as records of text and field stores data as records of text and field

related informationrelated information Visual Basic for Applications is the glue that binds Visual Basic for Applications is the glue that binds

these various data sources through automationthese various data sources through automation Visio communicates with all of these data sources Visio communicates with all of these data sources

through Visual Basic for Applications and other through Visual Basic for Applications and other automation controllersautomation controllers

Page 5: A Graphics Component For Microsoft ® Office Applications Programming In Visio ® – Part 1 Mr. David A. Edson, M.Arch. Technical Product Manager Developer.

Practical ExamplesPractical Examples

Excel data into Visio diagramsExcel data into Visio diagrams Organizational chartingOrganizational charting

Visio data reported to ExcelVisio data reported to Excel Asset tracking and reportingAsset tracking and reporting

Word data into Visio diagramsWord data into Visio diagrams Literary structureLiterary structure

Visio data reported to WordVisio data reported to Word Business processesBusiness processes

Page 6: A Graphics Component For Microsoft ® Office Applications Programming In Visio ® – Part 1 Mr. David A. Edson, M.Arch. Technical Product Manager Developer.

But Before We Begin…But Before We Begin…

Remember that the automation/object Remember that the automation/object model is actually looking at Visio’s model is actually looking at Visio’s prime/core technology – prime/core technology – the ShapeSheetthe ShapeSheet®® interface interface

Automation calls are actually feeding Automation calls are actually feeding information into and pulling information into and pulling information from ShapeSheet cells – information from ShapeSheet cells – both for SmartShapeboth for SmartShape®® symbols and symbols and the page itselfthe page itself

Page 7: A Graphics Component For Microsoft ® Office Applications Programming In Visio ® – Part 1 Mr. David A. Edson, M.Arch. Technical Product Manager Developer.

How Is The Data Shared?How Is The Data Shared?

Robust object models are the key to Robust object models are the key to data access and sharingdata access and sharing

Object models are hierarchal structuresObject models are hierarchal structures Each object has properties, methods Each object has properties, methods

and eventsand events An object’s property may be an object An object’s property may be an object

further down the hierarchyfurther down the hierarchy Through object models data is Through object models data is

accessed and then read by accessed and then read by another applicationanother application

Page 8: A Graphics Component For Microsoft ® Office Applications Programming In Visio ® – Part 1 Mr. David A. Edson, M.Arch. Technical Product Manager Developer.

Excel ApplicationObject

Visio ApplicationObject

Excel WorkbooksObject

Visio DocumentsObject

Workbooks.ItemProperty > ExcelWorkbook Object

Documents.ItemProperty > Visio

Document Object

Excel WorkbookObject

Visio DocumentObject

Workbook.Worksheet Property >

WorksheetsObject

Document.PagesProperty > Pages

Object

Excel WorksheetsObject

Visio PagesObject

Worksheets.ItemProperty >

Worksheet Object

Pages.ItemProperty > Page

Object

Excel WorksheetObject

Visio Page ObjectWorksheet.CellProperty > Cell

Object

Page.ShapesProperty>Shapes

Object

Excel Cell ObjectVisio Shapes

Object

Shapes.ItemProperty>Shape

Object

Visio ShapeObject

Shape.CellsProperty > Cell

Object

Cell.Result orCell.Formula

Cell.Value

Excel - Visio Data TransferExcel - Visio Data Transfer

Page 9: A Graphics Component For Microsoft ® Office Applications Programming In Visio ® – Part 1 Mr. David A. Edson, M.Arch. Technical Product Manager Developer.

Traversing Visio’s Traversing Visio’s Object ModelObject ModelObtain a reference to the Application Object:Obtain a reference to the Application Object: 

Public Sub GetAppObject ()Public Sub GetAppObject () Dim appVisio As Visio.ApplicationDim appVisio As Visio.Application Set appVisio = GetObject(“Visio.Application”)Set appVisio = GetObject(“Visio.Application”)End SubEnd Sub

Page 10: A Graphics Component For Microsoft ® Office Applications Programming In Visio ® – Part 1 Mr. David A. Edson, M.Arch. Technical Product Manager Developer.

Traversing Visio’s Traversing Visio’s Object ModelObject ModelGet a reference to the Documents Collection Object by calling upon Get a reference to the Documents Collection Object by calling upon the Documents Property of the Application Object:the Documents Property of the Application Object: 

Public Sub GetDocsObject ()Public Sub GetDocsObject () Dim appVisio As Visio.ApplicationDim appVisio As Visio.Application Dim docsObj As Visio.DocumentsDim docsObj As Visio.Documents Set appVisio = GetObject(“Visio.Application”)Set appVisio = GetObject(“Visio.Application”) Set docsObj = appVisio.DocumentsSet docsObj = appVisio.DocumentsEnd SubEnd Sub

Page 11: A Graphics Component For Microsoft ® Office Applications Programming In Visio ® – Part 1 Mr. David A. Edson, M.Arch. Technical Product Manager Developer.

Traversing Visio’s Traversing Visio’s Object ModelObject ModelObtain a reference to the first document in the documents Obtain a reference to the first document in the documents collection by calling upon the Item Property of the Documents collection by calling upon the Item Property of the Documents Collection Object:Collection Object: 

Public Sub GetDocObject ()Public Sub GetDocObject () Dim appVisio As Visio.ApplicationDim appVisio As Visio.Application Dim docsObj As Visio.DocumentsDim docsObj As Visio.Documents Dim docObj As Visio.DocumentDim docObj As Visio.Document Set appVisio = GetObject(“Visio.Application”)Set appVisio = GetObject(“Visio.Application”) Set docsObj = appVisio.DocumentsSet docsObj = appVisio.Documents Set docObj = docsObj.Item(1)Set docObj = docsObj.Item(1)End SubEnd Sub

Page 12: A Graphics Component For Microsoft ® Office Applications Programming In Visio ® – Part 1 Mr. David A. Edson, M.Arch. Technical Product Manager Developer.

Traversing Visio’s Traversing Visio’s Object ModelObject ModelBecause Visio includes VBA, there is a very elegant shortcut to Because Visio includes VBA, there is a very elegant shortcut to reference the currently active document. Keep in mind that it is not reference the currently active document. Keep in mind that it is not necessary to obtain a reference to the Application Object because necessary to obtain a reference to the Application Object because owing to the fact that you are working within Visio’s VBA you owing to the fact that you are working within Visio’s VBA you already have an implicit reference to the Application Object:already have an implicit reference to the Application Object: 

Public Sub GetDocObject ()Public Sub GetDocObject () Dim docObj As Visio.DocumentDim docObj As Visio.Document Set docObj = Visio.ActiveDocumentSet docObj = Visio.ActiveDocumentEnd SubEnd Sub

Page 13: A Graphics Component For Microsoft ® Office Applications Programming In Visio ® – Part 1 Mr. David A. Edson, M.Arch. Technical Product Manager Developer.

Traversing Visio’s Traversing Visio’s Object ModelObject ModelObtain a reference to the collection of Pages in the document by Obtain a reference to the collection of Pages in the document by calling upon the Pages Property of the Document Object:calling upon the Pages Property of the Document Object: 

Public Sub GetPagsObject ()Public Sub GetPagsObject () Dim docObj As Visio.DocumentDim docObj As Visio.Document Dim pagsObj As Visio.PagesDim pagsObj As Visio.Pages Set docObj = Visio.ActiveDocumentSet docObj = Visio.ActiveDocument Set pagsObj = docObj.PagesSet pagsObj = docObj.PagesEnd SubEnd Sub

Page 14: A Graphics Component For Microsoft ® Office Applications Programming In Visio ® – Part 1 Mr. David A. Edson, M.Arch. Technical Product Manager Developer.

Traversing Visio’s Traversing Visio’s Object ModelObject ModelGet a reference to the first Page in the Pages Collection by calling Get a reference to the first Page in the Pages Collection by calling upon the Item Property of the Pages Collection Object:upon the Item Property of the Pages Collection Object: 

Public Sub GetPagObject ()Public Sub GetPagObject () Dim docObj As Visio.DocumentDim docObj As Visio.Document Dim pagsObj As Visio.PagesDim pagsObj As Visio.Pages Dim pagObj As Visio.PageDim pagObj As Visio.Page Set docObj = Visio.ActiveDocumentSet docObj = Visio.ActiveDocument Set pagsObj = docObj.PagesSet pagsObj = docObj.Pages Set pagObj = pagsObj.Item(1)Set pagObj = pagsObj.Item(1)End SubEnd Sub

Page 15: A Graphics Component For Microsoft ® Office Applications Programming In Visio ® – Part 1 Mr. David A. Edson, M.Arch. Technical Product Manager Developer.

Traversing Visio’s Traversing Visio’s Object ModelObject ModelAgain…there is a quick method of obtaining a reference to the Again…there is a quick method of obtaining a reference to the currently active Page in a Visio document through VBA by currently active Page in a Visio document through VBA by referencing the ActivePage Object:referencing the ActivePage Object: Public Sub GetPagObject ()Public Sub GetPagObject () Dim pagObj As Visio.PageDim pagObj As Visio.Page Set pagObj = Visio.ActivePageSet pagObj = Visio.ActivePageEnd SubEnd Sub

Page 16: A Graphics Component For Microsoft ® Office Applications Programming In Visio ® – Part 1 Mr. David A. Edson, M.Arch. Technical Product Manager Developer.

Traversing Visio’s Traversing Visio’s Object ModelObject ModelGet a reference to the collection of Visio SmartShape symbols Get a reference to the collection of Visio SmartShape symbols that reside on that page by calling the Shapes Property of the that reside on that page by calling the Shapes Property of the Page Object:Page Object: 

Public Sub GetShpsObject ()Public Sub GetShpsObject () Dim pagObj As Visio.PageDim pagObj As Visio.Page Dim shpsObj As Visio.ShapesDim shpsObj As Visio.Shapes Set pagObj = Visio.ActivePageSet pagObj = Visio.ActivePage Set shpsObj = pagObj.ShapesSet shpsObj = pagObj.ShapesEnd SubEnd Sub

Page 17: A Graphics Component For Microsoft ® Office Applications Programming In Visio ® – Part 1 Mr. David A. Edson, M.Arch. Technical Product Manager Developer.

Traversing Visio’s Traversing Visio’s Object ModelObject ModelGet a reference to the first Visio SmartShape symbol on that Page Get a reference to the first Visio SmartShape symbol on that Page by calling upon the Item Property of the Shapes Collection Object:by calling upon the Item Property of the Shapes Collection Object: 

Public Sub GetShpObject ()Public Sub GetShpObject () Dim pagObj As Visio.PageDim pagObj As Visio.Page Dim shpsObj As Visio.ShapesDim shpsObj As Visio.Shapes Dim shpObj As Visio.ShapeDim shpObj As Visio.Shape Set pagObj = Visio.ActivePageSet pagObj = Visio.ActivePage Set shpsObj = pagObj.ShapesSet shpsObj = pagObj.Shapes Set shpObj = shpsObj.Item(1)Set shpObj = shpsObj.Item(1)End SubEnd Sub 

Page 18: A Graphics Component For Microsoft ® Office Applications Programming In Visio ® – Part 1 Mr. David A. Edson, M.Arch. Technical Product Manager Developer.

Traversing Visio’s Traversing Visio’s Object ModelObject ModelAn alternate to obtaining a SmartShape symbol by index is calling An alternate to obtaining a SmartShape symbol by index is calling for it by its name:for it by its name: 

Public Sub GetShpObject ()Public Sub GetShpObject () Dim pagObj As Visio.PageDim pagObj As Visio.Page Dim shpsObj As Visio.ShapesDim shpsObj As Visio.Shapes Dim shpObj As Visio.ShapeDim shpObj As Visio.Shape Set pagObj = Visio.ActivePageSet pagObj = Visio.ActivePage Set shpsObj = pagObj.ShapesSet shpsObj = pagObj.Shapes Set shpObj = shpsObj.Item(“Connector.1”)Set shpObj = shpsObj.Item(“Connector.1”)End SubEnd Sub 

This presumes that a SmartShape symbol exists on the active This presumes that a SmartShape symbol exists on the active Page which has the name of “Connector.1”Page which has the name of “Connector.1”

Page 19: A Graphics Component For Microsoft ® Office Applications Programming In Visio ® – Part 1 Mr. David A. Edson, M.Arch. Technical Product Manager Developer.

Traversing Visio’s Traversing Visio’s Object ModelObject ModelYet another alternative is to obtain the reference to the desired Yet another alternative is to obtain the reference to the desired Visio SmartShape Symbol by locating the currently selected Visio SmartShape Symbol by locating the currently selected SmartShape Symbol. To do this we utilize the Selection Property of SmartShape Symbol. To do this we utilize the Selection Property of the Active Window Object and select the first Item in the Selection:the Active Window Object and select the first Item in the Selection: 

Public Sub GetShpObject ()Public Sub GetShpObject () Dim windObj As Visio.WindowDim windObj As Visio.Window Dim shpObj As Visio.ShapeDim shpObj As Visio.Shape SetwindObj = Visio.ActiveWindowSetwindObj = Visio.ActiveWindow Set shpObj = windObj.Selection.Item(1)Set shpObj = windObj.Selection.Item(1)End SubEnd Sub

Page 20: A Graphics Component For Microsoft ® Office Applications Programming In Visio ® – Part 1 Mr. David A. Edson, M.Arch. Technical Product Manager Developer.

Traversing Visio’s Traversing Visio’s Object ModelObject ModelNow that we have a reference to the SmartShape symbol in Now that we have a reference to the SmartShape symbol in question we can obtain a reference to the Width Cell. We do this question we can obtain a reference to the Width Cell. We do this by calling for the Cells property of the Shape Object:by calling for the Cells property of the Shape Object: 

Public Sub GetCelObject ()Public Sub GetCelObject () Dim windObj As Visio.WindowDim windObj As Visio.Window Dim shpObj As Visio.ShapeDim shpObj As Visio.Shape Dim celObj As Visio.CellDim celObj As Visio.Cell SetwindObj = Visio.ActiveWindowSetwindObj = Visio.ActiveWindow Set shpObj = windObj.Selection.Item(1)Set shpObj = windObj.Selection.Item(1) Set celObj = shpObj.Cells(“Width”)Set celObj = shpObj.Cells(“Width”)End SubEnd Sub

Note that the Cells Property contains a mandatory argument Note that the Cells Property contains a mandatory argument which is the name of the cell we wish to referencewhich is the name of the cell we wish to reference

Page 21: A Graphics Component For Microsoft ® Office Applications Programming In Visio ® – Part 1 Mr. David A. Edson, M.Arch. Technical Product Manager Developer.

Traversing Visio’s Traversing Visio’s Object ModelObject ModelFinally, with a Cell Object referenced, we can extract the value in Finally, with a Cell Object referenced, we can extract the value in the cell by calling for the Result Property of the Cell Object, the cell by calling for the Result Property of the Cell Object, specifying the units we wish to see the value returned in. The value specifying the units we wish to see the value returned in. The value will be returned as a double:will be returned as a double: 

Public Sub GetCelValue ()Public Sub GetCelValue () Dim windObj As Visio.WindowDim windObj As Visio.Window Dim shpObj As Visio.ShapeDim shpObj As Visio.Shape Dim celObj As Visio.CellDim celObj As Visio.Cell Dim celVal As DoubleDim celVal As Double SetwindObj = Visio.ActiveWindowSetwindObj = Visio.ActiveWindow Set shpObj = windObj.Selection.Item(1)Set shpObj = windObj.Selection.Item(1) Set celObj = shpObj.Cells(“Width”)Set celObj = shpObj.Cells(“Width”) CelVal = celObj.Result(“in.”)CelVal = celObj.Result(“in.”)End SubEnd Sub

Page 22: A Graphics Component For Microsoft ® Office Applications Programming In Visio ® – Part 1 Mr. David A. Edson, M.Arch. Technical Product Manager Developer.

More Tips, Tricks More Tips, Tricks And Toys…And Toys… Using Visio’s Database WizardUsing Visio’s Database Wizard Using Visio’s OrgChart WizardUsing Visio’s OrgChart Wizard

Page 23: A Graphics Component For Microsoft ® Office Applications Programming In Visio ® – Part 1 Mr. David A. Edson, M.Arch. Technical Product Manager Developer.

Thank You!Thank You!Remember your evaluationsRemember your evaluations(what can make this better?)(what can make this better?)

Contact:Contact:

Mr. David A. Edson, M.Arch.Mr. David A. Edson, M.Arch.Technical Product ManagerTechnical Product ManagerDeveloper Tools MarketingDeveloper Tools MarketingMicrosoft CorporationMicrosoft Corporation

[email protected]@microsoft.com

Page 24: A Graphics Component For Microsoft ® Office Applications Programming In Visio ® – Part 1 Mr. David A. Edson, M.Arch. Technical Product Manager Developer.