如何使用Excel VBA打开SharePoint列表

如何使用Excel VBA打开SharePoint列表,excel,vba,sharepoint,count,office365,Excel,Vba,Sharepoint,Count,Office365,我有以下想法 首先,将我的Excel与SharePoint连接,以获取SharePoint列表 其次,遍历列表中的每一项并逐个打开它们 在每个列表项中都有一个包含不同文件的库。例如Word、Excel等 我需要计算列表中有多少个列表项以及每个列表项中有多少个文件 如果这有效,那么 在SharePoint的列表项中打开正确的Excel文件。类似于excel文件名中的“%activities%” 计算此Excel文件中的行数,并将数据复制到新的Excel文件中 因此,最终我们将拥有一个新

我有以下想法

  • 首先,将我的Excel与SharePoint连接,以获取SharePoint列表

  • 其次,遍历列表中的每一项并逐个打开它们

  • 在每个列表项中都有一个包含不同文件的库。例如Word、Excel等

  • 我需要计算列表中有多少个列表项以及每个列表项中有多少个文件

  • 如果这有效,那么

  • 在SharePoint的列表项中打开正确的Excel文件。类似于excel文件名中的“%activities%”

  • 计算此Excel文件中的行数,并将数据复制到新的Excel文件中

  • 因此,最终我们将拥有一个新的Excel文件,其中包含来自不同Excel文件的多张表格。这可能吗?或者这个想法太疯狂了:)

    我试着这样连接:

    Sub test()
      Dim dm As New DriveMapper
      Dim sharepointFolder As Scripting.folder
    
      Set sharepointFolder = dm.MapDrive("\\***.sharepoint.com@SSL\sites\General1462\Lists\")
    
      Debug.Print sharepointFolder.Path
    End Sub
    
    但我得到了一个错误代码(800704dc)。我尝试过这个解决方案

        Function tableWithAllSub([Microsoft.SharePoint.Client.Web]$Web){
    
        $counterSub= 1
        $counterLoadingBar = 0
        $counterForFileFound = 0
        $counterFileIsEmpty = 0
        $counterFolderIsEmpty = 0
        $counterForSumupAllRows = 0
        $counterForTemplates = 0
    
    $excludedLists  = @("Reusable Content","Content and Structure Reports","Form Templates")
    
    $Lists = Get-PnPList | Where {$_.Hidden -eq $False -and $excludedLists -notcontains $_.Title}
    
        $xl = new-object -c excel.application
        $xl.displayAlerts = $false
    
    
        $tableAllInformation = foreach ($List in $Lists){
            
    
            $fileFound = "Nein"
            $counterAllItemsElseIf = 0
            
    
    
            #FileRef = Pfad zur Datei. File_x0020_Type = Dateityp. FileLeafRef = Name der Datei.
            $allItems = Get-PnPListItem -List $List -Fields "FileRef", "File_x0020_Type", "FileLeafRef"
    
            foreach ($Item in $allItems) {
    
    
                $counterOfRows = 0
                $counterAllItemsElseIf++
    
                if ($Item["FileLeafRef"] -like "*Record*") {
    
    
                    $fileFound = "Ja"
                    $counterForFileFound++
    
    
                    Get-PnPFile -Url $Item["FileRef"] -Path C:\Users\$env:USERNAME\Desktop\$nameOfDirectoryToday\tmp\ -Filename $Item["FileLeafRef"] -AsFile -Force
                    
    
                    $nameOfFile = $Item["FileLeafRef"]
                    $filePath = "C:\Users\$env:USERNAME\Desktop\$nameOfDirectoryToday\tmp\$nameOfFile"
    
    
                    $row = 4
                    $column = "C"
                    
                            
                    $excelDocumentSource = $xl.workbooks.open($filePath, $null, $true)
                    $sheetToSearchIn = $excelDocumentSource.sheets.item('REGISTER')    
                    
    
                        $cellEmpty = "Nein"
                        if([string]::IsNullOrEmpty($sheetToSearchIn.Cells.Item($row, $column).Value2) ) {
                            $cellEmpty = "Ja"
                            $counterFileIsEmpty++
                        }
    
    
                        while(-not ([string]::IsNullOrEmpty($sheetToSearchIn.Cells.Item($row + $counterOfRows, $column).Value2) ) ) {
                            $counterOfRows++        
                        }
    
                        $counterForSumupAllRows += $counterOfRows
    
    
                        if($counterOfRows -eq 36 -and $sheetToSearchIn.Cells.Item(4, "C").Value2 -eq "services" -and $sheetToSearchIn.Cells.Item(5, "C").Value2 -eq "Local"){
                            $cellEmpty = "Template"
                            $counterForTemplates++
                        }
                        
    
                    $excelDocumentSource.close($false)                               
    
    
                    $userValueCollection = [Microsoft.SharePoint.Client.FieldUserValue[]]$Item["Editor"]
                    $lastModifiedBy = $userValueCollection.LookupValue.ToString()
    
    
                    $modifiedOn = $Item["Modified"]
    
    
                        if($List.Title -eq "service"){
                            $subFolderService = searchForForwardSlash($Item["FileRef"].substring(29))
                            $nameofSub = $List.Title + " - " + $subFolderService
                        }
                        else{$nameofSub = $List.Title}
    
    
                    [PSCustomObject] @{
                    'N'                = $counterSub++
                    'T'   = $nameofSub
                    'D'        = $fileFound
                    'Z'     = $cellEmpty
                    'A'    = $counterOfRows
                    'Z'  = $lastModifiedBy 
                    'G'           = $modifiedOn
                    'D'             = $Item["FileLeafRef"]
                    'P'        = $Item["FileRef"]
                    }   
                }
    
             elseIf ($counterAllItemsElseIf -eq $allItems.count -and $fileFound -eq "Nein"){
    
                    $counterFolderIsEmpty++
    
                    if($List.Title -eq "service"){
                        $subFolderService = searchForForwardSlash($Item["FileRef"].substring(29))
                        $nameofSub = $List.Title + " - " + $subFolderService
                    }
                    else{$nameofSub = $List.Title}
    
                    [PSCustomObject] @{
                    'N'                = $counterSub++
                    'T'   = $nameofSub
                    'D'        = $fileFound
                    'Z'     = " "
                    'A'    = " "
                    'Z'  = " " 
                    'G'           = " "
                    'D'             = " "
                    'P'        = " "
                    }
                        
                }
                
            }
    
                    
            $counterLoadingBar++
            $showProgress = [int]( (100/$Lists.count) * $counterLoadingBar)
            Write-Progress -Activity "Search in Progress" -Status "$showProgress% Complete:" -PercentComplete ( (100/$Lists.count) * $counterLoadingBar)
      
        }
    
        $endResult= [PSCustomObject] @{
                    'A'                = $Lists.count
                    'O'                = $counterFolderIsEmpty
                    'A'     = $counterForFileFound
                    'L'                    = $counterFileIsEmpty
                    'A'              = ($counterForFileFound - $counterFileIsEmpty)
                    'I'    = $counterForSumupAllRows - ($counterForTemplates * 36)
                    'A'             = $counterForTemplates
                    }
    
        $xl.quit()
    
        $tableAllInformation | Export-CSV $CSVFile -NoTypeInformation -Force
    
        $tableAllInformation | Format-Table *
        $endResult | Format-Table *
    }