简体   繁体   中英

Read and copy info from excel file on sharepoint

I am working writing VBA code in one SharePoint2016 excel file (File1) for task automation. Part of the task is to ready and copy information from another excel file (File2) in another SharePoint to my file. I do not want to write anything to File2, jut read.

Can I do this without opening the file? If so how would I reference it?

Thanks in advance!

I can't imagine how you can write to a file without opening it. Anyway, try the sample code below and see if that helps.

Sub OpenAndCloseWBFromSharePointFolder()

'If nobody has the file checked out
If Workbooks.CanCheckOut("http://excel-pc:43231/Shared Documents/ExcelList.xlsb") = True Then
Application.DisplayAlerts = False

'Open the file on the SharePoint server
Workbooks.Open Filename:="http://excel-pc:43231/Shared Documents/ExcelList.xlsb", UpdateLinks:=xlUpdateLinksNever

'Close the workbook
Workbooks("ExcelList.xlsb").Close
Application.DisplayAlerts = True
Else
Application.DisplayAlerts = False

'Open the File to check if you already have it checked out
Workbooks.Open Filename:="http://excel-pc:43231/Shared Documents/ExcelList.xlsb", UpdateLinks:=xlUpdateLinksNever

'See if doc can be checked in
If Application.Workbooks("ExcelList.xlsb").CanCheckIn Then

'Check In, Save and Close
Application.Workbooks("ExcelList.xlsb").CheckIn SaveChanges:=True, Comments:="Checked-In before Delete"

'Open the file again
Workbooks.Open Filename:="http://excel-pc:43231/Shared Documents/ExcelList.xlsb"

'Close the workbook
Workbooks("ExcelList.xlsb").Close
End If
End If

End Sub


Sub UpdateSpecificCells()

'If nobody has the file checked out
If Workbooks.CanCheckOut("http://excel-pc:43231/Shared Documents/ExcelList.xlsb") = True Then
Application.DisplayAlerts = False

'Open the file on the SharePoint server
Workbooks.Open Filename:="http://excel-pc:43231/Shared Documents/ExcelList.xlsb", UpdateLinks:=xlUpdateLinksNever


ActiveSheet.Cells(2, 7).Value = 100
ActiveSheet.Cells(3, 7).Value = 200
ActiveSheet.Cells(4, 7).Value = 300


'Close the workbook
Workbooks("ExcelList.xlsb").Save
Workbooks("ExcelList.xlsb").Close

End If
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM