• UserForms in Templates (2007)

    • This topic has 9 replies, 2 voices, and was last updated 16 years ago.
    Author
    Topic
    #459231

    I working on converting templates to 2007. Some of the templates contain UserForms, some of the templates have protection in them, and some do not. When using the report template (without any protection), after filling in the UserForm, the text will go into the Text Form Field text area only, it will not automatically fill in any refs to those form fields. The original code in the template including a switch to normal view then back to page (or print) view to refresh all ref fields. This no longer works. I’ve tried selecting Calculate on exit for all form fields but that doesn’t make a difference either.

    I think I found yesterday that if a template is protected, it needs an ActiveDocument.Fields.Update in the code, which I’ve added. As of yesterday, that particular template worked but I didn’t try it on any others. I was hoping that same code would work in an unprotected file but it doesn’t. I’ve spent hours searching for an answer and trying different variations but nothing is working.

    Viewing 4 reply threads
    Author
    Replies
    • #1157314

      Don’t know whether this speaks directly to the problem you’re having, but am curious why, in an unprotected document, you’re populating text from the userform, into a text formfield (rather than say a bookmark). If you assign text to a formfield, in an unprotected document, the text is going to replace the formfield rather than fill it, and then the ref fields then won’t have anything to reference.

      Better way to go would be to insert the text from the userform into a (wrapped) bookmark, and then have the ref field refer to the bookmark.

    • #1157319

      The text formfields are on the first page (or first occurrence) and the bookmark is set in the field settings box. After that first occurrence, all the fields are ref fields.

    • #1157320

      To expand further, you say if text is assigned to a formfield , in unprotected doc., the text will replace it. For each spot on the userform, where user fills in with text, that cell is assigned the same bookmark name. When document populates, it populates correctly and always (versions 97-2003) filled in correctly. It’s in Word 2007 where it’s not repeating.

      • #1157425

        Have to admit, I’ve never used formfields that way, or seen them used that way, but you’re right, if you programatically assign text to the Result of a formfield, then the formfield populates with the text, rather than being overwritten (the way it will in the user interface).

        Just had a chance to run your attached sample .dot in Word 2003 and Word 2007 and I get the same issue you’re getting, where the ref fields don’t seem to update. Not sure yet why that is happening; formfields are ‘legacy’ features in Word 2007, but they should still function OK.

        In the meantime, still can’t get over thinking that using formfields in this way – in an unprotected document – may not be the best way to go. Even if you can assign text to the formfields programatically at the time the document is created, once the document has been created and the formfields are sitting unprotected in the user interface, they are way too prone to being overwritten: all the user has to do is try to revise the text in one of the formfields (say, to correct a typo), and poof, the formfield is overwritten, and then any ref fields that link to it, are going to change to Error references the next time they get updated.

        If you were to do this with bookmarks rather than formfields, you still couldn’t guarantee that the bookmarks won’t get deleted, but it is a lot harder for a user to accidentally delete a bookmark, than to delete an unprotected formfield.

        In either case, if you don’t need to allow for the user ever editing the content in the formfields or the bookmarks after the document is first created (which your template doesn’t current appear to allow for anyway), then you could avoid the ref fields altogether – just replace them with bookmarks, and assign the text content appropriate to them, from what is entered in the userform text fields, at the time the document is created.

        None of the above solves your immediate problem of getting the ref fields to update in Word 2007; just wanted to put out the above ideas in case they’re helpful.

      • #1157440

        Here’s something that should work for you in all versions of Word:

        – I was wondering why you didn’t simply have a line of code like:

        ActiveDocument.Fields.Update

        to force the ref fields to update. But the problem with that, is that this will also force the text formfields to update, which in this case removes any text that has been assigned to their Result (another reason why formfields might not be best to use here!).

        Instead, you could selectively update all fields except Text Formfields, like this:

        Code:
        Dim fld As Field
        For Each fld In ActiveDocument.Fields
           If fld.Type  wdFieldFormTextInput Then
        	  fld.Update
           End If
        Next 'fld

        Just tested this in your template in Word 2007, and it appears to work fine.

    • #1157323

      I’m going to try attaching a stripped down file to maybe show better how the template is set up.

    • #1158123

      Gary, thanks for looking at this.

      I did add the ActiveDocument.Fields.Update code to one of the templates that is protected. (Most of the protected templates are set up in the same way (with the same type of userform) but it is set to protect the document after clicking ok.) It seemed to work on the one I added it to but I’ve got to try again a few more times just to make sure before adding it to all of them. Where I have added it is just between the ExitSub and EndSub.

      As far as the way the templates are set up, that was how I was instructed years ago to set them up, when we switched from Word Perfect to Word and to be honest, they have worked well for how we use them. There have been times when a user will select the form field and wipe out the text, but by either instructing them on how to change it in the future (double-clicking and editing there) or me dropping in the new field, we haven’t seen too many problems with them. If you’d like to expand further on just using bookmarks, I’d like to hear about it but would that start with the userform as it does now?

      As far as the code you posted, could you tell me where I’d put that? If I remember right, the fields would update when the document was printed but most users want to see the text immediately when the document is on screen.

      • #1158195

        Leanne,

        ActiveDocument.Fields.Update will work fine in a document that contains formfields, as long as that document is protected – with the document protected, ActiveDocument.Fields.Update will update all non-formfield fields, while leaving the contents of the formfields alone.

        By contrast, if you have formfields in an unprotected document, then ActiveDocument.Fields.Update will blank out any existing content in the unprotected formfields – so you can’t use ActiveDocument.Fields.Update in the template as you currently have it.

        Am away from home base (and my Ofc 2007 PC) until sometime later Monday, so can’t review the code right now, but going by recollection of your code, I think you would substitute the code I’ve suggested, in place of the statements you currently have that toggle the page view (in an attempt to force the fields to update). (You should also put the Dim statement up at the very beginning of the procedure.)

        You can then comment out the two statements you have that toggle the page view, because these are not really needed.

        Will try to provide some details on how to use bookmarks in your document, instead of formfields, when I get back on Monday. But it’s worth noting that bookmarks will work fine with your userform as you currently have it; it’s mainly a matter of changing the code behind your userform so that it doesn’t populate the content from the userform into unprotected formfields – instead it populates them into bookmarks.

        Another thing you could look into, since you’re migrating this template to Word 2007, is to use text ‘content controls’ instead of the unprotected formfields – content controls are the new replacements in Word 2007, for the traditional formfields, and they have some advantages that may be relevant to what you need to do: you can have a text content control in an unprotected document, and set the content control so that it can’t be deleted. You can find the content controls under the Developer tab in Word 2007. You should be able to get the content from the userform, into the content controls, in a manner very similar to what you’re currently using for the text formfields.

        Gary

      • #1158371

        Here’s some basic info on using bookmarks rather than formfields in your template – you can try this in a copy of your existing template:

        In place of your “txtReportTitle” formfield, delete it and replace it with a space. Select the space, and use Insert > Bookmarks to insert a bookmark here. You could name it something like “bmkReportTitle”.
        Do the same with the “txtProjName” and “txtProjLoc” formfields.
        Change the Ref fields that previously referred to the ‘txtReportTitle’ etc. bookmarks, to refer to the new bookmark names.

        In the code behind your userform, comment out these three lines of code:

        ActiveDocument.FormFields(“txtReportTitle”).Result = txtReportTitle
        ActiveDocument.FormFields(“txtProjName”).Result = txtProjName
        ActiveDocument.FormFields(“txtProjLoc”).Result = txtProjLoc

        In its place you’re going to use code that assigns the value of txtReportTitle etc., to the bookmark, while keeping the bookmark wrapped around the text.
        Simply assigning a string value to a bookmark in a document is easy:

        ActiveDocument.Bookmarks(“bmkExample”).Range.Text = “Example Text”

        – but doing it that way doesn’t preserve the bookmark (which you need to do here so the Ref fields can continue to refer to it.
        To assign text to a bookmark, while keeping the bookmark wrapped around the text, you can use code like this:

        Code:
        Public Sub InsertBmkTextWrapped(BmkName As String, BmkText As String)
           
           Dim Bmks As Bookmarks
           Dim BmkRange As Range
           
           Set Bmks = ActiveDocument.Bookmarks
           
           If Bmks.Exists(BmkName) Then
        	  Set BmkRange = Bmks(BmkName).Range
        	  BmkRange.Text = BmkText
        	  Bmks.Add BmkName, BmkRange
        	  Set BmkRange = Nothing
           End If
           
           Set Bmks = Nothing
           
        End Sub

        – you can put this procedure into the code module behind the userform, or alernatively, into a standard code module.
        You call this procedure for each of the bookmarks you want to populate, for instance:

        InsertBmkTextWrapped bmkReportTitle, txtReportTitle

        One caveat is that if the users are going to be editing the content in the bookmarks frequently, it is possible for them add text that won’t go into the bookmark – for instance if they type more text at the end of the bookmarked text – one method for a situation like that is to make it possible for the users to reshow the userform when they want to edit the key text, and have the text content from the bookmarks, populate back into the userform for further editing. But if the users rarely edit the bookmarked content, that might not be necessary.

        As mentioned previously, perhaps a better option in Word 2007 would be to use text content controls. There are some quirks in working with these; will try to post something about that sometime soon.

        Gary

    Viewing 4 reply threads
    Reply To: UserForms in Templates (2007)

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

    Your information: