简体   繁体   中英

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 .

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?

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)
  2. Set your word document if the code isn't placed in
  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

For the file explorer, see 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).

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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