【问题标题】:Excel VBA - Find Local File Location of Files on SharePointExcel VBA - 在 SharePoint 上查找文件的本地文件位置
【发布时间】:2019-09-23 12:16:22
【问题描述】:

我有一个创建了 SharePoint 文档文件夹的企业帐户,我已通过 OneDrive(在 D 盘中,本地访问)将其本地同步到我的计算机。

在 Excel 的 VBA 中,我知道有一种方法可以使用调用 OneDrive 本地位置

environ("OneDrive")

但是,我不知道如何在我的 Excel 文件中指定它以找到存储在 SharePoint 上的本地文件。

这很重要的原因是我需要使用 VBA 来执行邮件合并,并且我发现 SharePoint 存储的 XLSM 无法正确连接。代码如下:

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

我需要将 OSPFullPath 指向本地(例如 D:\One Drive\Excel.xlsm)。我无法使用“全名”功能。

我可以使用任何环境代码,或者无论如何检测文件位置?

【问题讨论】:

    标签: excel vba sharepoint


    【解决方案1】:

    如果我正确理解了这个问题,以下内容可能会有所帮助,因为它在 sharepoint 上获得了工作簿的全名:所有功劳在这里回答 3373470

    Private Function Local_Workbook_Name(ByRef wb As Workbook) As String
    
        Dim i As Long, j As Long
        Dim OneDrivePath As String
        Dim ShortName As String
    
        'Check if it looks like a OneDrive location
        If InStr(1, wb.FullName, "https://", vbTextCompare) > 0 Then
            'Replace forward slashes with back slashes
            ShortName = Replace(wb.FullName, "/", "\")
    
            'Remove the first four backslashes
            For i = 1 To 4
                ShortName = Mid(ShortName, InStr(ShortName, "\") + 1)
            Next
    
            'Loop to see if the tentative LocalWorkbookName is the name of a file that actually exists, if so return the name
            For j = 1 To 3
                OneDrivePath = Environ(Choose(j, "OneDrive", "OneDriveCommercial", "OneDriveConsumer"))
                If Len(OneDrivePath) > 0 Then
                    Local_Workbook_Name = OneDrivePath & "\" & ShortName
                    If Dir(Local_Workbook_Name) <> "" Then
                        Exit Function
                    End If
                End If
            Next j
            'Possibly raise an error here when attempt to convert to a local file name fails - e.g. for "shared with me" files
        End If
    
        Local_Workbook_Name = wb.FullName
    
    End Function
    

    此线程上有更多答案以及指向其他页面的链接可能会有所帮助

    【讨论】:

    • 感谢您的尝试!但是,我认为环境数组并没有返回我想要的有利位置。 Excel 似乎不喜欢 OneDriveCommercial,无法将其识别为 Sharepoint 位置。 OneDrive 和 OneDriveCommerical 都指向我的“个人”OneDrive,而不是我的 Sharepoint ......这很可悲,因为我以为他们就是这样分类的!
    • 我刚刚查看了我的环境变量,它们要么指向我的个人 OneDrive,要么指向我的工作 OneDrive。它们都没有指向我同步的 Sharepoint 文件夹。这是一个令人沮丧的问题。
    猜你喜欢
    • 1970-01-01
    • 2019-07-18
    • 2019-07-18
    • 2022-08-15
    • 2018-05-09
    • 2017-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多