VBA Code for a Word Properties Userform

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
 
H

Helmut Weber

Hi Alex,
impossible in a way, as I would be very surprised,
if one could change the names of BuiltInDocumentProperties.
If you would like to have an overview of all the values
of all DocumentProperties, I think, you have to modify
the existent form.

Greetings from Bavaria, Germany
Helmut Weber
"red.sys" & chr$(64) & "t-online.de"
Word 97, XP, NT4.0, W98
 
D

DeeJee

The code in this article should point you in the right direction
How to use a single VBA procedure to read or write both custom and built-in
Document Properties

Most of the string and date BuiltInDocumentProperties can't be changed.
Last author, Creation date ...
Is there a way arround to reset those values?
 
J

Jonathan West

DeeJee said:
Most of the string and date BuiltInDocumentProperties can't be changed.
Last author, Creation date ...
Is there a way arround to reset those values?

No, nothing you can do about that, short of creating a fresh file based on
the same template, and transferring the existing content to it.

--
Regards
Jonathan West - Word MVP
MultiLinker - Automated generation of hyperlinks in Word
Conversion to PDF & HTML
http://www.multilinker.com
 
A

Alex

Hi Guys

Thanks for the replies, I had looked at the MVP articles
before, and even though I understand the theory, putting
it into practice in conjunction with a userform seemed to
be beyond me.

After reading Helmut's comment's I realised I hadn't made
my first post too clear.

What I need from the userform is;

1. The custom property code should work as it does.
2. I then need to add two more textboxes to my user form,
one will place it's contents as the value for the 'Title'
document property nad the other will place it's contents
as the value for the 'Subject' document property.

So what I need is help with the code for part 2 and if
possible instructions on where to place the code, i.e.
does it get placed under the userforms OK_Click function
or does it get placed elsewhere?

thanks in advance

Alex
 
J

Jonathan West

Alex said:
Hi Guys

Thanks for the replies, I had looked at the MVP articles
before, and even though I understand the theory, putting
it into practice in conjunction with a userform seemed to
be beyond me.

After reading Helmut's comment's I realised I hadn't made
my first post too clear.

What I need from the userform is;

1. The custom property code should work as it does.
2. I then need to add two more textboxes to my user form,
one will place it's contents as the value for the 'Title'
document property nad the other will place it's contents
as the value for the 'Subject' document property.

OK, you need to put a bit of extra code round these lines

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

I'm assuming that your two controls are called "Title" and "Subject"
respectively, in which case you can change the code as follows

If strControl = "Title" or strControl = "Subject" Then
oDoc.BuiltInDocumentProperties(strControl).Value =
oControl.Value
Else
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

--
Regards
Jonathan West - Word MVP
MultiLinker - Automated generation of hyperlinks in Word
Conversion to PDF & HTML
http://www.multilinker.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top