A
Alex
About two years ago, I posted a request on this group
asking for help creating the VBA code used on a userform
to check and create custom word properties.
The nice people at MVP provided me with the following; the
code looks at the textbox name then compares it with the
custom properties of the document. If a property with the
same name exists then it modifies the property with the
contents of the textbox. If the property does not exist
then it creates a new custom property and uses the textbox
name as the property name (See below code):
Private Sub OK_Click()
'Assumptions:
'You are using the same names for the textboxes and for
the Documentproperties,
'example: Client is the name of the textbox and Client is
the name of the doc property
Dim oDoc As Document
Dim oControl As Control
Dim strControl As String
Set oDoc = ActiveDocument
For Each oControl In Me.Controls 'you can omit Me.
If LCase(TypeName(oControl)) = "textbox" Then
strControl = oControl.Name
If DoesDocPropExist(strControl) = False Then
oDoc.CustomDocumentProperties.Add _
Name:=strControl, _
LinkToContent:=False, _
Value:="<empty>", _
Type:=msoPropertyTypeString
End If
If Not oControl.Value = "" Then
oDoc.CustomDocumentProperties
(strControl).Value = oControl.Value
End If
End If
Next
Unload Me
End Sub
Private Function DoesDocPropExist(DocPropName As String)
As Boolean
On Error GoTo DoesNotExist
'Source Perry
'Check if the passed name matches an existing property
'Results in true if the property exists
DoesDocPropExist = _
(DocPropName =
ActiveDocument.CustomDocumentProperties(DocPropName).Name)
'Exit here is prop exists
Exit Function
DoesNotExist:
Err.Clear
End Function
What I need help with now is, I need to modify the code so
that it not only checks the custom properties but it also
checks the normal document properties.
I would like to keep the current functionality whereby it
uses the textbox name as the property name, thus one piece
of code can be reused on different forms.
If anyone can help, I would be most grateful
Regards
Alex
asking for help creating the VBA code used on a userform
to check and create custom word properties.
The nice people at MVP provided me with the following; the
code looks at the textbox name then compares it with the
custom properties of the document. If a property with the
same name exists then it modifies the property with the
contents of the textbox. If the property does not exist
then it creates a new custom property and uses the textbox
name as the property name (See below code):
Private Sub OK_Click()
'Assumptions:
'You are using the same names for the textboxes and for
the Documentproperties,
'example: Client is the name of the textbox and Client is
the name of the doc property
Dim oDoc As Document
Dim oControl As Control
Dim strControl As String
Set oDoc = ActiveDocument
For Each oControl In Me.Controls 'you can omit Me.
If LCase(TypeName(oControl)) = "textbox" Then
strControl = oControl.Name
If DoesDocPropExist(strControl) = False Then
oDoc.CustomDocumentProperties.Add _
Name:=strControl, _
LinkToContent:=False, _
Value:="<empty>", _
Type:=msoPropertyTypeString
End If
If Not oControl.Value = "" Then
oDoc.CustomDocumentProperties
(strControl).Value = oControl.Value
End If
End If
Next
Unload Me
End Sub
Private Function DoesDocPropExist(DocPropName As String)
As Boolean
On Error GoTo DoesNotExist
'Source Perry
'Check if the passed name matches an existing property
'Results in true if the property exists
DoesDocPropExist = _
(DocPropName =
ActiveDocument.CustomDocumentProperties(DocPropName).Name)
'Exit here is prop exists
Exit Function
DoesNotExist:
Err.Clear
End Function
What I need help with now is, I need to modify the code so
that it not only checks the custom properties but it also
checks the normal document properties.
I would like to keep the current functionality whereby it
uses the textbox name as the property name, thus one piece
of code can be reused on different forms.
If anyone can help, I would be most grateful
Regards
Alex