0

我正在尝试更新文档属性并创建新条目(如果它们不存在)

但是这种类型的东西不起作用

Set objDocProps = DSO.GetDocumentProperties(sfilename:=FileName)

With objDocProps
If .CustomProperties("ABC") Is Nothing Then
'create it here

如果我在那里放置一个错误处理程序,它会被锁定或失去连接

errhandler:
Select Case Err.Number
 Case -2147220987 ' missing custom property
 Debug.Print "missing custom property"
 With objDocProps
     .CustomProperties("ABC").Value = "banana!"
4

2 回答 2

0

您可以将CustomDocumentProperties集合用于适当的 Excel 工作簿吗?然后,您可以遍历集合并在找到该属性时对其进行编辑。如果它不存在,您可以创建该属性

于 2009-04-05T00:58:02.103 回答
0

尝试按名称访问 CustomProperties 时似乎存在问题。

我实现的解决方案是迭代 CustomPropery 集合以确定项目的索引(如果存在),然后使用它来设置值(或者如果不存在则添加一个新的)

传入:您的自定义属性对象,您希望填充的条目以及您希望填充它的值

Sub UpsertEntry(objCustomProps, entryname, entryvalue)
  'update the custom property with value supplied
  On Error Resume Next

  Dim icount 
  Dim iindex 

  For icount = 1 To objCustomProps.Count

    If objCustomProps.Item(icount).name = entryname Then
      iindex = icount
      Exit For
    Else
      iindex = 0
    End If

  Next


  If iindex = 0 Then 'no custom property found

   objCustomProps.Add entryname, entryvalue
   Wscript.Echo " Adding   [" & entryname & ":" & entryvalue & "]"
  Else
   objCustomProps.Item(iindex).Value = entryvalue
   Wscript.Echo " Changing [" & entryname & ":" & entryvalue & "]"

  End If
  On Error GoTo 0


End Sub
于 2009-04-21T11:49:51.127 回答