'About files (.vsdx) created by Microsoft visio

I'm investigating how to automatically update a visio file created with one mastershape (v1.0.vssx) to the next version of the mastershape (v1.1.vssx). When updating each master shape, use Master.Name as the key.

With the code below, I was able to open the vsdx file and vssx and open their respective Masters.

vssx_Master = vssxMaster vsdx_shape.master = vssx_Master

I wondered if I could update the master shape with the code, but unfortunately vssxMaster is the same as vssxMaster.Name and its type is String. Is there a way to replace the Master of one shape with another?

not work...

Sub Visio_Update(ByRef VISIOpath As String, ByRef except_sheets() As String, ByRef VSSXpath As String)
        
    
    Application.DisplayAlerts = False
    Application.ScreenUpdating = False
    
    Dim vsoApp As Visio.Application
    Dim vsoDoc As Visio.Document
    Dim vsoPage As Visio.Page
    Dim vsoItemsCnt As Long
    Dim vsoShape As Visio.Shape
    Dim FileName As String
    Dim FileText As String
           
    FileName = Dir(VISIOpath)
    FileName = Replace(FileName, ".vsdx", "")
    
    ChDir ThisWorkbook.path
    
    Set vsoApp = CreateObject("Visio.Application")
    Call vsoApp.Documents.OpenEx(VISIOpath, visOpenRW)
    Set vsoDoc = vsoApp.Documents.Item(1)
    vsoItemsCnt = vsoApp.Documents.Count
    
    Call vsoApp.Documents.OpenEx(VSSXpath, visOpenRW)
    Set vssxDoc = vsoApp.Documents.Item(vsoItemsCnt + 1)
    Set vssxMasters = vssxDoc.Masters
    
    For Each vsoPage In vsoDoc.Pages
        For Each vsoShape In vsoPage.Shapes
            If Not (vsoShape.Master Is Nothing) Then
                On Error Resume Next
                
                mastername = vsoShape.Master.Name
                vsoShape.ReplaceShape vssxMasters.Item(vsoShape.Master.Name)
                        
                If Err.Number = 0 Then
                    Debug.Print ("Masters.Item")
                    Debug.Print "updated succeeded : ", mastername
                    Err.Clear
                Else
                    Debug.Print ("Masters.Item")
                    Debug.Print Err.Description
                    Err.Clear
                End If
                
            
            End If
        Next
    Next
    
    vsoDoc.SaveAs ThisWorkbook.path & "\data\" & FileName & "_updated_.vsdx"
    Application.ScreenUpdating = True

End Sub


Sub test()
    choosed_path = "C:\Users\11665307\Desktop\data\vs1.vsdx"
    Update_Template = "C:\Users\11665307\Documents\test.vssx"
    
    Call Visio_Update(choosed_path, except_sheets, (Update_Template))

End Sub

I wondered if I could update the master shape with the code



Solution 1:[1]

You dont need iterate all masters into stencil :)

For Each vsoPage In doc.Pages
    For Each vsoShape In vsoPage.Shapes
        If Not (vsoShape.Master Is Nothing) Then
            vsoShape.ReplaceShape vssxMasters.Item(vsoShape.Master.Name)
        End If
    Next
Next

Solution 2:[2]

You must iterate through all the shapes on the page. If the shape was created based on the master from stencil v.1.0, then replace it with the corresponding master v.1.1. using the ReplaceShape method

Sub ttt()
Dim sh As Shape
For Each sh In ActivePage.Shapes
    If sh.Master.NameU = "Circle" Then sh.ReplaceShape Application.Documents.Item("BLOCK_M.vssx").Masters.ItemU("Diamond")
Next
End Sub

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Surrogate
Solution 2