word宏批量插入.csv格式的附件

1.ALT+F11打开宏的编译器,复制下面代码

2 F5运行,弹出对话框选择对应文件即可

vbscript 复制代码
Sub BatchInsertCSVAttachments()
    Dim fd As FileDialog
    Dim vrtSelectedItem As Variant
    Dim iconLabel As String
    
    ' 创建文件选择对话框
    Set fd = Application.FileDialog(msoFileDialogFilePicker)
    
    With fd
        .Title = "请选择要插入的CSV文件(可多选)"
        .Filters.Clear
        .Filters.Add "CSV Files", "*.csv"
        .AllowMultiSelect = True ' 允许通过按住 Ctrl 或 Shift 多选
        
        If .Show = -1 Then
            ' 遍历所选文件
            For Each vrtSelectedItem In .SelectedItems
                ' 获取文件名作为图标下方的标签
                iconLabel = Dir(vrtSelectedItem)
                
                ' 插入 OLE 对象(即文件附件)
                ' DisplayAsIcon:=True 强制显示为图标
                ' IconLabel 设置为文件名,否则会显示默认的 "Worksheet" 等字样
                Selection.InlineShapes.AddOLEObject _
                    FileName:=vrtSelectedItem, _
                    LinkToFile:=False, _
                    DisplayAsIcon:=True, _
                    IconLabel:=iconLabel
                
                ' 插入一个换行符,防止图标挤在一起
                Selection.TypeParagraph
                Selection.TypeParagraph
            Next
        End If
    End With
    
    MsgBox "批量插入完成!", vbInformation
    
    ' 清理内存
    Set fd = Nothing
End Sub
相关推荐
程序人生8886 小时前
PDF 转 Word 版式错乱、表格丢失?DocConverter Web 格式互转引擎的保真实践
前端·人工智能·opencv·机器学习·pdf·word
Patrick在香港8 小时前
同一份脚本,mac 正常 Windows 乱码:open() 默认编码实测(附 3.15 终局)
utf-8·windows·python·macos·跨平台·编码·标准库
郭庆汝12 小时前
windows本地部署AgentDesk
windows
笨笨D幸福12 小时前
win11 vhdx/wtg 安装出现问题,提示 Windows 安装程序无法将 Windows 配置为在此计算机的硬件上运行
windows
h397419 小时前
DirectShow过滤器开发-视频源过滤器
c++·windows·音视频·directshow
java资料站1 天前
二、Spring AI Alibaba · ChatModel
java·windows·spring
北冥you鱼1 天前
Go 语言空接口(interface{})使用场景与最佳实践
开发语言·windows·golang
夏虫不可与之言冰1 天前
Windows WSL2 (Ubuntu) 搭建 ESP‑IDF 6.1 开发环境全过程演示
linux·windows·ubuntu·esp32·乐鑫科技·乐鑫eim·乐鑫代理商
weixin_416660071 天前
多个 Markdown 文件批量转 Word:PowerShell + Pandoc,连同图片和公式一起检查
word·豆包·deepseek
必须得开心呀1 天前
为什么 python-docx 打不开加密的 .docx,Word COM 却能打开
python·word