Private objDoc As Object
Private objApp As Object
Private strgetPropName As String
Private varProperty As Variant
Private vargetPropType As MsoDocProperties
Private varCurrentgetPropValue As Variant
Private varNewgetPropValue As Variant
Private newDataTypeDate As Boolean
Private bolExists As Boolean
Private Sub Class_Initialize()
bolExists = False
End Sub
Private Sub Class_Terminate()
End Sub
Public Property Let setDoc(objDocument As Object)
Set objDoc = objDocument
Set objApp = objDocument.Application
End Property
Public Property Let setPropName(strPropName As String)
strgetPropName = strPropName
bolExists = testExists()
End Property
Public Property Let setNewPropValue(varData)
varNewgetPropValue = varData
vargetPropType = DatumMSO
End Property
Public Property Get getExists() As Boolean
getExists = bolExists
End Property
Private Property Get testExists() As Boolean
Dim prop As Variant
testExists = False
For Each prop In objDoc.CustomDocumentProperties
If LCase(prop.Name) = LCase(strgetPropName) Then
testExists = True
Set varProperty = prop
With varProperty
vargetPropType = .Type
varCurrentgetPropValue = .Value
End With
Exit For
End If
Next
End Property
Public Property Get getPropName() As String
getPropName = strgetPropName
End Property
Public Property Get getPropValue() As String
If bolExists Then
getPropValue = varCurrentgetPropValue
Else
getPropValue = ""
End If
End Property
Public Property Get getPropType() As String
If bolExists Then
getPropType = vargetPropType
Else
getPropType = ""
End If
End Property
Private Property Get getNewPropValue()
getNewPropValue = varNewgetPropValue
End Property
Private Property Get isgetNewValueTypeString() As Boolean
isgetNewValueTypeString = (TypeName(varNewgetPropValue) = "String")
End Property
Private Property Get isgetNewValueTypeDate() As Boolean
isgetNewValueTypeDate = (TypeName(varNewgetPropValue) = "Date")
End Property
Private Property Get isgetNewValueTypeNumber() As Boolean
isgetNewValueTypeNumber = (TypeName(varNewgetPropValue) = "Integer")
End Property
Private Property Get isgetNewValueTypeFloat() As Boolean
isgetNewValueTypeFloat = ((TypeName(varNewgetPropValue) = "Double") Or (TypeName(varNewgetPropValue) = "Single"))
End Property
Private Property Get isgetNewValueTypeBoolean() As Boolean
isgetNewValueTypeBoolean = (TypeName(varNewgetPropValue) = "Boolean")
End Property
Private Property Get getNewValueType() As String
getNewValueType = TypeName(varNewgetPropValue)
End Property
Private Property Get DatumMSO() As MsoDocProperties
If isgetNewValueTypeString Then
DatumMSO = msoPropertyTypeString
ElseIf isgetNewValueTypeDate Then
DatumMSO = msoPropertyTypeDate
ElseIf isgetNewValueTypeNumber Then
DatumMSO = msoPropertyTypeNumber
ElseIf isgetNewValueTypeFloat Then
DatumMSO = msoPropertyTypeFloat
ElseIf isgetNewValueTypeBoolean Then
DatumMSO = msoPropertyTypeBoolean
End If
End Property
Sub propDelete()
If bolExists Then
varProperty.Delete
bolExists = testExists()
End If
End Sub
Sub propUpdate()
If bolExists Then
If vargetPropType = DatumMSO Then
varProperty.Value = getNewPropValue
End If
End If
End Sub
Sub propWrite()
If bolExists Then
If vargetPropType = DatumMSO Then
varProperty.Value = getNewPropValue
Else
propDelete
propAdd
End If
Else
propAdd
End If
End Sub
Sub propAdd()
If Not bolExists Then
objDoc.CustomDocumentProperties.Add Name:=strgetPropName, _
LinkToContent:=False, Type:=DatumMSO, Value:=varNewgetPropValue
bolExists = testExists()
End If
End Sub
Option Explicit
Sub test1()
Dim clsCDP1 As clsCustomDocumentProperty
Dim arr As Variant
Dim itm As Variant
Dim entity As Object
Set entity = GetObject(, "word.application").documents(1)
Set clsCDP1 = New clsCustomDocumentProperty
clsCDP1.setDoc = entity
clsCDP1.setPropName = "fred"
Debug.Print clsCDP1.getPropName & " Exists :> " & clsCDP1.getExists
With New clsCustomDocumentProperty
.setDoc = entity
.setPropName = "fred"
.setNewPropValue = "Temp val!"
.propAdd
Debug.Print .getPropName & " Exists :> " & .getExists
.propDelete
Debug.Print .getPropName & " Exists :> " & .getExists
End With
arr = Array(22, 22.1, "fred", CDate("22 Feb 1922"), True)
For Each itm In arr
With New clsCustomDocumentProperty
.setDoc = entity
.setPropName = "fred"
.setNewPropValue = itm
.propAdd
.propUpdate
Debug.Print "'" & .getPropName & "' New data type (" & itm & ") updated? :> " & (.getPropValue = itm)
End With
Next
arr = Array(22, 22.1, "fred", CDate("22 Feb 1922"), True)
For Each itm In arr
With New clsCustomDocumentProperty
.setDoc = entity
.setPropName = "fred"
.setNewPropValue = itm
.propWrite
Debug.Print "'" & .getPropName & "' New data type (" & itm & ") force written? :> " & (.getPropValue = itm)
End With
Next
With New clsCustomDocumentProperty
.setDoc = entity
.setPropName = "fred"
.propDelete
End With
Set entity = CreateObject("excel.application")
entity.workbooks.Add
entity.Visible = True
With New clsCustomDocumentProperty
.setDoc = entity.workbooks(1)
.setPropName = "fred"
.setNewPropValue = "Temp val!"
.propAdd
Debug.Print .getPropName & " Exists :> " & .getExists
.propDelete
Debug.Print .getPropName & " Exists :> " & .getExists
End With
entity.Application.displayalerts = False
entity.Quit
Set entity = CreateObject("powerpoint.application")
entity.presentations.Add
entity.Visible = True
With New clsCustomDocumentProperty
.setDoc = entity.presentations(1)
.setPropName = "fred"
.setNewPropValue = "Temp val!"
.propAdd
Debug.Print .getPropName & " Exists :> " & .getExists
.propDelete
Debug.Print .getPropName & " Exists :> " & .getExists
End With
' entity.Application.displayalerts = False
entity.Quit
End Sub
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (1)
Commented: