测试属性名称是否存在 [英] Test whether a property name exists

查看:149
本文介绍了测试属性名称是否存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到此错误:


需要运行时错误'424'对象

Run-time error '424' object required

当我尝试运行这段代码:

when I try to run this code:

Sub SuperSaveAs()
Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim pathName As String
Dim myFileName As String

If (ActiveDocument.CustomDocumentProperties("_CheckOutSrcUrl").Value = True) Then
    pathName = ActiveDocument.CustomDocumentProperties("_CheckOutSrcUrl").Value
    myFileName = pathName + ActiveWorkbook.Name
        ActiveWorkbook.SaveAs Filename:= _
            myFileName _
            , FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
Else
    MsgBox "_CheckOutSrcUrl is missing"
End If

End Sub

此宏与Excel中的按钮连接。宏检查自定义文档属性是否存在。如果自定义文档属性存在,则宏应将文件保存为 _CheckOutSrcUrl (SharePoint Directory)的值。
我如何解决错误?

This macro is connected with a button in Excel. The macro checks if the custom document property exists. If the custom document property exists the macro should save the file to the Value of _CheckOutSrcUrl (SharePoint Directory). How can I fix the error?

推荐答案

您不能使用上述方法来测试属性名称是否存在或不。有两种明显的方法,这些不是我自己的个人答案:

You cannot use the above method to test whether a property name exists or not. There are two apparent approaches, and these are not my own personal answers:


  1. 使用循环来检查所有的属性名称和看看是否找到_CheckOutSrcUrl。请参阅 https://answers.microsoft.com/en-us/office/forum/office_2007-word/using-customdocumentproperties-with-vba/91ef15eb-b089-4c9b-a8a7-1685d073fb9f

使用VBA错误检测来查看属性_CheckOutSrcUrl是否存在。请参阅 http://www.vbaexpress.com/forum/showthread。 php?15366-Solved-CustomDocumentProperties-Problem

Use VBA error detection to see if the property "_CheckOutSrcUrl" exists. See http://www.vbaexpress.com/forum/showthread.php?15366-Solved-CustomDocumentProperties-Problem

适合您代码的#1的代码段示例 - 最好在一个函数中:

A snippet example of #1 adapted to your code - would be best in a function:

Dim propertyExists As Boolean
Dim prop As DocumentProperty
propertyExists = False
For Each prop In ActiveDocument.CustomDocumentProperties
    If prop.Name = "_CheckOutSrcUrl" Then
        propertyExists = True
        Exit For
    End If
Next prop

适用于您的代码的#2的代码段示例:

A snippet example of #2 adapted to your code:

Dim propertyExists As Boolean
Dim tempObj
On Error Resume Next
Set tempObj = ActiveDocument.CustomDocumentProperties.Item("_CheckOutSrcUrl")
propertyExists = (Err = 0)
On Error Goto 0

这篇关于测试属性名称是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