Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Export data from Visio Shapes using VBA

I want to model something similar to a (hyper-)graph in MS Visio 2016 Professional and then export the data of the shapes to csv to further work with it.

I am trying to make a VBA Script that goes through all the shapes on the sheet and writes the (manually inserted) data from the shapes to one csv file (and in the future maybe different csv files depending on the type of the shape).

To get a feeling for VBA I tried to start with a script that counts all the shapes on the sheet but I already failed on that. Please consider this is my first time working with VBA:

Sub countShapes()
Dim shp As Shape
Dim count As Integer
count = 0
Debug.Print count

For Each shp In ActiveSheet.Shapes
count = count + 1
Debug.Print count
Next

End Sub

This returns runtime error 424, object not found.

What am I missing?

As a second step, I want the script to check that shapes that have for example the same number in the data field "id" are identical in all other data fields as well and show an error if not (before exporting to the csv files). Can I realize this using vba in visio?

Thanks a lot for any help!

like image 951
ksbawpn Avatar asked Aug 06 '26 21:08

ksbawpn


1 Answers

ActiveSheet is an Excel property. I think you're looking for ActivePage, which is a Visio equivilent. So to fix your code above you could use this:

For Each shp In ActivePage.Shapes
    count = count + 1
    Debug.Print count
Next

However, if you're simply after the shape count for a page then you could write this instead:

Debug.Print ActivePage.Shapes.Count

Can I recommend some links that might also help:

  • https://visualsignals.co.uk/blogs/vislog/2007/10/just-for-starters/
  • https://visualsignals.co.uk/blogs/vislog/2007/11/looping-through/
  • vba programming for visio

As an alternative approach you might also be interested in Visio's built-in reporting tool:

  • Create a report of shape data (support docs)
  • Getting Started with Visio 16 - Build and Apply Reports from Share Data (Video)

Re the second part of your question (check data fields) I'm assuming you're talking about reading Shape Data. If that's the case you first want to check if a row named "ID" exists and, if it does, read that value. So something like this might get you going:

Public Sub TestGetCellValues()
    GetShapesCellValues ActivePage, "Prop.ID"
End Sub

Public Sub GetShapesCellValues(targetPage As Visio.Page, targetCellName As String)
Dim shp As Visio.Shape
If Not targetPage Is Nothing Then
    For Each shp In targetPage.Shapes
        If shp.CellExistsU(targetCellName, 0) = True Then
            Debug.Print shp.NameID & "!" _
                & targetCellName & " = " _
                & shp.CellsU(targetCellName).ResultIU
        End If
    Next shp
End If
End Sub

...which might output something like this (given the associated shapes):

Sheet.2!Prop.ID = 3
like image 127
JohnGoldsmith Avatar answered Aug 09 '26 20:08

JohnGoldsmith



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!