In that case, simply:
Sub test_CustomDOcumentProperties()
Dim stgCompany As String, tf As Boolean
stgCompany = "ACME"
MsgBox ThisWorkbook.CustomDocumentProperties.Count
tf = cdpExists(ThisWorkbook, "Company")
If tf Then
MsgBox "Company Exists as a Custom Document Property in ThisWorkbook", vbInformation, "Company: " & ThisWorkbook.CustomDocumentProperties("Company").Value
Else
ThisWorkbook.CustomDocumentProperties.Add "Company", False, msoPropertyTypeString, stgCompany
End If
End Sub
Function cdpExists(aWorkbook As Workbook, cdpName As String) As Boolean
Dim cdp As Object
For Each cdp In ThisWorkbook.CustomDocumentProperties
If LCase(cdp.Name) = LCase(cdpName) Then
cdpExists = True
Exit Function
End If
Next cdp
End Function
The advantage to closed file processing is that it is faster. The file can be open for some routines as is this case. If you use Chip's routines, you will need to delete some "DSOFile." prefix parts. When you Compile, you can see which ones.
e.g.
Sub Test_ReadPropertyFromClosedFile()
Dim s As Variant
s = ReadPropertyFromClosedFile(ThisWorkbook.FullName, "Company", PropertyLocationCustom)
If IsNull(s) Then s = ""
MsgBox "Company: " & s
End Sub
Bookmarks