How do I programmatically drop a Milestone onto a Block Timeline in Visio
Asked Answered
U

2

7

I'm trying to programmatically create a timeline and markers using the Visio 2010 COM Interops. My code is based off of Chris Castillo's 2 part blog posting (Part 1, Part 2), which is the only semi-complete example I've been able to find on how to do this. However his blog (from 2004) doesn't seem to work right. The milestones are not really connected to the timeline, and updating their date does not get them to move to the right place.

Any suggestions or fixes?

enter image description here

Imports Microsoft.Office.Interop.Visio
Imports System.Diagnostics.CodeAnalysis
Imports System.Runtime.InteropServices

...

Dim VisioApp As New Application

Dim myDoc As Document = VisioApp.Documents.Add("")
Dim myPage As Page = myDoc.Pages.Item(1)

Dim TimelineStencils As Document =
    VisioApp.Documents.Add("Timeline Shapes.vss")

Dim theTimeline As Shape
Dim theMilestone As Shape

VisioApp.AlertResponse = 1

theTimeline = myPage.Drop(
    TimelineStencils.Masters.ItemU("Block timeline"), 5.610236, 5.511811)

theTimeline.CellsU("User.visBeginDate").FormulaU = _
        VisioApp.ConvertResult(
            "1/1/2004", VisUnitCodes.visDate, VisUnitCodes.visInches)

theTimeline.CellsU("User.visEndDate").FormulaU = _
    VisioApp.ConvertResult(
        "12/31/2004", VisUnitCodes.visDate, VisUnitCodes.visInches)

VisioApp.Addons("ts").Run("/cmd=3")

theMilestone = myPage.Drop( _
    TimelineStencils.Masters.ItemU("Line milestone"), _
    5.610236, 5.511811)

theMilestone.CellsU("User.visMilestoneDate").FormulaU = _
    VisioApp.ConvertResult(
        "7/1/2004", VisUnitCodes.visDate, VisUnitCodes.visInches)

VisioApp.AlertResponse = 0
Uncut answered 25/9, 2012 at 18:17 Comment(0)
U
1

So I found a much better walk-through of programmatically adding visio objects that @JohnGoldsmith linked to in some of his other answers to related questions, and found a better way of creating the stencils and page, and it's working!

Dim VisioApp As New Application

Dim myDoc As Document = VisioApp.Documents.Add("Timeline.vst")
Dim myPage As Page = myDoc.Pages.Item(1)

Dim TimelineStencilName As String = "TIMELN_M.VSS"
Dim TimelineStencilDoc As Document

For Each Doc As Document In VisioApp.Documents
    If Doc.Name = TimelineStencilName Then
        TimelineStencilDoc = Doc
        Exit For
    End If
Next

Dim TimelineMaster As Master =
    TimelineStencilDoc.Masters.ItemU("Block timeline")

Dim MilestoneMaster As Master =
    TimelineStencilDoc.Masters.ItemU("Line milestone")

Dim theTimeline As Shape
Dim theMilestone As Shape

theTimeline = myPage.Drop(TimelineMaster, 5.610236, 5.511811)

theTimeline.CellsU("User.visBeginDate").FormulaU = _
    VisioApp.ConvertResult(
            "1/1/2004", VisUnitCodes.visDate, VisUnitCodes.visInches)

theTimeline.CellsU("User.visEndDate").FormulaU = _
    VisioApp.ConvertResult(
        "12/31/2004", VisUnitCodes.visDate, VisUnitCodes.visInches)

theMilestone = myPage.Drop( _
    MilestoneMaster, _
    5.610236, 5.511811)

theMilestone.CellsU("User.visMilestoneDate").FormulaU = _
    VisioApp.ConvertResult(
        "10/1/2004", VisUnitCodes.visDate, VisUnitCodes.visInches)
Uncut answered 27/9, 2012 at 20:18 Comment(1)
And for anyone who is struggling with all this too, here is the instructions on turning on Developer Mode, which enables ShapeSheets visguy.com/2008/08/04/…Uncut
G
1

