word批量导出visio图

具体步骤

修改word格式

将word文档修改为docm格式

打开VBA窗口

打开开发工具VisualBasic项,如果没有右键在自定义功能区添加

插入代码

插入 -> 模块,代码如下:

vba 复制代码
Sub ExportAllVisioDiagrams()
    Dim shp As InlineShape
    Dim i As Integer
    Dim savePath As String
    Dim docName As String
    Dim visioApp As Object
    Dim visioDoc As Object
    Dim startTime As Double
    
    ' 设置保存路径(修改为您想要的路径)
    savePath = "C:\Users\"
    
    ' 创建文件夹(如果不存在)
    If Dir(savePath, vbDirectory) = "" Then MkDir savePath
    
    ' 获取文档名称(不含扩展名)
    If ActiveDocument.Name Like "*.*" Then
        docName = Left(ActiveDocument.Name, InStrRev(ActiveDocument.Name, ".") - 1)
    Else
        docName = ActiveDocument.Name
    End If
    
    ' 创建Visio应用实例
    Set visioApp = CreateObject("Visio.Application")
    visioApp.Visible = True ' 设置为可见以便调试
    
    i = 1
    For Each shp In ActiveDocument.InlineShapes
        If shp.Type = wdInlineShapeEmbeddedOLEObject Then
            If InStr(1, shp.OLEFormat.ProgID, "Visio", vbTextCompare) > 0 Then
                On Error Resume Next
                
                ' 激活并选择Visio对象内容
                shp.OLEFormat.Activate
                shp.OLEFormat.Object.Application.ActiveWindow.SelectAll
                shp.OLEFormat.Object.Application.ActiveWindow.Selection.Copy
                
                ' 创建新Visio文档
                Set visioDoc = visioApp.Documents.Add("")
                
                ' 添加延迟确保复制完成
                startTime = Timer
                Do While Timer < startTime + 1
                    DoEvents
                Loop
                
                ' 粘贴内容
                visioApp.ActiveWindow.Page.Paste
                
                ' 保存文件
                visioDoc.SaveAs savePath & docName & "_Diagram" & i & ".vsdx"
                If Err.Number <> 0 Then
                    visioDoc.SaveAs savePath & docName & "_Diagram" & i & ".vsd"
                End If
                
                visioDoc.Close
                Set visioDoc = Nothing
                
                i = i + 1
                
                ' 每处理3个图表后增加延迟
                If i Mod 3 = 0 Then
                    startTime = Timer
                    Do While Timer < startTime + 2 ' 延迟2秒
                        DoEvents
                    Loop
                End If
                
                On Error GoTo 0
            End If
        End If
    Next shp
    
    ' 关闭Visio
    visioApp.Quit
    Set visioApp = Nothing
    
    MsgBox "已导出 " & (i - 1) & " 个Visio图表到 " & savePath
End Sub

运行代码

点击运行 -> 运行子过程即可

相关推荐
xingpanvip16 分钟前
星盘接口开发文档:组合三限盘接口指南
android·开发语言·前端·python·php·lua
无忧.芙桃30 分钟前
现代C++讲解之变量模板,泛型lambda,函数返回类型推导的使用
开发语言·c++·visualstudio
唐青枫31 分钟前
内存为什么越来越高?C#.NET GC 详解:分代回收、LOH、终结器与性能优化实战
c#·.net
weixin_4166600734 分钟前
豆包公式转Word,乱码解决
word·latex·豆包
格林威1 小时前
工业视觉检测:两大主流异常检测开源框架深度对比(PatchCore vs SPADE)
开发语言·人工智能·深度学习·数码相机·计算机视觉·视觉检测·工业相机
2zcode1 小时前
基于Matlab元胞自动机模拟(CA)静态再结晶过程
开发语言·matlab·静态再结晶
研究点啥好呢1 小时前
滴滴Go后端开发工程师面试题精选:10道高频考题+答案解析
java·开发语言·golang
Levin__NLP_CV_AIGC1 小时前
py文件中文件复制方法
开发语言·python
yong99901 小时前
EKF-SLAM在MATLAB上的仿真实现
开发语言·matlab
广州山泉婚姻1 小时前
C语言三种基本程序结构详解
c语言·开发语言