使用VBA读取SharePoint Doc库中文件的元数据或文件属性

发布于 2024-12-05 07:15:22 字数 1766 浏览 1 评论 0原文

我的 SharePoint 网站上有数百个 Word 模板 (DOTX)。许多用户团队都使用这些模板。

当用户需要自定义此文档时,他们可以单击 SharePoint 上的特殊链接,从他们选择的模板生成新文档 (DOCX)。此新文档文件始终需要“链接”回其在 SharePoint 上的模板文件。如果文档丢失该链接,它将无法正常工作并被视为“已损坏”。

当文档损坏时,我需要重新建立指向 SharePoint 上正确模板的链接。以编程方式执行此操作是有意义的,这样我就可以将解决方案分发给我的团队。

我想为每个模板文件提供一个唯一的模板 ID(三位数),存储在元数据或自定义文件属性中。当从模板生成新文档时,模板 ID 会自动转移到文档中,这样就完成了设置。现在我只需要使用VBA扫描SharePoint文档库中的模板文件来查找匹配的模板ID。找到后,我可以重新建立链接,一切都很好。

我基本上正在寻找这个:

Sub DocFixer()

Dim objTemplate as Template
Dim objBrokenDoc as Document

Set objBrokenDoc = ActiveDocument

For each objTemplate in "\\SharePoint\Template Library\".Templates
    If objTemplate.Properties("Template ID").Value = objBrokenDoc.Properties("Template ID").Value Then
        objBrokenDoc.AttachedTemplate = objTemplate.Path
        Exit For
    End If
Next

End Sub

…但是我在使用 VBA 读取 SharePoint 文档库内容时遇到了麻烦,而无需实际打开内容,因为使用如此多的模板会花费太长时间,而且对用户来说非常具有破坏性。

有什么想法吗?你能指出我正确的方向吗?

编辑:这是我的解决方案:

Sub Macro()

Dim FSO As Object
Set FSO = CreateObject("Scripting.FileSystemObject")

Dim objFile As Object
Dim objDSO As Object

For Each objFile In FSO.GetFolder("\\SharePoint\doc lib\").Files
    Set objDSO = CreateObject("DSOFile.OleDocumentProperties")
    objDSO.Open objFile.Path

    If objDSO.CustomProperties.Item("Template_ID") = ActiveDocument.CustomDocumentProperties("Template_ID").Value Then
        ActiveDocument.AttachedTemplate = objFile.Path
        End
    End If
Next

MsgBox ("No matching template found. Please attach the proper template manually."), vbCritical

End Sub

显然这会利用 DSOFile.dll (http://technet.microsoft.com/en-us/library/ee692828.aspx),但我不必添加引用?对那部分仍然很困惑。

此外,这可能无法通过 https:// (SSL) 运行。不过为我工作,所以我想我会分享。

I have a few hundred Word templates (DOTX) on a SharePoint site. Many teams of users work with these templates.

When a user needs to customize this documentation, they click a special link on SharePoint to generate a new document (DOCX) from the template they choose. This new document file always needs to be "linked" back to its template file on SharePoint. If the document loses that link, it won’t work correctly and is considered “broken”.

When documents break, I need to re-establish the link back to the right template on SharePoint. It makes sense to do this programmatically so I can distribute the solution to my team.

I want to give each template file a unique Template ID (a three-digit number), stored in metadata or a custom file property. When new documents are generated from the templates, the Template ID automatically carries over into the document, so that’s set. Now I just need to use VBA to scan the template files in the SharePoint document library for the matching Template ID. When that’s found, I can re-establish the link and all is well.

I’m basically looking for this:

Sub DocFixer()

Dim objTemplate as Template
Dim objBrokenDoc as Document

Set objBrokenDoc = ActiveDocument

For each objTemplate in "\\SharePoint\Template Library\".Templates
    If objTemplate.Properties("Template ID").Value = objBrokenDoc.Properties("Template ID").Value Then
        objBrokenDoc.AttachedTemplate = objTemplate.Path
        Exit For
    End If
Next

End Sub

…but I’m having trouble using VBA to read SharePoint doc library contents without actually opening the contents, as that takes far too long with so many templates, plus its very disruptive for the user.

Any ideas? Could you point me in the right direction?

Edit: Here's my solution:

Sub Macro()

Dim FSO As Object
Set FSO = CreateObject("Scripting.FileSystemObject")

Dim objFile As Object
Dim objDSO As Object

For Each objFile In FSO.GetFolder("\\SharePoint\doc lib\").Files
    Set objDSO = CreateObject("DSOFile.OleDocumentProperties")
    objDSO.Open objFile.Path

    If objDSO.CustomProperties.Item("Template_ID") = ActiveDocument.CustomDocumentProperties("Template_ID").Value Then
        ActiveDocument.AttachedTemplate = objFile.Path
        End
    End If
Next

MsgBox ("No matching template found. Please attach the proper template manually."), vbCritical

End Sub

Apparently this taps into DSOFile.dll (http://technet.microsoft.com/en-us/library/ee692828.aspx), but I didn't have to add the reference? Still confused on that part.

Also, this might not work over https:// (SSL). Worked for me though, so I thought I'd share.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陪我终i 2024-12-12 07:15:22

我首先从以下位置调用 SharePoint Web 服务 VBA。
到达那里后,您可以拨打 GetListItems 将直接拉回具有正确 TemplateID 属性的文档。

I would start by calling the SharePoint web services from VBA.
Once there you can make a call to GetListItems that will pull back the document with the correct TemplateID attribute directly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文