dsolefile - 当值不存在时的错误处理(在excel中)

时间:2009-04-03 10:15:38

标签: excel vba xls

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

然而 这种类型的东西不起作用

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

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

如果我在那里放了一个错误处理程序 barfs被锁定或丢失连接

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

2 个答案:

答案 0 :(得分:0)

您是否可以将CustomDocumentProperties集合用于相应的Excel工作簿?然后,您可以遍历集合并编辑属性(如果找到它)。如果它不存在,则可以创建属性

答案 1 :(得分: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