Dynamic Visio – XML & VBA (Part 1)

What ever you think Visio is – there’s a lot-more to it than flow-charts and block-diagrams. Thanks to the fact that it is part of Microsoft’s Office Suite – it has VBA (Visual Basic for Applications) built in and that ‘comes with’ the functionality to process XML (Extensible Mark-up Language) files. Simply put – Visio can be used to dynamically create illustrations by reading an input ‘specification’ defined in an XML file (that could be an output of another process).


Consider this simple XML file defining three ‘items’ with positional and dimensional values :

    <name>Item One</name>
    <name>Item Two</name>
    <name>Item Three</name>


The method named : Document_DocumentOpened() will execute automatically when the Visio document (to which it is ‘attached’) is opened. It will open the local XML file named : Items-01.xml, cycle through each ‘Item’ in ‘Items’ calling the method named Create_Shape() passing in the positional and dimensional values of each ‘Item’.

Private Sub Document_DocumentOpened(ByVal doc As IVDocument)
    Dim XDoc As Object
    Set XDoc = CreateObject("MSXML2.DOMDocument")
    XDoc.async = False: XDoc.validateOnParse = False
    XDoc.Load ("C:\Users\Joe\Documents\Work\Items-01.xml")
    Set Items = XDoc.documentElement
    Dim Name As String
    Dim Left As Double
    Dim Bottom As Double
    Dim Width As Double
    Dim Height As Double
    For Each Item In Items.childNodes
      Name = Item.childNodes(0).Text
      Left = Item.childNodes(1).Text
      Bottom = Item.childNodes(2).Text
      Width = Item.childNodes(3).Text
      Height = Item.childNodes(4).Text
      Call Create_Shape(Left, Bottom, Left + Width, Bottom + Height, Name)
    Next Item
    Set XDoc = Nothing
End Sub
Sub Create_Shape(xStart As Double, yStart As Double, xEnd As Double, yEnd As Double, Name As String)

  Dim vsoShape As Visio.Shape

  Set vsoShape = ActivePage.DrawRectangle(xStart, yStart, xEnd, yEnd)
  vsoShape.Text = Name

End Sub

The Visio

When each ‘Item’ is processed – the following Visio page with three rectangles will have been created :


Saving the text of named VB.NET controls to XML

How to save the text of named VB.NET controls to an XML file


Protected Sub SaveXML_2()

  Dim settings As New XmlWriterSettings()
  settings.Indent = True

  Dim XmlWrt As XmlWriter = XmlWriter.Create("C:\SaveXML_002.xml", settings)

  With XmlWrt




  End With
End Sub

XML (output)

  <customercontact>Adam Watson</customercontact>

Reading XML Nodes into correspondingly named VB.NET Textboxes

Question : How do I read XML node-values (from an XML file) into correspondingly named VB.NET Textbox controls ?


    Programming Widgets Inc.
    Ben Franklin
    Lead Programmer


Protected Sub Page_Load() Handles Me.Load

    Dim settings As New XmlReaderSettings()
    settings.ConformanceLevel = ConformanceLevel.Fragment
    settings.IgnoreWhitespace = True
    settings.IgnoreComments = True

    Dim XML_Reader As XmlReader = XmlReader.Create(C:\XML\basic-information.xml, settings)


    While XML_Reader.Read()

      If XML_Reader.NodeType = XmlNodeType.Element Then

        Dim NodeName As String = XML_Reader.Name

        Dim NodeValue As String = XML_Reader.Value

        Dim PageControl As RadTextBox = RadMultiPage1.FindControl(NodeName)

        If (Not PageControl Is Nothing) Then
          PageControl.Text = NodeValue
          ' "Control not found" - error message
        End If

      End If

      End While

End Sub