• Populate document bookmarks from User Form (W97)

    Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » Populate document bookmarks from User Form (W97)

    Author
    Topic
    #383397

    I have a document with Form Fields, triggered to open a User Form. How do I get the Userform to populate the documents’ Form Field Bookmarks and How do I Initiate the form to read the contents from existing bookmarks to populate the text boxes on the userform?

    See attached document for a sample. If you click on a field, the userbox comes up.

    Viewing 1 reply thread
    Author
    Replies
    • #653726

      I recommend document variables instead of bookmarks because the
      doc var values are stored in the doc even if the references to them are
      (accidentally) deleted. if your bookmarks are deleted, you lose reference
      and data. Plus, you can use a nice doc var manager to manipulate doc
      vars easily.

      So, in your document use Insert | Field | DocVariable and name them
      appropriately, for example “CoName”

      In your Queryclose event on the form (what? you’re not using a queryclose
      event!! — for shame….) put the populate statements:

      DocVarAssign “CoName”,txtBox1.Value

      The function looks like this:
      Sub DocVarAssign(DocVar As String, ByVal DocValue As Variant)
      On Error Resume Next
      With ActiveDocument
      .Variables(DocVar).Delete
      .Variables.Add Name:=DocVar, Value:=DocValue
      End With
      Err.Clear
      End Sub

    • #654099

      Musical,

      Just tacking on a couple of suggestions to Kevin’s:

      If you have a userform that is only going to appear once, at the time the document is created, then using bookmarks is the easiest way to go; you can just use code like:

      ActiveDocument.Bookmarks(“bmkProjectTitle”).Range.Text = txtProjectTitle.Text

      If you want to be able to make use of the userform at any time later in the document’s lifecycle, then it does get trickier; as Kevin notes you don’t have a guarantee that the bookmarks will still be there. It looks like you are trying to work around that by protecting the document, but if you do that, then users can’t readily add content to the rest of the document(!) – you can make the second section unprotected, but then users lose access to a lot of Word functionality, even in the unprotected sections.

      Another option you could look at: don’t use a protected document, and don’t use formfields (or any fields for that matter). Place the front page content into a table structure (which can have no borders; the table isn’t visible on the printed page, it just serves as a means to control the placement of the elements on the cover page).
      You can then use code that refers to the Range.Text of specific cells, to read/write the content of these cells (true, that doesn’t protect against a user deleting a row or a cell, but is it really necessary to protect against that?)

      Also, why do you need the userform at all? – why not let the users edit the cover page content directly – it’s not clear what benefit the userform is providing.

      Gary

      • #655108

        Hmmmmm… You hit on something here…..

        Basically, I just have a template cover with 6 mandetory fields. The fields would most likely be populated at the creation of the document, but could be modified during the document’s lifetime.

        I liked the Table idea! I was able to assign associated styles to each of the rows (yes I know SOMEONE will alter one of the styles SOMEHOW). Then, for the header of other pages, I used a style reference to populate the header.

        • #655308

          Looks nice! – always good to see StyleRef fields in action.

          Here’s something to make it just a little less likely that the users will alter the cover page table styles: put a “z” in front of the style names – that way they will all sit by themselves down at the bottom of the Styles dropdown – just makes it a little less likely that they will be messed with.

          I do this for all ‘reserved’ styles – have never had a problem with a user deleting one of these styles, but there’s always a first time I guess. If you ever ran into a problem with that, you could add a layer of insurance by having code run in Document_Open which checked for the existence of the key styles, and re-added them if any were missing – almost definitely overkill though….

          Gary

    Viewing 1 reply thread
    Reply To: Populate document bookmarks from User Form (W97)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: