Refreshing Timeline Addon using VBA

Started by IrateWithVSO, January 28, 2022, 05:51:15 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

IrateWithVSO

Hi:
I have found very old posts about this problem, but their advice isn't working for me. I suspect it may have something to do with differences in the recent versions of Visio. I am using Visio version 2108 (Build 14326.20738)

I am writing a VBA macro in Visio to create and modify a timeline from Visio's timeline stencil. I can put the timeline on a page and it shows up immediately, but when I edit the line's tick marks (through VBA), the changes to the timeline do not show up. I need to open the timeline's configure menu and then hit OK (without editing anything) and the changes then appear.

Old posts have suggested using the line "Application.Addons.Item("ts").Run "/cmd=3" to refresh the timeline - essentially forcing the addon to run. Unfortunately, this doesn't work for me. I don't get an error message, but it fails to produce any results.

Does anyone know how to fix this? Any help would be greatly appreciated!

Here is the code:

Sub Test()

Dim startDate, endData as Variant
Dim dateFormat as string
Dim stnObj as Visio.Document
Dim mastObj as Visio.Master
Dim pagsObj as Visio.Pages
Dim pagObj as Visio.Page
Dim winLab as String

'Setting up some visio document variables
Set pagsObj = ThisDocument.Pages
Set pagObj = pagsObj.Item(1)

'Opening Timeline Shape Stencil
Set stnObj = Documents.Add("TIMELN_M.vssx")

'Getting window ID (caption) of Stencil window to close it later
winCount = Application.Windows.Count
winApp = Application.Windows(winCount)
vsoWindowCount = winApp.Windows.Count
winLab = winApp.Windows.Item(vsoWindowCount).Caption

'Get Line timeline visio item
Set TL = stnObj.Masters.ItemU("Line timeline")

'Putting down the above timeline causes a window to open, but I need to suppress that
Application.AlertResponse = 1

'Put the timeline on the page
Set TLobj = pagObj.Drop(TL, 5.5, 7)

'Change start and end dates - these show up immediately on the timeline
startDate = Application.ConvertResult("01/01/2022", VisUnitCodes.visDate, VisUnitCodes.visInches)
endDate = Application.ConvertResult("12/31/2022", VisUnitCodes.visDate, VisUnitCodes.visInches)
TLobj.CellsU("User.visBeginDate").Formula = startDate
TLobj.CellsU("User.visEndDate").Formula = endDate

'Change the date format - this change shows up immediately on the timeline
Set x = TLobj.Cells("User.visBEMask")
x.Formula = Chr(34) & dateFormat & Chr(34)
Set y = TLobj.Cells("User.visIntmMask")
y.Formula = Chr(34) & dateFormat & Chr(34)

'Set timeline's tick markings - this change only shows up if I open the timeline's menu and then hit ok
Set z = TLobj.Cells("User.visTimeScale")
z.Formula = 5

'Stop suppressing dialog boxes
Application.AlertResponse = 0

'******this is the line that seems to not do what I expect
'Force the timeline to up date to the new tick mark scale instead of me having to open the menu and click ok
Application.Addons.Item("ts").Run "/cmd=3"
'******the above line seems to do nothing

'Close the stencil window opened earlier
Application.Windows.ItemEx(winLab).Activate
ActiveWindow.Close

End Sub

wapperdude

By the time the code gets to your problematic statement, the timeline has become unselected.  Your run addon statement requires that it be selected.  Add this statement before execution of that line.

'Re-select/activate timeline shape
    ActiveWindow.Select TLobj, visSelect   'This makes the shape ACTIVE

Visio 2019 Pro

IrateWithVSO

Thanks Wapperdude.

When I added the activate line you suggested, I received an error message telling me it was the wrong Window type. After some thought, I realized ActiveWindow is the stencil window that I opened earlier in the code. So, I moved the stencil window closure to before the ActiveWindow command (right after my Application.AlertResponse = 0 line). The logic here being that when I close the stencil window the ActiveWindow will become the Visio document.

After the change, the codes runs without error, but it does not cause the timeline to refresh. The problem persists.
Any other suggestions you have (or anyone else has) would be greatly appreciated.

