Log in

View Full Version : [SOLVED:] Add All Custom Document Properties to a Combobox



grahampleigh
03-07-2018, 08:44 AM
Hi everyone, new to the forum.

I use VB a lot with my work and have created several .dotm for frequently used documents. Typically I use custom document properties (entered via a userform) to store simple information such as document number and title but also more specific information for each document type.

The userform can be launched and the specific properties changed by entering a new value in a text box. The trouble with this is that each text box in my userform is specifically linked to one custom document property, for example:



Dim DP As DocumentProperties
Set DP = ActiveDocument.CustomDocumentProperties

DP("Document Title").Value = DocumentTitleTxt.Value

The textbox is inherently linked to the specific "Document Title" property.

I would like to be able to launch a user form and have EVERY custom document property (name) contained within the active document added to a combobox via the .AddItem method. Then, a single text box would display the value of the property currently selected from the combobox.

Is this possible? This is something I would use as it would be much easier and quicker and apply to any word document that has a custom document property.

I'm thinking a simple For...Next type arrangement but I just can't define the 'unknown' document property. Something like:


Private Sub UserForm_Initialize()
Dim DP As DocumentProperties
Set DP = ActiveDocument.CustomDocumentProperties


For Each DP In ActiveDocument

ComboBox1.AddItem DP().Name
Next


End Sub

but obviously the above doesn't work... Would really appreciate any insight you may have.
Thanks

gmaxey
03-07-2018, 10:38 AM
Private Sub ComboBox1_Change()
TextBox1.Text = vbNullString
On Error Resume Next
TextBox1.Text = ActiveDocument.CustomDocumentProperties(ComboBox1.Value).Value
On Error GoTo 0
End Sub
Private Sub UserForm_Initialize()
Dim oProp As DocumentProperty
For Each oProp In ActiveDocument.CustomDocumentProperties
ComboBox1.AddItem oProp.Name
Next
End Sub

SamT
03-07-2018, 12:28 PM
The Combo Box idea requires the User to select each item in the combo box.

I would create a second UserForm, FrmDocProps, Loaded from the main form's Initialize sub.

It's own Initilaize sub would create, for each CustomDocumentProperty, a set of three controls:
A Label with the Property Name, A Textbox with the Property Value, and an Option button.

The User would either reset the value in the Textbox, or check the option button (To show that the property was noticed.) The Option button could also be checked by the TextBox.Exit Event. The changed Property Values can easily be entered into the Doc. This would require an OnEvents Class for the TextBoxes.

The frmDocProps Initialize sub could handle such things as incrementing the Doc Number, and would Show the Form at its end.

A simple loop, For i = 1 to CustomDocumentProperties.Count, can handle all control Names. "Lbl" & i, "TBox" & i, and "OpBut" & i.


Lots of work, but a pleasant User Experience

grahampleigh
03-07-2018, 09:08 PM
Private Sub ComboBox1_Change()
TextBox1.Text = vbNullString
On Error Resume Next
TextBox1.Text = ActiveDocument.CustomDocumentProperties(ComboBox1.Value).Value
On Error GoTo 0
End Sub
Private Sub UserForm_Initialize()
Dim oProp As DocumentProperty
For Each oProp In ActiveDocument.CustomDocumentProperties
ComboBox1.AddItem oProp.Name
Next
End Sub


Perfect, thank you. Does exactly what I needed - and so simple, not sure why I couldn't figure that out!

grahampleigh
03-07-2018, 09:11 PM
SamT - thanks for the response.

The above was a simplified example of what I was trying to achieve, but you're correct, ultimately the user form will list all of the current custom document properties in the document, allow the user to select and change the value of a property (if they want to), to insert that property field back into the current document and also create a new custom property if it doesn't already exist.

Work in progress but I'll share the final result if it will help others.

SamT
03-08-2018, 08:30 AM
I'll share the final result if it will help others.That would be greatly appreciated.

grahampleigh
03-08-2018, 10:19 AM
That would be greatly appreciated.

Ok I'm pretty much finished with it. I tried to upload the userform by attaching it to the post but it didn't let me. Anyone know how I can do this? If not I can paste all the code in here.

There is just one thing that I haven't got working yet:



Private Sub cmdDelete_Click()'deletes the currently selected (from the ListBox) custom document property from the active document


Set DP = ActiveDocument.CustomDocumentProperties

'this currently DOES NOT WORK. Currently if 'Delete' is pressed with no selection the code fails. I don't know the Syntax for "nothing in the ListBox is selected".
If ListBox1.Enabled = False Then
Exit Sub
End If

DP(ListBox1.Value).Delete

Refresh 'refreshes the ListBox and ComboBox
UpdateDoc 'updates the fields in the document - if the recently deleted field was referenced in the document it will now show "Error Unknown Document Property Name"

End Sub


Otherwise it works just like I wanted.

21769

P.S I realise this userform replicates most of the functions of the custom tab on the Advanced Document Properties window but I find this way is more user friendly, plus it allows me to insert a field from the same place. (And it was a fun little project for me to practice on). :thumb

SamT
03-08-2018, 10:42 AM
I tried to upload the userform by attaching it to the post but it didn't let me. Anyone know how I can do this?Use the Go Advanced button . On that page, use the Manage Attachments button


Currently if 'Delete' is pressed with no selection the code fails


If ListBox1.ListIndex > -1 then DP(ListBox1.Value).Delete

gmaxey
03-08-2018, 11:14 AM
For what is it worth, Graham Mayor and I have a comprehensive add-in for this sort of thing: https://gregmaxey.com/word_tip_pages/cc_var_bm_doc_prop_tools_addin.html

grahampleigh
03-10-2018, 08:57 AM
For what is it worth, Graham Mayor and I have a comprehensive add-in for this sort of thing: https://gregmaxey.com/word_tip_pages/cc_var_bm_doc_prop_tools_addin.html

The add-in is excellent, I will almost certainly be using this moving forward. Great work.

I'm still happy that I was able to get my simple solution to work - it's all a learning curve after all.

:thumb

grahampleigh
03-10-2018, 09:25 PM
In case anyone is interested in this simple Custom Document Properties userform, I have attached it here (as a .zip, .frm not a valid file extension for uploading).

Cheers.

gmayor
03-10-2018, 09:48 PM
For what is it worth, Graham Mayor and I have a comprehensive add-in for this sort of thing: https://gregmaxey.com/word_tip_pages/cc_var_bm_doc_prop_tools_addin.html
Also a simpler one ;) http://www.gmayor.com/BookmarkandVariableEditor.htm

(http://www.gmayor.com/BookmarkandVariableEditor.htm)