简体   繁体   English

如何使用VBA打开文件浏览器并嵌入文件?

[英]How to open file browser and emded a file using VBA?

I have a Word document which I want users to be able to embed other files into . 我有一个Word文档,希望用户能够将其他文件嵌入到其中

The other files will be various types and from the user's own drives which I can't predict. 其他文件将是各种类型,并且来自用户自己无法预测的驱动器。

Rather than them having to do this manually, is there a way of coding a command button to open file explorer, allow the user to select file(s) and then embed those files into the Word document to allow them to send it as one complete document? 而不是他们必须手动执行此操作,而是有一种方法可以对命令按钮进行编码以打开文件浏览器,允许用户选择文件,然后将这些文件嵌入到Word文档中以允许他们将其作为一个完整的文件发送。文献?

This should be a good place for you to start : 这应该是您开始的好地方:

Sub SelectFilesToEmbed()
    Dim dlgOpen As FileDialog, _
        objFile As FileDialogSelectedItems, _
        wdDoc As Word.Document
    Set dlgOpen = Application.FileDialog(FileDialogType:=msoFileDialogFilePicker)

    With dlgOpen
        .Title = "Select the files to be embedded"
       .AllowMultiSelect = True
       .Show
        If .Show = -1 Then
            For Each objFile In .SelectedItems
                wdDoc.Range.InlineShapes.AddOLEObject _
                                    ClassType:="Excel.Sheet.12", _
                                    Filename:=objFile, _
                                    LinkToFile:=False, _
                                    DisplayAsIcon:=False
            Next objFile
        Else
        End If
    End With

End Sub

You still have to solve the problems : 您仍然必须解决问题:

  1. Multiple files' types (here the code is for an excel sheet) 多个文件的类型(此处的代码用于Excel工作表)
  2. Set your word document if the code isn't placed in 如果未放置代码,请设置Word文档
  3. Set the place you want to embed the files (bookmarks or else) 设置您要嵌入文件的位置(书签或其他位置)

The link from which I took the embedding part : Embed a file into a Word doc using VBA 我使用嵌入部分的链接: 使用VBA将文件嵌入到Word文档中

For the file explorer, see Application.FileDialog 有关文件资源管理器,请参见Application.FileDialog

To embed files, I suggest to record the action as a VBA macro, and then adapt the macro to your needs (ie use the file selected via FileDialog). 要嵌入文件,我建议将操作记录为VBA宏,然后根据需要调整宏(即使用通过FileDialog选择的文件)。

Thanks to everyone for reading/helping. 感谢大家的阅读/帮助。 This project has sadly now been binned and so this is not a current requirement any more :( Just didn't want people to spend time on something that may not be actioned. 可悲的是,这个项目现在已经被分箱了,所以这不再是当前的要求:(只是不想让人们花时间去做一些可能无法采取的措施。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM