Consulting

Results 1 to 12 of 12

Thread: Add All Custom Document Properties to a Combobox

  1. #1

    Add All Custom Document Properties to a Combobox

    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

  2. #2
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location
    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
    Greg

    Visit my website: http://gregmaxey.com

  3. #3
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    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
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  4. #4

    Thumbs up

    Quote Originally Posted by gmaxey View Post
    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!

  5. #5
    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.

  6. #6
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    I'll share the final result if it will help others.
    That would be greatly appreciated.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  7. #7
    Quote Originally Posted by SamT View Post
    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.

    Capture.jpg

    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).

  8. #8
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    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
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  9. #9
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location
    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...ols_addin.html
    Attached Images Attached Images
    Greg

    Visit my website: http://gregmaxey.com

  10. #10
    Quote Originally Posted by gmaxey View Post
    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...ols_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.


  11. #11
    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.
    Attached Files Attached Files

  12. #12
    Quote Originally Posted by gmaxey View Post
    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...ols_addin.html
    Also a simpler one http://www.gmayor.com/BookmarkandVariableEditor.htm

    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •