Development

Tips & code for programming Visio & using the ShapeSheet

Maps & Geography

Maps & geography shapes & templates galore!

Network

Shapes & tools for Visio network diagrams.

News

Find out what’s new in the Visio world!

Shapes

Download useful & attractive Visio shapes. For free!

where can i buy windows 7 cheapest buy windows 7 upgrade from xp windows vista purchase price buy inventor 2010 pctools spyware doctor 5.5 buy windows 7 best price norton ghost 12 product key windows 7 price future shop windows xp price singapore windows 2008 web server download omnipage coupon buy windows 7 starter download adobe indesign cs3 for mac buy vista to windows 7 microsoft project 2003 download buy windows xp operating system windows 7 home premium cheap mathcad pricing photoshop buy mac discount autocad 2009 purchase microsoft money 2007 deluxe download photoshop mac autodesk maya 2009 price windows vista business download windows 7 price 64 bit purchase windows 7 ultimate full version buy windows 7 home premium key lightroom for apple purchase windows xp india adobe cs4 photoshop buy where to buy encarta 2009 purchase vista oem buy windows 7 tesco cheap adobe fireworks windows 7 price online best price corel draw 11 mac discount powerpoint buy microsoft office for mac canada buy microsoft project 2002 adobe production premium mac turbotax 2009 deluxe best price windows 7 ultimate 64 bit best price buy windows 7 amazon adobe flash pricing adobe dreamweaver sale purchase windows 7 ultimate buy quickbooks pro 2007 roxio copy & convert 3 buy download photoshop elements 6 steinberg nuendo price uniblue registrybooster 2009 download cheap photoshop programs adobe photoshop elements cheapest buy windows 7 future shop adobe presenter 7 download microsoft office 2003 sale buy quarkxpress 8 for mac purchase adobe cs3 design premium purchase after effects cs3 paperport professional 12 prices cakewalk download parallels desktop price buy microsoft windows 7 home premium cheap adobe after effects for mac how to purchase windows 7 online download microsoft streets and trips 2008 microsoft office 2003 cheapest buy windows 7 online singapore purchase autodesk 3ds max 2009 buy windows xp pro best price adobe flash cs3 buy uniblue registrybooster 2009 cheap windows 7 ultimate retail buy autodesk autocad buy microsoft office document imaging purchase microsoft office 2003 buy windows xp media centre edition discount microsoft windows 7 ultimate windows 7 price upgrade cheap adobe photoshop software burnaware professional price buy acronis true image 2010 windows 7 price change buy acrobat 7 where to buy adobe flash cs3 quicken 2010 price buy windows xp corporate buy office 2007 student product key windows 7 price hong kong buy windows 7 64 buy acrobat professional 9 symantec winfax pro 10.4 buy cheap windows 7 keys adobe photoshop cs4 buy buy microsoft visio 2007 turbotax deluxe 2009 download windows xp price in rupees buy lightroom software buy windows 7 buy windows 7 serial key steinberg nuendo 3 download download autodesk 3ds max 9 buy ms project online windows 7 price egypt windows 2008 enterprise download parallels mac download adobe lightroom sale cheap cubase 5 where to buy windows xp service pack 2 purchase windows vista download cheap windows 7 in singapore lowest price microsoft office purchase windows 7 license key where can i buy windows 7 starter edition buy i.r.i.s. readiris pro 11 cheap windows xp operating system buy office 2003 key where to buy windows xp cheap autocad mechanical 2009 download buy matlab software buy 3ds max 2010 student download dragon naturally speaking autocad inventor download get autocad 2010 buy adobe illustrator cs2 buy windows 7 mac windows xp price canada steinberg cubase sx3 download cheap windows 2003 enterprise download adobe illustrator cs3 can i buy windows xp online windows xp price in malaysia buy adobe cs4 suite best price adobe illustrator cs3 download font folio 11 acdsee pro license key cs3 production premium mac ms streets and trips 2008 download download adobe illustrator mac windows 7 price in india where can i buy cubase sx3 windows vista download iso roxio creator 2010 coupon code microsoft office price student windows 7 home premium 64 bit oem buy microsoft frontpage software buy windows xp 64 pro cheap windows 7 home premium full version buy burnaware professional cheap microsoft works 9 microsoft visio download adobe cs4 web premium oem download disk director cheap adobe creative suite cs4 microsoft money home and business download buy parallels desktop 5 for mac cheapest norton 360 3.0 lightroom price buy windows vista pro microsoft money buy online buy microsoft expression blend 3 buy windows 7 full version for students purchase lightroom 2 buy microsoft office ultimate pdf converter professional 6 promotional code buy adobe cs3 design premium buy indesign cs3 software buy windows vista ultimate upgrade acdsee photo editor download microsoft office 2003 professional cheap windows 7 for college students illustrator cs3 download download adobe indesign cs4 cyberlink powerdirector prices buy windows vista ultimate license buy access 2003 online windows 7 price guide mathcad price buy after effects cs4 mac cheap fl studio 8 xxl cheap photoshop elements 8 buy windows vista home premium oem buy wordperfect x3 buy windows 7 home premium 64 bit buy windows 7 brisbane buy windows 7 bangkok autodesk mudbox price purchase vista license cheap adobe premiere elements 8 download adobe creative suite 4 design premium best price microsoft excel 2003 buy microsoft project 2000 microsoft expression price discount photoshop elements 8 cheap maya software 3ds max sale buy windows 7 ebay windows 7 price 2010 deskshare videoeditmagic 4.3 price buy windows xp 2010 norton 360 price comparison buy windows xp used adobe contribute mac purchase windows 7 for mac buy windows vista home premium product key buy windows 2003 datacenter buy windows 7 professional cheap microsoft windows 7 ultimate buy sony vegas pro 8 purchase access 2007 final cut express 4 cost download microsoft visio 2007 buy onenote 2003 photoshop lightroom for mac symantec pcanywhere download windows 7 price pakistan buy premiere pro cs3 cheap windows 7 for students in australia web page maker 3 download buy windows xp home sp3 adobe photoshop best buy cheap windows 2008 enterprise buy windows 7 direct download download microsoft mappoint 2009 buy windows xp home download microsoft autoroute 2007 europe ashampoo burning studio download buy windows xp calgary archicad 13 download adobe cs4 design premium trial price photoshop elements 8 windows 7 home premium cheapest autodesk lustre pricing purchase archicad 13 acdsee pro 2 download windows 7 discount canada vmware workstation 7 download discount autodesk 3ds max 2009 buy autodesk inventor professional buy windows xp home full version buy windows 7 language pack buy microsoft office 07 buy microsoft excel 2007 download propellerhead reason 4 mappoint 2009 european buy access 2007 only buy frontpage 2003 purchase windows 7 ultimate online purchase microsoft windows vista buy dragon naturally speaking 10 oil price vista gadget pinnacle studio 14 best price cheap corel video studio pro x2 buy photoshop 7 for mac buy adobe cs3 design premium mac download ashampoo burning studio 9 cheap microsoft project 2007 dvd ripper platinum 5 buy adobe premiere pro cs4 download best price microsoft windows vista windows 7 discount for high school students purchase windows 7 professional (64 bit) compare price photoshop buy windows 7 philippines buy microsoft office powerpoint 2007 purchase windows xp sp3 buy photoshop elements 8 mcafee total protection 2009 download best buy autocad 2009 buy windows vista singapore purchase windows 7 now buy windows xp mumbai buy windows 7 ultimate 64 bit oem windows 7 price kenya ms works download adobe acrobat 9 oem buy windows 7 downloadable adobe cs4 web premium trial cheap windows 7 professional best price windows 7 home premium oem download adobe photoshop elements 6 navisworks manage price windows 7 price comparison buy guitar pro 5.2 download acronis true image 11 buy nero 8 ultra edition acdsee price download adobe robohelp 8 buy powerdirector 7 ultra imsi turbofloorplan home and landscape pro 12 buy buy microsoft expression studio 2 windows 7 discount for military buy windows 7 30 download pinnacle studio 12 ultimate adobe acrobat price comparison mappoint 2009 europe buy cheap avanquest mylogomaker professional 2 buy windows xp cheap microsoft office discount code buy windows 7 switzerland buy windows xp volume license buy norton 360 download cheap adobe photoshop cs3 download microsoft expression studio 3 buy office 2010 professional fl studio 8 xxl download buy microsoft office 2007 home and student cheap adobe cs4 design premium abbyy finereader 9.0 express edition where to buy streets and trips 2010 office 2007 price student buy roxio creator 2010 cheap adobe cs4 master collection mac buy windows xp ebay cheap indesign software buy corel photoimpact x3 download avanquest mylogomaker professional 2 cheap ashampoo burning studio 8 adobe captivate pricing autocad architecture oem buy microsoft office pro 2007 where can i buy powerpoint 2003 turbo tax cost buy windows 7 india online purchase windows 7 license online buy adobe flash cs4 buy windows vista ultimate product key buy cs4 mac buy adobe indesign cs4 best buy microsoft windows 7 ultimate cheap adobe dreamweaver cs4 mac cheap windows 7 canada roxio creator 2009 special edition microsoft office 2010 price in india windows 7 price canadian buy windows xp embedded download corel draw 12 adobe creative suite 4 production premium download buy adobe photoshop cs3 extended buy windows vista business 64 bit buy ms office 2003 pro best buy quicken 2010 buy microsoft office for mac cheap buy adobe after effects cs4 cheap norton 360 3.0 best price adobe cs4 master collection windows 7 price for teachers buy windows xp student purchase adobe acrobat 8 professional buy sql server guitar pro 5 mac download
Home » Code, Data, Power User, Visio 2007

Trilogy-meter Data Graphics Part III

Submitted by Visio Guy on February 25, 2009 – 2:00 pm | | 3512 views 5 Comments

trilogy-meter-part-iii

Today, we continue with the movie-trilogy theme, but concentrate on advanced tips for importing and working with multiple shapes automatically.

This is the third and final piece in our trilogy of articles on Visio data graphics and data-linking.

If you haven’t read Part I and Part II of this three-part series on Visio data graphics and data linking, I urge you to check them out now. Without this background information, today’s article won’t make a lot of sense. You can find them here:

Trilogy-meter Data Graphics Part I

Trilogy-meter Data Graphics Part II

The Goal: Many Data-linked Shapes With Attached Info-graphics

What we want to do today is this:

  • Import 21 images related to movie trilogies
  • Link each image to a corresponding Excel ratings data record
  • Display this data on each shape using a data graphic
  • Use as many automatic features as possible

When we’re all done, we’ll have 21 trilogy shapes, each showing ratings for part 1, part 2 and part 3 of the series.

They will look something like this:

three-trilogy-data-graphic-examples

The first two articles in this series have already discussed the details of adding shape data fields to shapes, linking real data to shapes, and creating data graphics to show off the data. Today we’ll focus on handling large numbers of shapes and data records.

Get the Images

Before we get too far down the road, we’ll need some images to work with. I simply went to Google, searched on each of the 21 trilogies listed in the Excel spreadsheet, and saved a thumbnail that I liked to my hard drive.

If you’re extremely curious, you can see them here:

trilogy-images

click image for larger view

If you’re extremely lazy efficient, you can get them all zipped-up here: Download: trilogy-images (76 KB)

And if you don’t have the Excel file from the first two articles of this series, you can get it here: Download: trilogy-meter-data.zip (5 KB)

Batch Importing

After manually downloading twenty-one thumbnails from the web (phew!), I wanted to import them into Visio.

The trouble is, Insert > Picture > From File only allows you to select one blasted picture at a time! Since I’m a (half) developer, I just don’t have the patience for this, when there are ways to automate such drudgery.

List File Names Using a Batch File

So the first thing I did was to automatically create a list of all the filenames for each image that was downloaded. I did this by creating a batch file called: dump names.bat. This file has only three lines in it, two of which you don’t need, but are there so that you can see what happened while the file is working.

dir *.jpg /b
dir *.jpg > movies.txt /b
pause

The first and last lines are just for show when you double-click on the .bat file. The middle line sends each .jpg file to a text file, without all of the path, and date and extra info:

command-image-list

After running this, you’ll have a shiny-new text file called movies.txt, with the contents of your image directory:

notepad-movies-list

You can easily copy and paste this list, into, oh I don’t know, say a bit of VBA code that just might import all of the images into Visio for you!

Import Images Using VBA Code

And here’s that bit of VBA code. You can see where I’ve pasted the file names at the beginning, then wrapped line-continuations and quote marks around each item so that I could quickly process them in an array.

Option Explicit On

Sub ImportImages()

  '// Imports a bunch of images as specified in
  '// the arr variable, and adds the 'Trilogy'
  '// shape data field to each image.
  '// Note: this code must be in a Visio document's
  '// VBA project, and the document must be saved
  '// in the same directory as the image files.
  '// List of files to import (from batch file):

  Dim arr As Object
  arr = Array( _
              "alien.jpg", _
              "back-to-the-future.jpg", _
              "batman.jpg", _
              "blade.jpg", _
              "die-hard.jpg", _
              "godfather.jpg", _
              "indiana-jones.jpg", _
              "jaws.jpg", _
              "jurassic-park.jpg", _
              "lord-of-the-rings.jpg", _
              "mad-max.jpg", _
              "matrix.jpg", _
              "planet-of-the-apes.jpg", _
              "rambo.jpg", _
              "rocky.jpg", _
              "spiderman.jpg", _
              "star-trek.jpg", _
              "star-wars.jpg", _
              "superman.jpg", _
              "terminator.jpg", _
              "x-men.jpg")

  '// Get a page object:
  Dim pg As Visio.Page
  pg = Visio.ActivePage

  Dim shp As Visio.Shape
  Dim iRow As Integer
  Dim imageFileName As String
  Dim niceName As String

  '// ShapeSheet section and row constants for Shape
  '// Data/Custom Propertes section. Makes code easier
  '// to read:
  Const SEC_PROP% = Visio.VisSectionIndices.visSectionProp
  Const ROW_PROP_LABEL% = Visio.VisCellIndices.visCustPropsLabel
  Const ROW_PROP_VALUE% = Visio.VisCellIndices.visCustPropsValue
  Const ROW_LAST% = Visio.VisRowIndices.visRowLast
  Const ROW_TAG_DEFAULT% = Visio.VisRowTags.visTagDefault

  Dim i As Integer
  For i = LBound(arr) To UBound(arr)

    '// Get the file name from the array:
    imageFileName = arr(i)

    '// Build a user-friendly name from the file
    '// name by getting rid of the file-extension,
    '// replacing dashes with spaces and capitalizing
    '// the first letter of each word.
    '//
    '// So, for example: "back-to-the-future.jpg"
    '// becomes "Back To The Future". Not perfect,
    '// but nicer than before:
    niceName = Replace(imageFileName, "-", " ")
    niceName = Replace(niceName, ".jpg", "")
    niceName = VBA.StrConv(niceName, vbProperCase)
    '// Import an image:
    shp = pg.Import(ThisDocument.Path & imageFileName)

    '// Group the shape, because data graphics don't
    '// work on images for some reason:
    shp = shp.Group

    '// Add just the 'Trilogy' Shape Data field:
    iRow = shp.AddRow(SEC_PROP, ROW_LAST, ROW_TAG_DEFAULT)

    '// Set the label for the shape data field:
    shp.CellsSRC(SEC_PROP, iRow, ROW_PROP_LABEL).Formula = _
    Chr(34) & "Trilogy" & Chr(34)
    '// Set the value for the shape data field,
    '// using the nice name:
    shp.CellsSRC(SEC_PROP, iRow, ROW_PROP_VALUE).Formula = _
    Chr(34) & niceName & Chr(34)
  Next i
End Sub

If you use the batch file to create the list, then massage the list into the code above, you should be able to import a whole bunch of  images lickity-split into Visio, and they will be ready to link to data and display data graphics, right away!

Or Import by Hand

If you don’t like reading code, that’s OK. What this code does can be summarized in a few bullet points:

  1. Defines the list of image file names as an array so that each item can be programmatically processed
  2. Imports each image into Visio as a shape
  3. Groups each shape (because data graphics don’t seem to work on images)
  4. Adds the shape data field “Trilogy” to the grouped shape
  5. Sets the Label for the shape data field to “Trilogy”
  6. Sets the Value for the shape data field to a prettied-up version of the file name.

If you don’t know much about using VBA code, and your head is spinning from the listing above, you might want to check out a few articles that will help to get you acquainted. First, have a look at John Goldsmith’s excellent “getting started” article: Just For Starters. Once you’re comfortable dabbling with VBA code and Visio, be sure to ease your peace of mind by educating yourself about VBA and security in: VBA Macro Security.

If you don’t want to mess with the VBA code at all, then this is what you’ll have to do to keep up:

  1. Import each image into Visio
    You can do this using Insert > Picture > From File, or by clicking the Insert Picture button on the Picture Toolbar
    insert-picture-toolbar-button
  2. Select each image by itself, and group it by typing Ctrl+G or Shape > Grouping > Group
    Visio’s data graphics don’t seem to work with images, so we make a group to fool Visio
  3. Add the Shape Data field “Trilogy” to all 21 shapes
    Just select all 21 shapes, then go to Data > Shape Data
  4. Type the name of the trilogy into each Trilogy shape data field
    These names should match those in the Excel file

Why Only One Shape Data Field?

You may have noticed that this time around, we’ve only added the “Trilogy” shape data field, but left the ratings fields out. We didn’t add Part 1, Part 2 and Part 3 like we did in the previous articles.

This is because Trilogy acts as a unique key into our database (the Excel file), and Visio has a way to help us link many shapes at once.

A Few Last Preparations

Before we’re ready to link, there’s just a few more things we need to do.

Go Get the Existing Data Graphic

We’ve now got 21 shapes just itching to be linked to data, but before we do that, we’ll want to go get the data graphic we built in Trilogy-meter Data Graphics Part II so we can apply it to our shape.

The easiest way to get a data graphic from one document into another is to simply drag a shape to which the data graphic is applied into the new document. Visio will bring along the data graphic for the ride.

Open the Excel File

If you’ve not done it already, we need to open the Excel file in Visio. You do this via Data > Link Data to Shapes, as discussed in the other articles.

When you’ve imported the data, you are ready to go. You should have an environment that looks something like this: 21 image shapes, grouped and populated with the Trilogy shape data field. Our custom multi-bar chart data graphic ready and waiting on the right, and our imported Excel data in the External Data pane at the bottom:

before-linking

click for larger image

Time to Link!

Finally we get to do the auto-linking. So go down to the External Data pane, right click and choose: Select All to highlight all of the records.

Select Data and Go!

Once all of the records are selected, you can right-click again and choose: Automatic Link…

linking

Choose Auto-linking Options

Visio will then present you with three dialogs to help in the auto-linking.

In our case, Visio guesses right each time. We want to link to all shapes on the current page:

auto-link-1

We want to link data records where the Trilogy data column is equal to the shape’s Trilogy shape data field:

auto-link-2

Visio summarizes the decisions that have been made, and leaves you to click the Finish button complete the auto-linking:

auto-link-3

Tweak the Results

Voila! We can see here that the shapes have all been populated with data. We’ve selected the Star Wars shape, and can see in the Shape Data window that it indeed has three new data fields for Part 1, 2 and 3, along with the corresponding numerical data.

after-link

click for larger image

But there is a slight problem. Visio applied a data graphic to each shape, but it applied the wrong one. This is because the check-box at the bottom of the Data Graphics pane on the right is checked. The one that says: “Apply after linking data to shapes.”

One way to keep Visio from applying the wrong data graphic is to delete all of the other “default” data graphics from the Data Graphics pane. Another way is to simply uncheck that box.

Visio seems to try to use the last-applied data graphic as the default, but this would require you to first apply the data graphic to one shape in order to set a default. This seems kind of silly to me. I haven’t found any other way to specify which data graphic should be used in conjunction with our check-box, but if I do, I’ll be sure to let you know.

At any rate, it only takes a second to change the data graphic to our bar chart. Just select all of the image-shapes by drawing a selection net around them or pressing Ctrl+A, then click on our bar-chart data graphic. Presto, you’ve done it!

apply-bar-data-graphic

click for larger image

Related Posts

  1. Trilogy-meter Data Graphics Part II
  2. Trilogy-meter Data Graphics Part I
  3. Data Graphics Resources
  4. Ape to Man Data Graphic
  5. 12 Month Data Graphic Shape (almost)

5 Comments »

  • Fabien says:

    Thanks for all the tips!

  • Chris,

    Two suggestions:

    1) As long as you have to select all shapes and then apply the correct data graphic after doing the data linking — why not do it first and eliminate the application of the wrong one? To do so: a) Select All, b) apply the data graphic (nothing interesting will happen yet because the shapes don’t have data, but hang on for a moment), c) do the autolinking. The result: the data graphics will appear as soon as data linking is finished.

    2) Even better, add two lines of code to your VBA routine to preselect the data graphic:
    ActiveWindow.SelectAll
    ActiveWindow.Selection.DataGraphic = _
    ActiveDocument.Masters(“TrilogyDataGraphic”)
    Then everything will be right when you do the autolinking. (NOTE: replace “TrilogyDataGraphic” with the actual name of your data graphic master.)

    Of course, with another 6-8 lines of code you can even do the data linking automatically… maybe that should be the long anticipated fourth part of the trilogy!?!?

  • Visio Guy says:

    See Scott, the only way to get any comments is to write something stupid (hahahaha)

    I had a feeling that there was a smarter way to do this, but I was packing for Seattle while writing these three articles…

    But it seems to me that applying the data graphics before the auto linking will cause a problem, because the shapes don’t have all of the fields yet. The auto-linking actually adds the fields, so that they can then accept the data graphic properly. I’ll have to re-check this.

    Thanks for the tips, and anybody who submits code automatically gets an extra FIVE POINTS!

  • Good point about the fields not being there yet, hadn’t thought about that… easy enough to add them in a “for each shp” loop…

    Have a safe trip home (I arrived back in Boston a couple of hours ago).

    Scott

  • [...] Trilogy-meter Data Graphics Part III [...]

Leave a comment!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.