简体   繁体   English

如何从 Excel vba 以编辑模式在 SharePoint 上打开启用宏的 Excel 文件?

[英]How to open a macro enabled Excel file on a SharePoint in edit mode from an Excel vba?

I have an macro enabled Excel file on a SharePoint that when a user opens it from the SharePoint the file opens programmatically another macro enabled Excel file on the same SharePoint.我在 SharePoint 上有一个启用宏的 Excel 文件,当用户从 SharePoint 打开它时,该文件会以编程方式打开同一 SharePoint 上另一个启用宏的 Excel 文件。 The file being opened by the vba macro needs to be edited, and must be editable by multiple users at the same time. vba 宏打开的文件需要编辑,并且必须可以被多个用户同时编辑。 However I can't get it to even open in edit mode.但是我什至无法在编辑模式下打开它。 Using Office 365使用 Office 365

I've tried << ActiveWorkbook.LockServerFile >> but always get an error message << Run-time error 1004: Method 'LockServer' of object'_Workbook' failed >>.我试过 << ActiveWorkbook.LockServerFile >> 但总是收到错误消息 << Run-time error 1004: Method 'LockServer' of object'_Workbook' failed >>。

The code that I show below is in the Excel file that is opened manually by the user and that opens automatically the other Excel file.我在下面显示的代码位于由用户手动打开并自动打开其他 Excel 文件的 Excel 文件中。 The other Excel file when opened works fine (if I remove the LockServerFile command), all it's macro's work fine, but it is open in read only and changes cannot be saved.打开时的另一个 Excel 文件工作正常(如果我删除 LockServerFile 命令),所有宏都可以正常工作,但它以只读方式打开,无法保存更改。 Again this file should be editable by multiple users simultaneously.同样,此文件应可由多个用户同时编辑。

' this code is in the "ThisWorkbook" tab ' 此代码位于“ThisWorkbook”选项卡中

Sub workbook_open()子工作簿_open()

Set DB = Workbooks.Open(DBname, 3, False, , , , True) Set DB = Workbooks.Open(DBname, 3, False, , , , True)

ActiveWorkbook.LockServerFile ' this is where is crashes ActiveWorkbook.LockServerFile '这是崩溃的地方

'more code... '更多代码...

End Sub结束子

' Note: DB is declared in a module ' 注意:DB 是在模块中声明的

Public DB as Workbook Public Const DBname As String = " https://ledvance365.sharepoint.com ... .xlsm" Public DB 作为工作簿 Public Const DBname As String = " https://ledvance365.sharepoint.com ... .xlsm"

Looks like << ActiveWorkbook.LockServerFile >> wasn't working because the SharePoint settings was not on "Open Documents in Client Applications by Default" 看起来<< ActiveWorkbook.LockServerFile >>无法正常工作,因为SharePoint设置不在“默认情况下在客户端应用程序中打开文档”

But once I got the SharePoint owner to change the SharePoint settings to "Open Documents in Client Applications by Default" the << ActiveWorkbook.LockServerFile >> command worked. 但是一旦我让SharePoint所有者将SharePoint设置更改为“默认情况下在客户端应用程序中打开文档”,<< ActiveWorkbook.LockServerFile >>命令就起作用了。

Maybe check out the following link to check if the file is already locked. 也许检查以下链接以检查文件是否已被锁定。 https://www.mrexcel.com/forum/excel-questions/906983-vba-support-checking-if-file-locked-sharepoint.html https://www.mrexcel.com/forum/excel-questions/906983-vba-support-checking-if-file-locked-sharepoint.html

Also in general it helps if you use your objects when you set them. 通常,如果在设置对象时使用对象,它也会有所帮助。

Dim DB as Workbook
Set DB = Workbooks.Open(filename:=DBname, editable:=True)
DB.LockServerFile

I had a similar issue.我有一个类似的问题。 The code crashes at the same point you highlighted.代码在您突出显示的同一点崩溃。

ActiveWorkbook.LockServerFile

Seems to fail when the document is already editable.当文档已经可编辑时似乎失败。

On Error Resume Next
    ActiveWorkbook.LockServerFile

Fixes the issue I was experiencing since the code will continue on when the file is already editable and similarly makes a file editable if it wasn't previously.修复了我遇到的问题,因为当文件已经可编辑时代码将继续运行,并且如果文件以前不是可编辑的,则类似地使文件可编辑。

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

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