VBA-Insert multiple PDF files

DoubleEntry

New Member
Joined
Mar 15, 2016
Messages
3
Hi

I am new to VBA and I was hoping you could help me with the following.

I am trying to write a code to help me insert multiple (about 100) pdf files to different worksheets and name the worksheets as the pdf file they contain. Also create a linked index as the main page with the filenames linked to the worksheets.

All the pdf files are in one folder.

Hope you can help.

Kind regards,
DE
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
not a good idea to insert pdf into a workbook, but how about inserting LINKS to the 100 pdfs?
and you can open the pdf with just a click.

Code:
Public Sub grabAllFilenames()
Dim xFileSystemObject As Object
Dim xFolder As Object
Dim xSubFolder As Object
Dim vFolderName As String, sLongFilename As String
Dim xFile


vFolderName = "c:\temp\"


Set xFileSystemObject = CreateObject("Scripting.FileSystemObject")
Set xFolder = xFileSystemObject.GetFolder(vFolderName)


Range("A1").Value = "File"
Range("A2").Select
For Each xFile In xFolder.Files
   if instr(xfile.name,".pdf")>0 then
   ActiveCell.Value = xFile.Name
   ActiveSheet.Hyperlinks.Add Anchor:=Selection, Address:=xFile, TextToDisplay:=xFile.Name
  endif


   ActiveCell.Offset(1, 0).Select   'next row
Next


Set xFolder = Nothing
Set xSubFolder = Nothing
Set xFileSystemObject = Nothing
End Sub
 
Last edited:
Upvote 0
Thank you ranman256. I need to embed the files as objects as the excel workbook will be sent to audit. But I will try this way.
Many thanks again.

DE
 
Upvote 0
I made the provide code work:

VBA Code:
Public Sub grabAllFilenames()
Dim xFileSystemObject As Object
Dim xFolder As Object
Dim xSubFolder As Object
Dim vFolderName As String, sLongFilename As String
Dim xFile

vFolderName = "C:\Users\Audit\Samples"
Set xFileSystemObject = CreateObject("Scripting.FileSystemObject")
Set xFolder = xFileSystemObject.GetFolder(vFolderName)

Range("A1").Value = "Files"
Range("a2").Select
For Each xFile In xFolder.Files
   If InStr(xFile.Name, ".pdf") > 0 Then
      ActiveSheet.OLEObjects.Add(Filename:= _
        xFile _
        , Link:=False, DisplayAsIcon:=True, IconFileName:= _
        """C:\Program Files (x86)\Adobe\Acrobat DC\Acrobat\Acrobat.exe""", IconIndex _
        :=0, IconLabel:=xFile.Name).Select
        
  End If

   ActiveCell.Offset(1, 0).Select   'next row
Next


Set xFolder = Nothing
Set xSubFolder = Nothing
Set xFileSystemObject = Nothing
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top