Applying Selected DataGraphic to the Whole Document

A recent newsgroup question asked if there is a way to apply a DataGraphic to all pages in a document.  Well, there is no built-in option in Visio 2007 (or Visio 2010), but it can be done with a little bit of VBA.  This blog presents a macro that can be used in most cases.

The specific use case was for an Organization Chart that has many pages, and it can be really tedious to make each page active, select all the shapes, then apply a DataGraphic, so I have written a macro that allows you to apply the required DataGraphic to a single shape, then you select that shape; run the macro to apply the same DataGraphic to all shapes in the document that has a Shape Data row with the same name as the the first Shape Data row in the selected shape.

The screenshots are from the Visio 2010 Beta, but the code will work for Visio 2007 Professional edition too.



So, here is the macro that achieves the desired result:

   1: Option Explicit
   3: Public Sub ApplyDataGraphicToDocument()
   4: Dim mstDG As Visio.Master
   5: Dim shp As Visio.Shape
   6: Dim pag As Visio.Page
   7: Dim firstProp As String
   9:     If Visio.ActiveWindow.Selection.Count = 0 Then
  10:         MsgBox "Please select a shape which already has data graphics"
  11:         Exit Sub
  12:     Else
  13:         Set shp = Visio.ActiveWindow.Selection.PrimaryItem
  14:         If shp.DataGraphic Is Nothing Then
  15:             MsgBox "Please select a shape which already has data graphics"
  16:             Exit Sub
  17:         Else
  18:             'Get the shapes DataGraphic master
  19:             Set mstDG = shp.DataGraphic
  20:             'Get the name of the first Shape Data row
  21:             firstProp = "Prop." & _
  22:                 shp.CellsSRC(Visio.visSectionProp, 0, 0).RowNameU
  23:         End If
  24:     End If
  26:     For Each pag In Visio.ActiveDocument.Pages
  27:         If pag.Type = visTypeForeground Then
  28:             For Each shp In pag.Shapes
  29:                 'Check that the named Shape Data row exists
  30:                 If shp.CellExistsU(firstProp, Visio.visExistsAnywhere) Then
  31:                     'Set the DataGraphic
  32:                     shp.DataGraphic = mstDG
  33:                 End If
  34:             Next
  35:         End If
  36:     Next
  38: End Sub



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.


O365 and SharePoint

Simplify Tasks

Want to learn the simple way?

Paul Turley's SQL Server BI Blog

sharing my experiences with the Microsoft data platform, SQL Server BI, Data Modeling, SSAS Design, Power Pivot, Power BI, SSRS Advanced Design, Power BI, Dashboards & Visualization since 2009

John Goldsmith's visLog

be smart, be clear, be visual ...

Mo's blog

Personal views on Dynamics 365 for Operations and Technical Architecture.

Chris Webb's BI Blog

Microsoft Power BI, Analysis Services, MDX, DAX, M, Power Pivot and Power Query

Azure Solutions for Office 365, and more...

Rob Fahrni



Life with Visio and other Microsoft Toys!

Nilsandrey's Weblog

Just another weblog

Things that Should be Easy

Every so often (too often in the IT industry) I encounter things that should have been very easy to do but turned out to be far too complicated. My favorite topics include SharePoint, .Net development, and software architecture, especially distributed systems.

Visio Guy

Smart graphics for visual people

%d bloggers like this: