checking empty CustomDocumentProperties in MS Word

I am trying to edit the CustomDocumentProperties in MS Word. I have custom properties with a name and value already in CustomDocumentProperties, however, i want to check for an empty value under the 'Name' section. Looking at the code below, i am creating the actDoc.CustomDocumentProperties and checking that there are properties in there before i enter the If statements.


Dim objProps As Object

        Try
            objProps = actDoc.CustomDocumentProperties



            'covers empty properties
            'if property values are present in word document, do the following...
            If objProps.Count > 0 Then

'If the first property in CustomDocumentProperties exists, this first block of code is passed


                'if name doesn't exist, nor will value, so add both values
                If IsNothing(objProps.Item("EventTitle")) Then
                    objProps.Add(Name:="EventTitle", Value:=strTitle) ' Type:=Office.MsoDocProperties.msoPropertyTypeString, LinkToContent:=False,
                    'elseif name exists and value doesn't
                ElseIf Not IsNothing(objProps.Item("EventTitle").Name) And IsNothing(objProps.Item("EventTitle").Value) Then
                    objProps.Item("EventTitle").Value = strTitle
                Else
                    'both exist so do nothing
                End If

'however, if i have no more properties then the first If statment should be working, and adding the corresponding property values into CustomDocumentProperties, but
'this line of code is skipped and a "false parameter" error appears. I know the reason for this error is because it is checking for "PlantName" as the property name inside
'CustomDocumentProperties, which doesn't exist.


                If IsNothing(objProps.Item("PlantName")) Then
                    objProps.Add(Name:="PlantName", Value:=strPlant)
                ElseIf Not IsNothing(objProps.Item("PlantName").Name) And IsNothing(objProps.Item("PlantName").Value) Then
                    objProps.Item("PlantName").Value = strPlant
                Else
                End If


How do i check for a null property existence using only the built in methods of my objProps Object?
dev-ngps7Asked:
Who is Participating?
 
wguerramConnect With a Mentor Commented:
Write this code between a Try - Catch

Try
   'If this code fails, it means PlantName is Null
   dim strTest as Object
   strTest = objProps.Item("PlantName").Value
catch
   'If an error occurs PlantName will be added
   objProps.Add(Name:="PlantName", Value:=strPlant)
end try

If  IsNothing(objProps.Item("PlantName").Value) Then
   objProps.Item("PlantName").Value = strPlant
End If
0
All Courses

From novice to tech pro — start learning today.