I have a business account that created a SharePoint document folder, of which I've locally synced it to my computers via OneDrive (in D drive, accessible locally).
In VBA of Excel, I know there is a way to call the OneDrive local location by using
environ("OneDrive")
However, I don't know how to specify that in my Excel file in order to locate the local file that's stored on SharePoint.
The reason why this is important is that I need to use VBA to execute mail merge and that I discovered that a SharePoint-stored XLSM cannot be connected properly. Here is the code:
Private Sub InvitationLetter_Click()
Dim WordApp As New Word.Application, ActionFormDocument As Word.Document, WorksheetName As String
Dim OSPFullPath As String: OSPFullPath = ThisWorkbook.FullName
WorksheetName = ActiveWorkbook.Sheets("Guest Speakers").Name
With WordApp
.DisplayAlerts = wdAlertsNone
Set ActionFormDocument = .Documents.Open(ThisWorkbook.Path & "\1.2 - Guest Speaker\02 - Guest Speaker Invitation Letter.docx", _
ConfirmConversions:=False, ReadOnly:=False, AddToRecentfiles:=False)
With ActionFormDocument
With .MailMerge
.MainDocumentType = wdFormLetters
.SuppressBlankLines = False
.OpenDataSource Name:=OSPFullPath, ReadOnly:=False, _
LinkToSource:=True, AddToRecentfiles:=False, _
Format:=wdOpenFormatAuto, _
Connection:="Provider=Microsoft.ACE.OLEDB.12.0;" & _
"User ID=Admin;Data Source=OSPFullPath;" & _
"Mode=Read;Extended Properties=""HDR=YES;IMEX=1"";", _
SQLStatement:="SELECT * FROM`" & WorksheetName & "$`" & "WHERE `Status` = 'Pending' And `Nomination Details Alert` LIKE '%Urgent%'", _
SubType:=wdMergeSubTypeAccess
.ViewMailMergeFieldCodes = 0
With .DataSource
.FirstRecord = wdDefaultFirstRecord
.LastRecord = wdDefaultLastRecord
End With
End With
End With
.DisplayAlerts = wdAlertsAll
.Visible = True
.Activate
End With
Unload Me
End Sub
I need to point the OSPFullPath locally (e.g. D:\One Drive\Excel.xlsm) instead. I cannot use the 'fullname' function.
Any environ code I can use, or anyway to detect the file location?