VBA设置自定义文档属性
我有一些麻烦,我相信是一个快速解决。 我有一个与“脚本状态”的自定义文档属性字段的Excel文档。 脚本状态属性来自从其下载文档的文档库中的列。 我的目标是让用户下载表单,完成他们分配的任务,然后有一个“BeforeSave”macros运行,它将扫描工作并基于macros的结果更新脚本状态属性(即,如果字段丢失,脚本会说“未完成”等)。 在SharePoint中,这是一个选项下拉框,其选项包括:未分配,已分配,未完成,已完成/已通过,失败,重新testing和延期。 我有检查工作集的逻辑和工作正常,而不是如何更新属性字段。 到目前为止我所拥有的仅仅是:
Application.ThisWorkbook.CustomDocumentProperties.Item("Script Status").Value = "Fail"
一旦运行,我会在提示“无效的程序调用或争议”时发生错误。 我试图研究这条线的正确的语法,但一直无济于事。 任何帮助将不胜感激!
从Sharepoint文件可能会有一些怪异的,不可否认,这不是我熟悉的,但从阅读其他线程我知道这些文件有一些困难。 在这里可能会也可能不是这样。
无论如何,我们可以尝试诊断,也许我们会解决这个问题。
正如我在评论中提到的,如果在工作簿中不存在指定的DocumentProperty
(“脚本状态”),我可以复制此错误。 这可能就像打字错误一样容易。 您可以使用此函数来testing是否存在已命名的DocumentProperty
:
Function CustomPropertyExists(propName As String) As Boolean Dim wb As Workbook Dim docProp As DocumentProperty Dim propExists As Boolean Set wb = Application.ThisWorkbook For Each docProp In wb.CustomDocumentProperties If docProp.Name = propName Then propExists = True Exit For End If Next CustomPropertyExists = propExists End Function
如果你把它放在一个标准模块中,你可以从Worksheet调用这个函数,如下所示:
=CustomPropertyExists("Script Status")
,它将返回True
或False
的值,取决于是否find指定的属性。
你可以从子程序调用它,例如:
If CustomPropertyExists("Script Status") Then MsgBox "Exists!",vbInformation Else MsgBox "Does not exist", vbCritical End If
看来,与Excel文档关联的Sharepoint属性既不是一个CustomDocumentProperty也不是一个BuiltInDocumentProperty。 在涉及更多的代码时,Sharepoint字段是“ContentTypeProperty”。 使用ContentTypeProperty代替自定义属性的原始问题中发布相同的代码,代码工作成功。
请引用David的代码,以确定您的“属性”是否是真正的自定义属性而不是内容types。 这非常有帮助!
我不知道这是否会帮助任何人,但我正在努力将.xlsm文件保存到已设置了“自定义属性”的Sharepoint站点。 我找不到解决scheme,但我设法使用这个解决scheme。
Private Sub SaveToSharePoint() 'set the save location of the document and name Dim FolderPath As String: FolderPath = "//sharepoint.com/sites/Shared Documents/" Dim Type As String: Type = "Doc" Dim CurrentYear As String: CurrentYear = CStr(Year(Date)) Dim FileName As String: FileName = Type & "_" & CurrentYear & ".xlsm" Dim FullFilePath As String: FullFilePath = FolderPath + FileName 'Creates the initial file and saves it as .xlsm On Error Resume Next ThisWorkbook.SaveAs FullFilePath, FileFormat:=52 'Sets the custom properties ThisWorkbook.ContentTypeProperties("Type").Value = Type ThisWorkbook.ContentTypeProperties("Year").Value = CurrentYear 'Updates the file On Error Resume Next ThisWorkbook.SaveAs FullFilePath, FileFormat:=52 End Sub