Running Chris' code against the built in stencil ("TIMELN_M.VSS") and template on Visio 2010 I found it worked perfectly. If you run it against the built in stencil with a blank document, however, the Description and Date shape data field are not displayed. The visibility of these two fields depend on a User cell ("User.visTLShowProps") in the Document ShapeSheet that either gets added by the addon or is added when one of the masters is dropped.

The standard template contains two persistent events that call functionality in the addon for DocumentCreated and DocumentOpened and, again, either these add a couple of other User cells that are used to toggle the value of the above on and off, or they exist in the template. In any case, these are not added to the Document ShapeSheet if you're not using the original template.

This means that you need to toggle the value yourself. For example (a quick VBA example):

Private Sub SetTlShowProps(ByRef vDoc As Visio.Document)
If (Not vDoc Is Nothing) Then
    Dim docShp As Visio.Shape
    Set docShp = vDoc.DocumentSheet
    If (docShp.CellExistsU("User.visTLShowProps", 0)) Then
        docShp.CellsU("User.visTLShowProps").FormulaU = 1
    End If
End If
End Sub

Or, of course you could add those 'push' cells into you're own document template in the ShapeSheet:

User.visTLShowPropsOn = SETF(GetRef(User.visTLShowProps),1)

User.visTLShowPropsOff = SETF(GetRef(User.visTLShowProps),0)

A final point if this doesn't solve your problem is, are you using a different or derived stencil ("Timeline Shapes.vss")?

Gastrotomy answered 27/9, 2012 at 9:48 Comment(2)
Thanks for your help John. When you ran Chris's code and it worked, was that from a standalone application, or from within Visio in some sort of VBA console? I changed my Timeline stencil to "TIMELN_M.VSS", but am still getting the same results. I was under the impression they were the same.Uncut
Also, is there something I can change Dim myDoc As Document = VisioApp.Documents.Add("") to that would load the timeline document instead of a blank document?Uncut
U
1

So I found a much better walk-through of programmatically adding visio objects that @JohnGoldsmith linked to in some of his other answers to related questions, and found a better way of creating the stencils and page, and it's working!

Dim VisioApp As New Application

Dim myDoc As Document = VisioApp.Documents.Add("Timeline.vst")
Dim myPage As Page = myDoc.Pages.Item(1)

Dim TimelineStencilName As String = "TIMELN_M.VSS"
Dim TimelineStencilDoc As Document

For Each Doc As Document In VisioApp.Documents
    If Doc.Name = TimelineStencilName Then
        TimelineStencilDoc = Doc
        Exit For
    End If
Next

Dim TimelineMaster As Master =
    TimelineStencilDoc.Masters.ItemU("Block timeline")

Dim MilestoneMaster As Master =
    TimelineStencilDoc.Masters.ItemU("Line milestone")

Dim theTimeline As Shape
Dim theMilestone As Shape

theTimeline = myPage.Drop(TimelineMaster, 5.610236, 5.511811)

theTimeline.CellsU("User.visBeginDate").FormulaU = _
    VisioApp.ConvertResult(
            "1/1/2004", VisUnitCodes.visDate, VisUnitCodes.visInches)

theTimeline.CellsU("User.visEndDate").FormulaU = _
    VisioApp.ConvertResult(
        "12/31/2004", VisUnitCodes.visDate, VisUnitCodes.visInches)

theMilestone = myPage.Drop( _
    MilestoneMaster, _
    5.610236, 5.511811)

theMilestone.CellsU("User.visMilestoneDate").FormulaU = _
    VisioApp.ConvertResult(
        "10/1/2004", VisUnitCodes.visDate, VisUnitCodes.visInches)
Uncut answered 27/9, 2012 at 20:18 Comment(1)
And for anyone who is struggling with all this too, here is the instructions on turning on Developer Mode, which enables ShapeSheets visguy.com/2008/08/04/…Uncut

© 2022 - 2024 — McMap. All rights reserved.