Results 1 to 5 of 5
  1. #1
    3 Star Lounger
    Join Date
    Jul 2002
    Location
    Edison, New Jersey, USA
    Posts
    215
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Populate document bookmarks from User Form (W97)

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

  2. #2
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Populate document bookmarks from User Form (W97)

    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
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  3. #3
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Populate document bookmarks from User Form (W97)

    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

  4. #4
    3 Star Lounger
    Join Date
    Jul 2002
    Location
    Edison, New Jersey, USA
    Posts
    215
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Re: Populate document bookmarks from User Form (W97)

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

  5. #5
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Populate document bookmarks from User Form (W97)

    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

Posting Permissions

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