Using VBA to read the metadata or file properties of files in a SharePoint doc library

三世轮回 提交于 2020-01-13 14:13:29

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/7477269/using-vba-to-read-the-metadata-or-file-properties-of-files-in-a-sharepoint-doc-l

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!