通过 Python 或 VBA 脚本在与设计文件相同的窗口中打开 Visio 形状

问题描述 投票:0回答:1

我希望有人能够建议我如何解决 Visio Design 文档遇到的问题。有几个 .vsdx 数百个文件,每个文件都会在新窗口中打开所有形状。

问题在于如何强制 Visio 在同一窗口中打开所有形状。

目标是相当于“选项 > 高级 > 在同一窗口中打开所有形状”

我尝试了两种方法但没有成功:

  1. Python 脚本
    import os
    import win32com.client
    directory = "C:\\Users\\User\\Documents\\Designs"
    
    def process_visio_files(directory):
        print("Visio Started.")
        visio.Visible = False  # Run Visio in the background
        for root, dirs, files in os.walk(directory):
            if "Archiv" in root:
                continue  # Skip folders containing "Archiv"
            for file in files:            
                if file.endswith('.vsdx'):
                    print("File is {}".format(file))
                    full_path = os.path.join(root, file)
                    # Process the Visio file
                    document = visio.Documents.Open(full_path)
                    dir(document)
                    document.PropertyToPreventSeparateWindows = True # HERE IS WHERE I NEED TO DO SOME MAGIC FOR THE SHAPES TO OPEN IN THE SAME WINDOW
                    document.Save()
                    document.Close()
        visio.Quit()
    
    process_visio_files(directory)

我不太能找到一种方法来使用 Python 实现所需的更改。 VBA 取得了一定的成功。

  1. VBA脚本(这个脚本关闭了一些形状,所有未以只读模式打开的形状):
Sub ProcessVisioFilesInFolders()
    Dim rootFolder As String
    rootFolder = "C:\Users\User\Documents\Designs\"

    ProcessFolder rootFolder
End Sub

Sub ProcessFolder(folderPath As String)
    Dim fs As Object
    Dim folder As Object
    Dim subfolder As Object
    Dim file As Object
    Dim visioApp As Object

    On Error Resume Next
    ' Try to get an existing instance of Visio
    Set visioApp = GetObject(, "Visio.Application")
    On Error GoTo 0

    If visioApp Is Nothing Then
        ' If Visio is not running, create a new instance
        Set visioApp = CreateObject("Visio.Application")
        visioApp.Visible = True ' Make Visio application visible
    End If

    ' Create FileSystemObject
    Set fs = CreateObject("Scripting.FileSystemObject")

    MsgBox "Processing folder: " & folderPath

    ' Loop through all files in the folder
    For Each file In fs.GetFolder(folderPath).Files
        If LCase(file.Name) Like "*.vsdx" Then
            MsgBox "Processing file: " & file.Path
            ' Process .vsdx files
            ProcessVisioFile visioApp, file.Path
        End If
    Next file

    ' Loop through all subfolders in the folder
    For Each subfolder In fs.GetFolder(folderPath).SubFolders
        ' Check if the subfolder name contains 'Archiv'
        If InStr(1, subfolder.Name, "Archiv", vbTextCompare) = 0 Then
            ' Recursively process subfolders
            ProcessFolder subfolder.Path
        Else
            MsgBox "Skipping 'Archiv' subfolder: " & subfolder.Path
        End If
    Next subfolder
End Sub

Sub ProcessVisioFile(visioApp As Object, filePath As String)
    Dim doc As Object
    Dim win As Object
    Dim shapeSheetWin As Object

    ' Open the Visio file
    Set doc = visioApp.Documents.Open(filePath)

    ' Ensure Visio application is visible and brought to the front
    visioApp.Visible = True

    ' Introduce a delay to allow the application to become active
    Dim startTime As Double
    startTime = Timer
    Do While Timer < startTime + 1 ' Adjust the delay as needed
        DoEvents
    Loop

    ' Loop through all windows
    For Each win In visioApp.Windows
        If win.Type = 1 Then ' visDrawing type
            ' Your processing logic here
            On Error Resume Next
            win.HideShapesheet
            On Error GoTo 0
        ElseIf win.Type = 2 Then ' visStencil type
            ' Close stencil windows
            Set shapeSheetWin = win
            shapeSheetWin.Close
        End If
    Next win

    ' Save the changes to the Visio file
    MsgBox "Saving changes to Visio file: " & filePath
    doc.Save

    ' Close the Visio file
    MsgBox "Closing Visio file: " & filePath
    doc.Close
End Sub

任何人都可以分享如何解决在单独窗口中打开的形状的问题吗?

python python-3.x vba visio
1个回答
0
投票

在您的第一张图片的原始帖子中,我可以看到很多模板,它们在单独的窗口中以只读 [RO] 的形式打开...
这不是 ShapeSheet 窗口!!!

我没有具有相同问题的文档,我用代码创建了一个文档

Option Base 1
Sub Prepare()
Dim pth(3) As String, curpath As String, i As Integer
pth(1) = "C:\My Shapes\19_inch_Rack_flexible_RU.vss"
pth(2) = "C:\My Shapes\Favorites.vssx"
pth(3) = "C:\My Shapes\GOST_R_21.1101-2013.vss"
For i = LBound(pth) To UBound(pth)
    curpath = pth(i)
    Documents.OpenEx curpath, visOpenRO ' open stencil in new window as read-only
Next
ActiveDocument.DocumentSheet.OpenSheetWindow ' add Document ShapeSheet window 
End Sub

Demo

在我这边,如果窗口类型不是绘图窗口,下一个代码将关闭所有窗口。请阅读有关 VisWinTypes 枚举 (Visio)
的更多信息 接下来的代码可以关闭所有非绘图窗口

    Sub Test()
    Dim va As Application, vw As Window
    Set va = Application
    For Each vw In va.Windows
        Debug.Print vw.Caption, vw.Type, vw.SubType
        If Not vw.Type = visDrawing Then vw.Close ' visDrawing = 1 Python dont know Visio internal constants
    Next
    ActiveDocument.ContainsWorkspaceEx = False ' prevent save document workspace (a lot of RO-opened stencils)
    ActiveDocument.Save
    End Sub

'ActiveDocument.ContainsWorkspaceEx = False' 此行阻止保存工作区(所有 RO 模板)。请查看这篇文章Document.ContainsWorkspaceEx 属性 (Visio)

我现在还没有Python环境,稍后再找找……

© www.soinside.com 2019 - 2024. All rights reserved.