首页 文章

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

提问于
浏览
3

我在SharePoint网站上有几百个Word模板(DOTX) . 许多用户团队使用这些模板 .

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

当文档中断时,我需要在SharePoint上重新 Build 回到正确模板的链接 . 以编程方式执行此操作是有意义的,因此我可以将解决方案分发给我的团队 .

我想为每个模板文件提供一个唯一的模板ID(一个三位数字),存储在元数据或自定义文件属性中 . 从模板生成新文档时,模板ID会自动转移到文档中,以便进行设置 . 现在,我只需要使用VBA扫描SharePoint文档库中的模板文件以获取匹配的模板ID . 当找到它时,我可以重新 Build 链接,一切都很顺利 .

我基本上是在找这个:

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) . 虽然为我工作,所以我想我会分享 .

1 回答

  • 0

    我首先从VBA调用SharePoint web services . 在那里,您可以调用GetListItems,它将直接使用正确的TemplateID属性撤回文档 .

相关问题