Here is what the code looks like now, just to be clear.

Sub Test()

Dim startDate, endData as Variant
Dim dateFormat as string
Dim stnObj as Visio.Document
Dim mastObj as Visio.Master
Dim pagsObj as Visio.Pages
Dim pagObj as Visio.Page
Dim winLab as String

'Setting up some visio document variables
Set pagsObj = ThisDocument.Pages
Set pagObj = pagsObj.Item(1)

'Opening Timeline Shape Stencil
Set stnObj = Documents.Add("TIMELN_M.vssx")

'Getting window ID (caption) of Stencil window to close it later
winCount = Application.Windows.Count
winApp = Application.Windows(winCount)
vsoWindowCount = winApp.Windows.Count
winLab = winApp.Windows.Item(vsoWindowCount).Caption

'Get Line timeline visio item
Set TL = stnObj.Masters.ItemU("Line timeline")

'Putting down the above timeline causes a window to open, but I need to suppress that
Application.AlertResponse = 1

'Put the timeline on the page
Set TLobj = pagObj.Drop(TL, 5.5, 7)

'Change start and end dates - these show up immediately on the timeline
startDate = Application.ConvertResult("01/01/2022", VisUnitCodes.visDate, VisUnitCodes.visInches)
endDate = Application.ConvertResult("12/31/2022", VisUnitCodes.visDate, VisUnitCodes.visInches)
TLobj.CellsU("User.visBeginDate").Formula = startDate
TLobj.CellsU("User.visEndDate").Formula = endDate

'Change the date format - this change shows up immediately on the timeline
Set x = TLobj.Cells("User.visBEMask")
x.Formula = Chr(34) & dateFormat & Chr(34)
Set y = TLobj.Cells("User.visIntmMask")
y.Formula = Chr(34) & dateFormat & Chr(34)

'Set timeline's tick markings - this change only shows up if I open the timeline's menu and then hit ok
Set z = TLobj.Cells("User.visTimeScale")
z.Formula = 5

'Stop suppressing dialog boxes
Application.AlertResponse = 0

'Close the stencil window opened earlier
Application.Windows.ItemEx(winLab).Activate
ActiveWindow.Close

ActiveWindow.Select TLobj, visSelect
'******this is the line that seems to not do what I expect
'Force the timeline to up date to the new tick mark scale instead of me having to open the menu and click ok
Application.Addons.Item("ts").Run "/cmd=3"
'******the above line seems to do nothing

End Sub

Paul Herber

To close the stencil you should be able to do a

stnObj.Close

instead of keeping track of its window.
Electronic and Electrical engineering, business and software stencils for Visio -

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

IrateWithVSO

Oh man! You're right, Paul.
That is a way better/easy way to handle the stencil. Thanks!

Now, if I can just get the timeline to refresh...

wapperdude

That should have worked.  It did for me.  Here's the code I ran, basically, an edited version of yours.  Some variable names changed.  Didn't go to effort (bad doggy!) to declare the variables.


Sub Macro1()

    Windows.ItemEx("Drawing1").Activate
    ActiveWindow.Page.Drop Application.Documents.Item("TIMELN_U.vssx").Masters.ItemU("Line timeline"), 5.25, 6#
    Set vTLshp = ActiveWindow.Selection(1)
   
'Change start and end dates - these show up immediately on the timeline
    startDate = Application.ConvertResult("01/01/2022", VisUnitCodes.visDate, VisUnitCodes.visInches)
    endDate = Application.ConvertResult("12/31/2022", VisUnitCodes.visDate, VisUnitCodes.visInches)
    vTLshp.CellsU("User.visEndDate").Formula = endDate
    vTLshp.CellsU("User.visBeginDate").Formula = startDate
   
'Set timeline's tick markings - this change only shows up if I open the timeline's menu and then hit ok
    Set Z = vTLshp.Cells("User.visTimeScale")
    Z.Formula = 5
   
'Re-select/activate timeline shape
    ActiveWindow.Select vTLshp, visSelect   'This makes the shape ACTIVE

'Putting down the above timeline causes a window to open, but I need to suppress that
    Application.AlertResponse = 1

'******this is the line that seems to not do what I expect
'Force the timeline to up date to the new tick mark scale instead of me having to open the menu and click ok
    Application.Addons.Item("ts").Run "/cmd=3"
'******the above line seems to do nothing
'Stop suppressing dialog boxes
    Application.AlertResponse = 0

End Sub
Visio 2019 Pro

wapperdude

My apologies re the code.  I had changed the start so I didn't have to keep deleting / redropping the shape.  Once that is re-established, then the pop-up window suppression must be moved back to the top, where you originally had it.  So, here's corrected version, with Stencil window close at the end. 


Sub Macro1()

    Windows.ItemEx("Drawing2").Activate
   
'Putting down the timeline causes a pop-up window to open, but I need to suppress that
    Application.AlertResponse = 1
   
    ActiveWindow.Page.Drop Application.Documents.Item("TIMELN_U.vssx").Masters.ItemU("Line timeline"), 5.25, 6#
    Set vTLshp = ActiveWindow.Selection(1)
   
'Change start and end dates - these show up immediately on the timeline
    startDate = Application.ConvertResult("01/01/2022", VisUnitCodes.visDate, VisUnitCodes.visInches)
    endDate = Application.ConvertResult("12/31/2022", VisUnitCodes.visDate, VisUnitCodes.visInches)
    vTLshp.CellsU("User.visEndDate").Formula = endDate
    vTLshp.CellsU("User.visBeginDate").Formula = startDate

'NOTE:  I deleted some lines of code as they weren't pertinent to the investigation.
   
'Set timeline's tick markings - this change only shows up if I open the timeline's menu and then hit ok
    Set Z = vTLshp.Cells("User.visTimeScale")
    Z.Formula = 6  'My default is months, i.e., "5", so used "6" to observe change.  Went from months to quarters display
   
'Re-select/activate timeline shape
    ActiveWindow.Select vTLshp, visSelect   'This makes the shape ACTIVE

'******this is the line that seems to not do what I expect
'Force the timeline to up date to the new tick mark scale instead of me having to open the menu and click ok
Application.Addons.Item("ts").Run "/cmd=3"
'******the above line seems to do nothing

'Stop suppressing dialog boxes
    Application.AlertResponse = 0

.Close the Stencil Window
    Application.Documents.Item("TIMELN_U.vssx").Close

End Sub
Visio 2019 Pro

IrateWithVSO

This is really helpful!

I went through both versions of your code and noticed a couple of differences compared to my code.

Firstly, I liked the way you dropped the timeline in a single line, without opening the stencil. Looking more closely at the line, I noticed you are using "TIMELN_U.vssx". I am using "TIMELN_M.vssx". I'm not sure what the difference is, but it immediately looks different when dropped on the page object. By looks different, I mean it's not just a line with a date at each end - it already has labelled tick marks.

Secondly, and this is most important, your code worked. "ActiveWindow.Select vTLshp, visSelect" followed by "Application.Addons.Item("ts").Run "/cmd=3"" worked as advertised. My problem is solved!

Thank you so much for this.

As you have probably guessed, I'm not very familiar with VBA for Visio. It's a powerful tool, but not very intuitive. There don't seem to be any good resources on-line (aside from this forum). Are there any resources you can recommend (printed or on-line)?

Thanks again!


wapperdude

#8
The "-U" and "-M" refer to the units base: "U" = US units and "M" = metric units.  Just now that applies to a timeline other than fitting it on a page and presumably scaling the length to get "time" for a particular drawing, I'm not sure.  I would think visibly, they both ought to look the same.  Apparently not.

Your coding was pretty good, I thought.  You were thorough enough to include the "Dim" statements and healthy dose of comments.  Good job.

VBA references...yeah, there's not much with regards to Visio.  Check out this forum posting.  Has multiple links to resources.  http://visguy.com/vgforum/index.php?topic=9444.msg42096#msg42096


While, reasonably close, texts for Excel can be helpful.  Keep in mind, they have different object models, so there are unique commands for each, Visio and Excel. 

Visio 2019 Pro

IrateWithVSO

Ah, metric vs. US units...of course.

Thanks for the link!