Consulting

Results 1 to 13 of 13

Thread: Edit values in Customdocumentproperties from userform

  1. #1
    VBAX Regular
    Joined
    Nov 2020
    Posts
    16
    Location

    Edit values in Customdocumentproperties from userform

    Hi. I´m really new with VBA but am trying to edit my word-document.
    It is a word-document where i edit different bookmarks spread thoughout the text with a userform that pops up when opening the document.

    I use this code (and there are 14 more bookmarks i´m not showing here):

    Privat Sub Generera_Click()  
    
        Dim Inskrivning_fastighetsbeteckning As Range
        Set Inskrivning_fastighetsbeteckning = ThisDocument.Bookmarks("Inskrivning_fastighetsbeteckning").Range
        Set Inskrivning_fastighetsbeteckning1 = ThisDocument.Bookmarks("Inskrivning_fastighetsbeteckning1").Range
        Set Inskrivning_fastighetsbeteckning2 = ThisDocument.Bookmarks("Inskrivning_fastighetsbeteckning2").Range
        Set Inskrivning_fastighetsbeteckning3 = ThisDocument.Bookmarks("Inskrivning_fastighetsbeteckning3").Range
        Inskrivning_fastighetsbeteckning.Text = Me.TextBox1.Value
        Inskrivning_fastighetsbeteckning1.Text = Me.TextBox1.Value
        Inskrivning_fastighetsbeteckning2.Text = Me.TextBox1.Value
        Inskrivning_fastighetsbeteckning3.Text = Me.TextBox1.Value
    Me.Repaint
    UserForm1.Hide
     
    End Sub

    My issue is that my code deletes the bookmarks after writing to them. And it is also very laborious to enter the bookmarks throughout the text (some repeat 25 times) to begin with.
    I first wanted to create code that recreated the bookmarks over the new textvalues but then I still have the the problem with repeating bookmarks up to 25 times (name1, name2 etc) in the first place.
    I really want to use Custom document properties instead but I don´t know how to change my code to change the values in the Custom document properties. PS. I have created the custom document properties in the document and they work when editing them manually.
    If I could use the userform to change the values then it would be sooooo much more helpful for me, my team and my customers. The userform could change from a onetime thing to something used for editing the document in all future revisions.

    These are the names of my customdocumentproperties:
    "Inskrivning_Fastighetsbeteckning"
    "Inskrivning_Företagsnamn"
    "Inskrivning_Verksamhetsnamn"
    "Inskrivning_Gatuadress"
    "Inskrivning_Postadress"
    "Inskrivning_Organisationsnummer"
    "Inskrivning_Pärms_placering"
    "Inskrivning_Återsamlingsplats"
    "Inskrivning_Fastighetsägare"
    "Inskrivning_Fastighetsägare_organisationsnummer"
    "Inskrivning_Teknikernamn"
    "Inskrivning_Teknikergatuadress"
    "Inskrivning_Teknikerpostadress"
    "Inskrivning_Tekniker_epost"
    "Inskrivning_Tekniker_telefon"

    My textboxes are named from TextBox1 to TextBox15 and reflect the order of the above documentproperties.
    How do i go about achieving this?

    I have also other questions regarding my userform but everyting depends on getting the userform to change the values of existing customdocumentproperties.

    There are also lot of checkboxes that delete other bookmarks also in this code (i didn´t show them in the first code cause they are not the issue for me, they work but i don´t really know if they are efficient):

    Dim Name_Name As Range
        Set Name_Name = ThisDocument.Bookmarks("Name_Name").Range
        If UserForm1.CB_Name.Value = False Then
        Name_Name.Delete
    End If
    Like i said to start with, I am really new at this and am basically learning by doing for now.
    Last edited by Bob Phillips; 11-20-2020 at 06:54 AM. Reason: Added code tags

  2. #2
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,242
    Location
    This may help:

    Sub ScratchMacro()
    'A basic Word macro coded by Greg Maxey
    Dim oRng As Range
    Dim oCDP As DocumentProperty
      'Say you have a bm named "bmDemo" and you want to write text to it ...
      Set oRng = ActiveDocument.Bookmarks("bmDemo").Range
      oRng.Text = "Testing one, two, three ..."
      'Doing that destroys the bookmark so recreate it around the new defined range.
      ActiveDocument.Bookmarks.Add "bmDemo", oRng
      'Say you have a custom document property named "cdpDemo"
      Set oCDP = ActiveDocument.CustomDocumentProperties("cdpDemo")
      'You change its value based:
      oCDP.Value = "Testing one, two, three ..."
      'You have a DocProperty field in the document
      ActiveDocument.Fields.Update
    lbl_Exit:
      Exit Sub
    End Sub
    Greg

    Visit my website: http://gregmaxey.com

  3. #3
    VBAX Regular
    Joined
    Nov 2020
    Posts
    16
    Location
    I was hoping to use custom document properties instead of bookmarks.
    Would something like this work:

    Dim Inskrivning_Fastighetsbeteckning As DocumentProperty
    Set Inskrivning_Fastighetsbeteckning = ThisDocument.CustomDocumentProperties ("Inskrivning_Fastighetsbeteckning")
    Inskrivning_Fastighetsbeteckning.Value = Me.TextBox1.Value
    ActiveDocument.Fields.Update
    lbl_exit:
    UserForm1.Hide
    End Sub

  4. #4
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,242
    Location
    I was just showing your how to write to a bookmark range and preserve the bookmark.

    Something as simple as this should work:
    ThisDocument.CustomDocumentProperties ("Inskrivning_Fastighetsbeteckning").Value = TextBox1.Value
    Greg

    Visit my website: http://gregmaxey.com

  5. #5
    VBAX Regular
    Joined
    Nov 2020
    Posts
    16
    Location
    Thx, Greg. I´ve got both the bookmarks method and the customdocumentproperties method to work. I am using the customdocumentproperties method since it requires alot less code (by a lot) and it´s still possible to update the document manually. BTW, this is the code that worked for me (I couldn´t get your exampleline to work for me):

    Private Sub Generera_Click()


    If Me.TextBox1.Value = True Then
    Dim Inskrivning_fastighetsbeteckning As DocumentProperty
    Set Inskrivning_fastighetsbeteckning = ThisDocument.CustomDocumentProperties("Inskrivning_fastighetsbeteckning")
    Inskrivning_fastighetsbeteckning.Value = Me.TextBox1.Value
    ActiveDocument.Fields.Update
    End If
    lbl_exit:

  6. #6
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,242
    Location
    I really don't know why you are having to declare the DocumentProperty variable. Are you sure that this doesn't work:

    ThisDocument.CustomDocumentProperties("Inskrivning_fastighetsbeteckning").V alue = TextBox1.Value
    Greg

    Visit my website: http://gregmaxey.com

  7. #7
    VBAX Regular
    Joined
    Nov 2020
    Posts
    16
    Location
    I tried this but it does nothing when i try it:

    If Me.TextBox1.Value = True Then
    ThisDocument.CustomDocumentProperties("Inskrivning_Fastighetsbeteckning").V alue = TextBox1.Value
    ActiveDocument.Fields.Update
    End If

  8. #8
    VBAX Regular
    Joined
    Nov 2020
    Posts
    16
    Location
    When i'm on the topic of custom document properties. How would i go about getting the textboxes to display the current values of the document properties? Was thinking some code could run when opening the form

  9. #9
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,242
    Location
    You would go about it as you have thought. Why don't you try something like:

    TextBox1.Value = ThisDocument.CustomDocumentProperties("Inskrivning_Fastighetsbeteckning").V alue
    Greg

    Visit my website: http://gregmaxey.com

  10. #10
    VBAX Regular
    Joined
    Nov 2020
    Posts
    16
    Location
    Greg, should this code be placed placed in the beginning of the code (to be executed when opening the userform, assuming the code is correct)? Or is there another place for the opening (activating?) event?

    Private Sub Userform1_Open
    TextBox1.Value = ThisDocument.CustomDocumentProperties("Inskrivning_Fastighetsbeteckning").V alue
    End Sub

    I hope i´m not pestering you with what I assume are very basic questions for you but as you may have figured out i´m not fluent in VBA

  11. #11
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,242
    Location
    From within a standard module, you could do it before you show the form:

    Sub DisplayForm()
    Dim oFrm As UserForm1 'If you didn't change the form  name
      Set oFrm = New UserForm1
      'You could do it here before you show the form:
      'oFrm.TextBox1.Text = ActiveDocument.CustomDocumentProperties("cdpDemo").Value
      oFrm.Show
      Unload oFrm
      Set oFrm = Nothing
    lbl_Exit:
      Exit Sub
    End Sub

    For you could do it with one of the UserForm event procedures:

    Option Explicit
    
    Private Sub UserForm_Activate()
      'You could do it here:
      TextBox1.Text = ActiveDocument.CustomDocumentProperties("cdpDemo").Value
    End Sub
    Private Sub UserForm_Initialize()
      'You could do it here:
      TextBox1.Text = ActiveDocument.CustomDocumentProperties("cdpDemo").Value
    End Sub
    Greg

    Visit my website: http://gregmaxey.com

  12. #12
    VBAX Regular
    Joined
    Nov 2020
    Posts
    16
    Location
    I couldn´t get the code to run on start of userform but i created a button to call the code instead.

    I found that the line: "TextBox1.Text = ActiveDocument.CustomDocumentProperties("Inskrivning_Fastighetsbeteckning") .Value" renders my textform unusable.
    It reads the value as it should but it seems it reads constantly so it is impossible to write a new value in the textbox (which was the purpose if the textbox to begin with).

    How do i go about making it read the current value, display the value in a textbox and then being able to edit the value in the textbox.

  13. #13
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,242
    Location
    You need to stop telling me what you "couldn't" do and instead post your code so I can see what you are trying to do.
    See attached document. There is code in the standard module and in the UserForm1 module.Demo.docm
    Greg

    Visit my website: http://gregmaxey.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
  •