VBA for Looping Through All Files in SharePoint Library and Attach to Email

furstukin

Board Regular
Joined
Apr 22, 2011
Messages
71
I am trying to redo some code that works flawlessly on a local or shared drive, but I need to use SharePoint as the location for my files as it is the only place we can guarantee everyone has access too.

My code is simple, all it does is look to see if there was a list of external email addresses and if so it opens a template email and attaches all files in a given location to that email, then blind carbon copies everyone. a

It looks like this.

Code:
Sub SendExtEmail()
Dim OutApp
Dim OutMail As Outlook.MailItem
Dim ExtAttach, EVMmail As String
Sheets("Macro Run Controls").Select
ExtAttach = C:\Path to files
EVMmail = C:\Location of saved email template
'Create the external manager email

Set OutApp = CreateObject("Outlook.Application")
On Error Resume Next
Set OutMail = OutApp.CreateItemFromTemplate(EVMmail)
With OutMail
    .SentOnBehalfOfName = "YourEmail@mail.com"
    .To = ""
    .CC = ""
    .BCC = "YourEmail@mail.com; " & ExtMgrStr
    
    StrFile = Dir(ExtAttach & "*.*")
    Do While Len(StrFile) > 0
        .Attachments.Add ExtAttach & StrFile
        StrFile = Dir
    Loop
    
    .Display   'or use .Send
End With
On Error GoTo 0

End Sub

I have tried to just add a path to the SharePoint Library which works fine for opening and saving files, or even directly referencing them to attach it to an email (Example: .Attachments.Add "http://SharePoint/Library/MyFile.pdf works just fine). Unfortunately SharePoint Libraries apparently can't be used as a directory I can loop through. Is there a method I can use to do this?
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Ok I figured it out on my own. You can use the SharePoint as a directory you just have to change the way the path is written. for a standard SharePoint path i.e. http://SharePoint.Company.com/TeamSites/Site1/Documents ... you need to do a few things. First remove http:, then change all "/" to "" and then ensure there is a trailing "" at the end of your path. Once you convert it to this \\SharePoint.Company.com\TeamSites\Site1\Documents\ VBA can use it as a directory in the same way as above.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,213,490
Messages
6,113,957
Members
448,535
Latest member
alrossman

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