AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / computer / 问题 / 1517644
Accepted
mythofechelon
mythofechelon
Asked: 2020-01-17 02:16:27 +0800 CST2020-01-17 02:16:27 +0800 CST 2020-01-17 02:16:27 +0800 CST

批量插入图像作为文件对象

  • 772

我正在写一份报告,其中以文本形式总结了许多证据,并由数百个随附的屏幕截图支持,这些屏幕截图不一定需要查看,但需要作为选项提供。

因此,为了实现这一点,我想将图像文件批量插入/嵌入为对象,而不是图片,就像 Word 默认情况下对 HTML、PDF 等文件所做的那样。这样,如果用户想要查看文件,他们只需双击它们即可在默认应用程序中打开它们。

基本上,我希望最终结果如下所示: 在此处输入图像描述

但是,我看不到自动执行此操作的方法:

  • Insert选项卡→Text组→Object按钮→Create from File选项卡不允许选择多个文件。
  • 复制和粘贴通常会将它们作为图片插入。
  • 专门复制和粘贴(CTRL + ALT + V)Paste→→第二/底部Files→Display as icon将它们作为图片插入,即使它不应该这样做。

我可以手动完成,但这非常耗时,因为每个都必须单独完成,Word 永远不会记住最后使用的路径,Word 永远不会记住最后选择的图标,等等。

microsoft-word microsoft-word-2016
  • 1 1 个回答
  • 146 Views

1 个回答

  • Voted
  1. Best Answer
    mythofechelon
    2020-01-17T03:39:02+08:002020-01-17T03:39:02+08:00

    我创建了以下 VBA 代码,它可以满足我的需求:

    Public lastPath As String
    
    Sub InsertFolderContents()
        ' This mode is used to pick a folder and have all files inserted
        Dim counter_filesInserted As Integer
        counter_filesInserted = 1 ' Even though no files have been inserted yet, it's easier to not have to think in 0-based indexes
    
        Dim fileExplorer As FileDialog
        Dim folder_Path As String
    
        Set fileExplorer = Application.FileDialog(msoFileDialogFolderPicker)
        With fileExplorer
            .InitialFileName = lastPath
    
            If .Show = -1 Then ' ".Show" actually causes the dialogue to open
                folder_Path = .SelectedItems.Item(1) & Application.PathSeparator ' "Application.PathSeparator" is required to be appended otherwise the later concatenated path is invalid
                lastPath = folder_Path
            Else
                folder_Path = "NONE"
            End If
        End With
    
        Dim Files As String
        Files = Dir(folder_Path)
    
        ' For some reason, calling InsertFiles from within Do While completely breaks "Files = Dir" so need to build array of files THEN loop through them to call InsertFiles
    
        Dim counter_fileList As Integer
    
        Dim DirectoryListArray() As String
        ReDim DirectoryListArray(1000)
    
        Do While Files <> ""
            DirectoryListArray(counter_fileList) = Files
            Files = Dir
            counter_fileList = counter_fileList + 1
        Loop
    
        ReDim Preserve DirectoryListArray(counter_fileList - 1)
    
        For counter_fileList = 0 To UBound(DirectoryListArray)
            Dim file_Name_Original As String
            file_Name_Original = DirectoryListArray(counter_fileList)
            Dim file_Path As String
            file_Path = folder_Path & file_Name_Original
    
            InsertFiles file_Path, counter_filesInserted
        Next counter_fileList
    End Sub
    
    
    Sub InsertMultipleFiles()
        ' This mode is used to pick specific files to have inserted
    
        Dim counter_filesInserted As Integer
        counter_filesInserted = 1 ' Even though no files have been inserted yet, it's easier to not have to think in 0-based indexes
    
        Dim fileExplorer As FileDialog
        Dim folder_Path As String
    
        Set fileExplorer = Application.FileDialog(msoFileDialogFilePicker)
        With fileExplorer
            .InitialFileName = lastPath
            .AllowMultiSelect = True
    
            If .Show = -1 Then ' ".Show" actually causes the dialogue to open
                folder_Path = Left(.SelectedItems.Item(1), InStrRev(.SelectedItems.Item(1), "\"))
                lastPath = folder_Path
            Else
                folder_Path = "NONE"
            End If
    
            Dim file_Path As Variant
            For Each file_Path In .SelectedItems
                InsertFiles file_Path, counter_filesInserted
            Next
        End With
    End Sub
    
    Function InsertFiles(file_Path, counter_filesInserted)
        Dim file_Name_Original As String
        Dim file_Ext As String
        Dim file_Inserted As Boolean
        Dim regex As Object
    
        file_Name_Original = Dir(file_Path)
    
        file_Ext = Right(file_Path, Len(file_Path) - InStrRev(file_Path, "."))
    
        file_Inserted = False
    
        ' My report standalone files are named "<section number> <section title> - " so this regex strips those out for readability but doesn't affect files that aren't named that way
        Set regex = CreateObject("VBScript.RegExp")
        regex.Pattern = "\d{1,2}.\d{1,2}(.\d{1,2})?[\w\s]+ - "
        regex.IgnoreCase = True
        regex.Global = True
        file_Name_Shortened = regex.Replace(file_Name_Original, "")
    
        ' The IconIndex number is literally just what number icon is inside that file -1 (as it's a 0-based index). An easy way to determine this is to use Word's "Change icon" function.
    
        If file_Ext = "png" Or file_Ext = "jpg" Then
            Selection.InlineShapes.AddOLEObject _
            FileName:=file_Path, _
            LinkToFile:=False, _
            DisplayAsIcon:=True, _
            IconFileName:="C:\Program Files (x86)\Internet Explorer\iexplore.exe", _
            IconIndex:=13, _
            IconLabel:=file_Name_Shortened
    
            file_Inserted = True
        ElseIf file_Ext = "html" Then
            Selection.InlineShapes.AddOLEObject _
            FileName:=file_Path, _
            LinkToFile:=False, _
            DisplayAsIcon:=True, _
            IconFileName:="C:\Program Files (x86)\Internet Explorer\iexplore.exe", _
            IconIndex:=1, _
            IconLabel:=file_Name_Shortened
    
            file_Inserted = True
        ElseIf file_Ext = "pdf" Then
            Selection.InlineShapes.AddOLEObject _
            FileName:=file_Path, _
            LinkToFile:=False, _
            DisplayAsIcon:=True, _
            IconFileName:="C:\Windows\Installer\{AC76BA86-7AD7-1033-7B44-AC0F074E4100}\PDFFile_8.ico", _
            IconIndex:=1, _
            IconLabel:=file_Name_Shortened
    
            file_Inserted = True
        ElseIf file_Ext = "csv" Or file_Ext Like "xls*" Then
            Selection.InlineShapes.AddOLEObject _
            FileName:=file_Path, _
            LinkToFile:=False, _
            DisplayAsIcon:=True, _
            IconFileName:="C:\Windows\Installer\{90160000-000F-0000-0000-0000000FF1CE}\xlicons.exe", _
            IconIndex:=1, _
            IconLabel:=file_Name_Shortened
    
            file_Inserted = True
        ElseIf file_Ext Like "doc*" Then
            Selection.InlineShapes.AddOLEObject _
            FileName:=file_Path, _
            LinkToFile:=False, _
            DisplayAsIcon:=True, _
            IconFileName:="C:\Windows\Installer\{90160000-000F-0000-0000-0000000FF1CE}\wordicon.exe", _
            IconIndex:=13, _
            IconLabel:=file_Name_Shortened
    
            file_Inserted = True
        End If
    
        If file_Inserted = True Then
            ' Inserted file objects look untidy without a tab for space between them but you have to not do this every 4th otherwise it looks weird.
            If (counter_filesInserted Mod 4) <> 0 Or counter_filesInserted = 0 Then
                    Selection.TypeText Text:=vbTab
            End If
    
            counter_filesInserted = counter_filesInserted + 1
        End If
    End Function
    

    在此处输入图像描述

    这样做的一个很好的副作用是文件按字母顺序排序,而如果您使用常规方法批量导入,则文件不是按字母顺序排列的。

    • 1

相关问题

  • MS Word – 如何在每页的右边缘插入一列

  • 使用 Microsoft Word 保存不带 BOM 的 UTF-8 文件

  • 如何去除Word中奇怪的空格符号

  • 在word中编辑模板以删除难看的空格

  • Microsoft Word - 如何减小所有样式的文本大小

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何减少“vmmem”进程的消耗?

    • 11 个回答
  • Marko Smith

    从 Microsoft Stream 下载视频

    • 4 个回答
  • Marko Smith

    Google Chrome DevTools 无法解析 SourceMap:chrome-extension

    • 6 个回答
  • Marko Smith

    Windows 照片查看器因为内存不足而无法运行?

    • 5 个回答
  • Marko Smith

    支持结束后如何激活 WindowsXP?

    • 6 个回答
  • Marko Smith

    远程桌面间歇性冻结

    • 7 个回答
  • Marko Smith

    子网掩码 /32 是什么意思?

    • 6 个回答
  • Marko Smith

    鼠标指针在 Windows 中按下的箭头键上移动?

    • 1 个回答
  • Marko Smith

    VirtualBox 无法以 VERR_NEM_VM_CREATE_FAILED 启动

    • 8 个回答
  • Marko Smith

    应用程序不会出现在 MacBook 的摄像头和麦克风隐私设置中

    • 5 个回答
  • Martin Hope
    CiaranWelsh 如何减少“vmmem”进程的消耗? 2020-06-10 02:06:58 +0800 CST
  • Martin Hope
    Jim Windows 10 搜索未加载,显示空白窗口 2020-02-06 03:28:26 +0800 CST
  • Martin Hope
    v15 为什么通过电缆(同轴电缆)的千兆位/秒 Internet 连接不能像光纤一样提供对称速度? 2020-01-25 08:53:31 +0800 CST
  • Martin Hope
    fixer1234 “HTTPS Everywhere”仍然相关吗? 2019-10-27 18:06:25 +0800 CST
  • Martin Hope
    andre_ss6 远程桌面间歇性冻结 2019-09-11 12:56:40 +0800 CST
  • Martin Hope
    Riley Carney 为什么在 URL 后面加一个点会删除登录信息? 2019-08-06 10:59:24 +0800 CST
  • Martin Hope
    zdimension 鼠标指针在 Windows 中按下的箭头键上移动? 2019-08-04 06:39:57 +0800 CST
  • Martin Hope
    jonsca 我所有的 Firefox 附加组件突然被禁用了,我该如何重新启用它们? 2019-05-04 17:58:52 +0800 CST
  • Martin Hope
    MCK 是否可以使用文本创建二维码? 2019-04-02 06:32:14 +0800 CST
  • Martin Hope
    SoniEx2 更改 git init 默认分支名称 2019-04-01 06:16:56 +0800 CST

热门标签

windows-10 linux windows microsoft-excel networking ubuntu worksheet-function bash command-line hard-drive

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve