2011-09-19 85 views
3

我在SharePoint网站上有几百个Word模板(DOTX)。许多用户团队都使用这些模板。使用VBA读取SharePoint文档库中文件的元数据或文件属性

当用户需要自定义此文档时,他们点击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)。尽管为我工作,所以我想我会分享。

+0

只要确保不要用空格创建模板ID,例如“模板ID”,因为它被转换为内部名称的“Template_x0020_ID” – Nat

回答

相关问题