Get VBA Code to reproduce a given 2D shape programmatically

Started by doudou, September 19, 2014, 02:23:47 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

doudou

Hello,
I have a Visio diagram that was generated by a colleague, and I am wondering what would be the easiest way to generate VBA code to programmatically reproduce it. I am thinking of writing a VBA code that loops through all the shapes on the active page and export to excel. I am mostly interested in 2D shapes,  so that is why I skip over connectors. I think this approach may work, however I am not sure which shape properties are needed to reproduce a shape using VBA. I am thinking shape size, shape fill, shape outline, shape text. Any help would be great. Thanks!

Sub GetShapeData()
    Dim xlApp As Excel.Application
    Set xlApp = CreateObject("Excel.Application")
    Dim wb As Excel.Workbook
    xlApp.Visible = True
    Set wb = xlApp.Workbooks.Add
    Dim ws As Excel.Worksheet
    Set ws = wb.Worksheets(1)
    Dim i As Long: i = 1
    Dim shp As Visio.Shape
    For Each shp In ActivePage.Shapes
        If Not shp.OneD Then
            'ws.Cells(i, 1) = shp.Properties
            i = i + 1
       End If
    Next
End Sub

Jumpy

You'll need position, size, the complete geoemtry section, ....

However: Your approach will only work for simple shapes. Smartshapes or grouped shapes are to complex for this.

Paul Herber

There will be far too many properties that you'll need to copy to be able to do this.
What exactly are you trying to do?
Why not save the diagram in XML format and then programmatically load that XML into a parser that understands Visio XML, i.e. Visio itself? The XML is an exact representation of the diagram.

Electronic and Electrical engineering, business and software stencils for Visio -

https://www.paulherber.co.uk/

doudou

Quote from: Paul Herber on September 19, 2014, 02:38:24 PM
There will be far too many properties that you'll need to copy to be able to do this.
What exactly are you trying to do?
Why not save the diagram in XML format and then programmatically load that XML into a parser that understands Visio XML, i.e. Visio itself? The XML is an exact representation of the diagram.

Hi Paul,

Thanks for your reply. It is probably best like you have suggested if I give you a little more information on what I am trying to do.

I have supply chain data in an Excel spreadsheet. Each row on the Excel spreadsheet represents a trade lane. What I am trying to do is write a customized VBA solution that will generate on demand Visio diagrams for supply chain for selected product. Besides the shape box containing the name of the product,  all the top shapes will be the same, so I was wondering what would be the best way to programmatically recreate those shapes on each diagram.

I should perhaps mention, that although I have a fairly extensive knowledge of VBA and Excel, I had never used Visio before until a few ago. Thanks again

Yacine

Salut Jean,
from what I read, you don't need to draw everything with VBA.
Best would be to create masters from your colleagues drawing, then use these masters for your own pruposes.
(The masters being saved in a stencil of course).
So as to let VBA do only the imporant automation stuff (getting data, adjusting geometries, finding the right placement for the shapes, ...).
Yacine