Consulting

Results 1 to 11 of 11

Thread: User Form retain input using Custom Document Properties

  1. #1
    VBAX Regular
    Joined
    Jul 2020
    Posts
    11
    Location

    User Form retain input using Custom Document Properties

    Relatively new to VBA coding.


    I’ve set up a word document with a user form that works fine via custom properties. Example of code below:


    Private Sub OK_Click()

    ' Update the properties values
    ThisDocument.CustomDocumentProperties("customTitle").Value = Me.DocumentTitle.Value
    ThisDocument.CustomDocumentProperties("customStartDate").Value = Me.CertDate.Value
    ThisDocument.CustomDocumentProperties("customProjectNo").Value = Me.ProjectNo.Value
    ThisDocument.CustomDocumentProperties("customEqDescription").Value = Me.EqDescription.Value
    ThisDocument.CustomDocumentProperties("customClientName").Value = Me.ClientName.Value
    ThisDocument.CustomDocumentProperties("customClientContact").Value = Me.ClientContact.Value
    ThisDocument.CustomDocumentProperties("customOwnerPhoneNo").Value = Me.OwnerPhoneNo.Value
    ThisDocument.CustomDocumentProperties("customOwnerEmail").Value = Me.OwnerEmail.Value
    ThisDocument.CustomDocumentProperties("customRevisionNo").Value = Me.RevisionNo.Value
    ' Show changes of document properties in document
    ThisDocument.Fields.Update

    ' Hide the userform
    UserForm1.Hide

    End Sub

    Problem is when I save and open an existing document the data is not saved in the user form. If I hit enter on the user form it wipes all entries in the document.
    Would be good if the user form could retain the old inputs. Is this doable, and does someone have some code that does this?

    Appreciate any advice...

  2. #2
    How are you using this document? What is its relationship to the 'existing document'?
    I suspect that the issue might be as simple as confusion between the use of ThisDocument and ActiveDocument, ThisDocument being the document with the macro.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    VBAX Regular
    Joined
    Jul 2020
    Posts
    11
    Location
    It is a simple word document that is repeated time and again for different clients etc.


    I open the document, save as, and fill out the user form.

  4. #4
    Save it as a template c/w the macro and userform and create new documents from it. Change ThisDocument to ActiveDocument

    Frankly I wouldn't use document properties for this task. I would suggest instead using content controls to both receive and display the data in the document. See https://www.gmayor.com/Userform.htm
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  5. #5
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,711
    Location
    If you want to use custom properties, try something like this

    Seems to work

    Option Explicit
    
    
    Private Sub OK_Click()
        ThisDocument.CustomDocumentProperties("SomeProp").Value = Me.custProp.Value
        
        'https://stackoverflow.com/questions/54769648/custom-document-property-not-getting-saved-in-word-document
        ThisDocument.Saved = False      '   <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
        
        Me.Hide
        Unload UserForm1
    End Sub
    
    
    Private Sub UserForm_Initialize()
        Dim oCP As DocumentProperty
        Dim i As Long
        
        With ThisDocument
            For i = 1 To .CustomDocumentProperties.Count
                If .CustomDocumentProperties(i).Name = "SomeProp" Then
                    Set oCP = .CustomDocumentProperties(i)
                    Exit For
                End If
            Next i
        
            If oCP Is Nothing Then  '   custom prop doesn't exist yet
                .CustomDocumentProperties.Add Name:="SomeProp", LinkToContent:=False, Value:="", Type:=msoPropertyTypeString
            Else
                Me.custProp.Value = oCP.Value
            End If
        End With
    End Sub
    Attached Files Attached Files
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  6. #6
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,334
    Location
    Paul,

    Why the For ... Next loop? Why not:
    Dim oCP As DocumentProperty
      With ThisDocument
        On Error Resume Next
        Set oCP = .CustomDocumentProperties("SomeProp")
        If oCP Is Nothing Then  '   custom prop doesn't exist yet
         .CustomDocumentProperties.Add Name:="SomeProp", LinkToContent:=False, Value:="", Type:=msoPropertyTypeString
        Else
          custProp.Value = oCP.Value
        End If
        Set oCP = Nothing
      End With
    Greg

    Visit my website: http://gregmaxey.com

  7. #7
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,711
    Location
    @Greg -- That is a better approach

    Actually, I tried something like that but hit some error (my fault, not Word's) and just went brute force

    Glad you pointed it out
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  8. #8
    VBAX Regular
    Joined
    Jul 2020
    Posts
    11
    Location
    Thanks gents.

    I'm working my way through Graham Mayor's content control method currently.

  9. #9
    VBAX Regular
    Joined
    Jul 2020
    Posts
    11
    Location
    I could never get this to work. For some reason the userform when opening does not populate the document, and there seems to be no macro to run it again...

  10. #10
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,711
    Location
    Without the actual document, it's hard to see what could possibly be wrong

    Use [Go Advanced] at botton right of Reply box, and then the paperclip icon to attach the file
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  11. #11
    VBAX Contributor
    Joined
    Jul 2020
    Location
    Sun Prairie
    Posts
    119
    Location
    Both Graham Mayor and Greg Maxey have pages on this:



    That is, where the userform populates the document and uses information in the document to populate/initialize the userform.

Posting Permissions

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