关于 Microsoft visio 创建的文件 (.vsdx)

About files (.vsdx) created by Microsoft visio

我正在研究如何将使用一个母版 (v1.0.vssx) 创建的 visio 文件自动更新到下一版本的母版 (v1.1.vssx)。更新每个master shape时,使用Master.Name作为key。

使用下面的代码,我能够打开 vsdx 文件和 vssx 并打开它们各自的 Masters。

vssx_Master = vssxMaster vsdx_shape.master = vssx_Master

我想知道我是否可以用代码更新master shape,但不幸的是vssxMaster与vssxMaster.Name相同,它的类型是String。 有没有办法把一种形状的Master替换成另一种形状的Master?

不工作...

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:\Users665307\Desktop\data\vs1.vsdx"
    Update_Template = "C:\Users665307\Documents\test.vssx"
    
    Call Visio_Update(choosed_path, except_sheets, (Update_Template))

End Sub

我想知道我是否可以用代码更新主形状

您必须遍历页面上的所有形状。如果形状是基于 stencil v.1.0 的母版创建的,则将其替换为相应的母版 v.1.1。使用 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

您不需要将所有母版迭代到模板中:)

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