Upload document into the excel via VBA based on initial name

Rahulwork

Active Member
Joined
Jun 9, 2013
Messages
284
Hello Everyone,

I wrote below code, this code upload all document from sharedrive into excel based on their naming convention.

However it's applicable for Pdf file only. can you please help me that how can i upload other ext file as well like outlook/word/excel?

Dim folderPath As String, fileName As String
Dim destCell As Range

With Worksheets("ABC")
folderPath = .Range("F7").Value
Set destCell = .Range("G12")
End With

If Right(folderPath, 1) <> "\" Then folderPath = folderPath & "\"

Application.ScreenUpdating = False
fileName = Dir(folderPath & "ABC*.pdf")
Do While fileName <> vbNullString
destCell.Worksheet.OLEObjects.Add _
fileName:=folderPath & fileName, _
Link:=False, _
DisplayAsIcon:=True, _
IconFilename:="C:\windows\Installer\{AC76BA86-1033-F400-7760-000000000005}\_PDFFile.ico", _
IconIndex:=0, _
IconLabel:=fileName, _
Left:=destCell.Left, _
Top:=destCell.Top, _
Width:=150, _
Height:=5
Set destCell = destCell.Offset(, 1)
fileName = Dir
Loop
Application.ScreenUpdating = True

MsgBox "Document Uploaded Successfully..!!"
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
It is not clear to me exactly what you are asking for help with. This is the line in your code that chooses certain pdf files:
VBA Code:
fileName = Dir(folderPath & "ABC*.pdf")

If you want your code to use another file extension, then change that line. Example:
VBA Code:
fileName = Dir(folderPath & "ABC*.docx")

Of course, for each new extension you will also need to specify a new icon file in this line:
VBA Code:
IconFilename:="C:\windows\Installer\{AC76BA86-1033-F400-7760-000000000005}\_PDFFile.ico", _
 
Upvote 0
Thanks for response.

i want if in folder there is pdf and word file with ABC so i need both the document with their icon.

by above code it's coming only of pdf.

IconFilename:="C:\windows\Installer\{AC76BA86-1033-F400-7760-000000000005}\_PDFFile.ico", _

can you pls tell me how can i put multple iconfilename in code?
 
Upvote 0
The general approach would be to use a Select Case statement (not tested)
VBA Code:
  Dim IconFile As String, FileExt As String
    Do While fileName <> vbNullString
        FileExt = UCase(Right(fileName, Len(fileName) - InStrRev(fileName, ".")))
        'select icon file depending on what the file extension is
        Select Case FileExt
        Case "PDF"
            IconFile = "C:\windows\Installer\{AC76BA86-1033-F400-7760-000000000005}\_PDFFile.ico"
        Case "DOC", "DOCX"
            IconFile = "C:\windows\Installer\{AC76BA86-1033-F400-7760-000000000005}\_PDFFile.ico"    '<- replace with word icon file on your PC that you want to use
        Case "TXT"
            IconFile = "C:\windows\Installer\{AC76BA86-1033-F400-7760-000000000005}\_PDFFile.ico"    '<- replace with text file icon file on your PC that you want to use
        End Select

        destCell.Worksheet.OLEObjects.Add _
        fileName:=folderPath & fileName, _
        Link:=False, _
        DisplayAsIcon:=True, _
        IconFileName:=IconFile, IconIndex:=0, IconLabel:=fileName, Left:=destCell.Left, Top:=destCell.Top, Width:=150, Height:=5
        Set destCell = destCell.Offset(, 1)
        fileName = Dir
    Loop
 
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,260
Members
452,627
Latest member
KitkatToby

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