我收到此错误:
运行时错误' 424'需要的对象
当我尝试运行此代码时:
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目录)。
我该如何修复错误?
答案 0 :(得分:9)
您无法使用上述方法来测试属性名称是否存在。有两种明显的方法,这些不是我个人的答案:
使用循环检查所有属性名称,并查看是否找到“_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
适用于您的代码的#1代码段示例 - 在函数中最佳:
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的代码段示例适用于您的代码:
Dim propertyExists As Boolean
Dim tempObj
On Error Resume Next
Set tempObj = ActiveDocument.CustomDocumentProperties.Item("_CheckOutSrcUrl")
propertyExists = (Err = 0)
On Error Goto 0
答案 1 :(得分:0)
基于@Cybermike:
Function propertyExists(propName) As Boolean
Dim tempObj
On Error Resume Next
Set tempObj = ActiveDocument.CustomDocumentProperties.Item(propName)
propertyExists = (Err = 0)
On Error GoTo 0
End Function