Search code examples
vbavisioshapesheet

How to copy a Visio shapesheet section between shapes in VBA


Is there a method available for copying a section out of a shape to another shape using VBA? I'm specifically trying to copy all the custom properties and user cells from one pagesheet to another page.


Solution

  • Unfortunately there isn't a simple method to do this. You will have to loop over all the rows in the source sheet and create the same rows in the destination sheet. E.g.:

    Dim oPageSheet1 As Visio.Shape
    Dim oPageSheet2 As Visio.Shape
    Dim rowName     As String
    Dim i   As Integer
    
    Set oPageSheet1 = Visio.ActiveDocument.Pages.Item(1).PageSheet
    Set oPageSheet2 = Visio.ActiveDocument.Pages.Item(2).PageSheet
    
    i = visRowUser
    
    While oPageSheet1.CellsSRCExists(visSectionUser, i, visUserValue, False)
        oPageSheet2.AddNamedRow visSectionUser, oPageSheet1.Section(visSectionUser).Row(i).NameU, 0
        oPageSheet2.Section(visSectionUser).Row(i).Name = oPageSheet1.Section(visSectionUser).Row(i).Name
        oPageSheet2.CellsSRC(visSectionUser, i, visUserValue).FormulaU = oPageSheet1.CellsSRC(visSectionUser, i, visUserValue).FormulaU
        oPageSheet2.CellsSRC(visSectionUser, i, visUserPrompt).FormulaU = oPageSheet1.CellsSRC(visSectionUser, i, visUserPrompt).FormulaU
        i = i + 1
    Wend
    

    If you have to copy a large number of rows and performance is a consideration you should investigate using AddRows and SetFormulas.