简体   繁体   English

从SharePoint上的Excel文件读取和复制信息

[英]Read and copy info from excel file on sharepoint

I am working writing VBA code in one SharePoint2016 excel file (File1) for task automation. 我正在一个任务中将VBA代码编写到一个SharePoint2016 Excel文件(File1)中。 Part of the task is to ready and copy information from another excel file (File2) in another SharePoint to my file. 部分任务是准备信息并将信息从另一个SharePoint中的另一个Excel文件(File2)复制到我的文件中。 I do not want to write anything to File2, jut read. 我不想向File2写任何东西,请继续阅读。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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