Consulting

Results 1 to 12 of 12

Thread: Can Plain Text Content Control be used the same as Legacy Form Field with UserForms?

  1. #1
    VBAX Regular
    Joined
    Apr 2012
    Posts
    15
    Location

    Can Plain Text Content Control be used the same as Legacy Form Field with UserForms?

    I have several templates that use a UserForm to populate a series of legacy form fields throughout a fairly complex document. I am looking at upgrading the templates to replace the legacy form fields with plain text content controls to eliminate some of the restrictions of the legacy fields. Here is an example of what I've got in one of these templates:


    Private Sub cmdSubmit_Click()
    With ActiveDocument
    .Bookmarks("IR").Range = txtCase.Value
    .FormFields("Court").Result = cboCourt.Value
    .FormFields("County").Result = cboCounty.Value
    .FormFields("Number").Result = txtNumber.Value


    Is there an easy way to convert a form such as this to utilize content controls as opposed to the legacy form components? If so, I would appreciate any input you can provide on what this code should look like. I would primarily use the plain text content control and the drop-down list content control. I've played around with it a little bit but don't have enough experience to make this work. I've also tried searching Greg Maxey's page but didn't find anything similar to what I'm trying to accomplish. Thanks.

    Tim

  2. #2
    The process would be similar e.g.

    Dim oCC As ContentControl
    For Each oCC In ActiveDocument.ContentControls
        If oCC.Title = "IR" Then
            oCC.Range.Text = txtCase.Text
        End If
     'etc
    Next oCC
    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
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location
    I think I would use:
    With ActiveDocument
    .SelectContentControlByTitle("Court").Item(1).Range.Text = cboCourt.Value
    'ect.
    End With
    Greg

    Visit my website: http://gregmaxey.com

  4. #4
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Since the document is being populated via a userform, one has to wonder why that wouldn't be done using:
    • bookmarks;
    • DocVariables; and/or
    • Custom Document Properties,
    the latter two using fields, of course.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

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

    Bookmarks and fields are easily deleted? (sometimes inadvertently). It takes a dedicated purposeful action to delete a content control when the "Content control cannot be deleted" property is set. I think CCs are ideal as range targets in documents created using userforms.

    Do you have some information or an opinion why not to use them for that purpose?
    Greg

    Visit my website: http://gregmaxey.com

  6. #6
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Yes, I recognise that bookmarks are somewhat fragile but, for a document being populated from a userform, which usually means a document newly-created from a template, that really shouldn't be an issue.

    I have no objection to the use of content controls - or formfields (even in an unprotected document) - for the OP's purposes, but I do wonder what benefit the added complexity achieves.

    A benefit of using DocVariables and/or Custom Document Properties is that they'll remain with the document even if the fields referencing them get deleted.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

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

    Not to argue as very little I do is any better that what you or someone else would elect to do. I'm simply a fan of content controls and if MS ever gets off their duff and fixes a few long standing issues I will like them even more.

    When I create a template that is intended to be populated from a userform, I typically use CCs with the "CC cannot be deleted" property set. I also map the CCs so even if they are deleted I can restore the data from the XMLPart.

    With my CC Tools add-in is a very quick and easy to do both.

    I like CCs over fields because they are readily editable after populating form the userform. If I enter something wrong in the form, the I see it and can fix it in a breeze using the CC.
    Greg

    Visit my website: http://gregmaxey.com

  8. #8
    VBAX Regular
    Joined
    Apr 2012
    Posts
    15
    Location

    Fixed the body of the document but not the header

    I tried Greg's suggestion but ran into an issue with the DAO reference so I attempted Graham's fix below.

    Quote Originally Posted by gmayor View Post
    The process would be similar e.g.

    Dim oCC As ContentControl
    For Each oCC In ActiveDocument.ContentControls
        If oCC.Title = "IR" Then
            oCC.Range.Text = txtCase.Text
        End If
     'etc
    Next oCC
    The UserForm now correctly populates all of the Plain Text Content Controls in the body of the document; however, there is a single field located in the header that is not populating. In the previous version of this template I had to use a bookmark since legacy form fields are not supported in the header. Here is the code I am using:

        If oCC.Title = "RN" Then
            oCC.Range.Text = txtRN.Text
        End If
    Am I missing something? I'm not receiving any runtime errors, the UserForm is simply not populating this one specific Content Control field. Thanks.

    Tim

  9. #9
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location
    I really don't know what DOA has to do with my code but nevertheless, ActiveDocument.ContentControls only processes CCs in the main text story range

    You can select the CC in the header and run:

    Sub GetCCID()
    Debug.Print Selection.ContentControls(1).ID
    End Sub

    Then you can write to explicitly using:

    Sub ScratchMacro()
    'A basic Word macro coded by Greg Maxey
    ActiveDocument.ContentControls("VALUE OF ID PRINTED ABOVE").Range.Text = "write out this text"
    End Sub
    Greg

    Visit my website: http://gregmaxey.com

  10. #10
    VBAX Regular
    Joined
    Apr 2012
    Posts
    15
    Location
    Greg,

    I doubt it had anything to do with your code and everything to do with my lack of experience/knowledge. I've recreated the same template using the method you suggested. Just as before, I'm receiving Run Time Error 438 - "Object does not support this property or method." The debugger highlights the first line of code which appears as follows:
    .SelectContentControlByTitle("Company").Item(1).Range.Text = txtBus.Value
    As far as I can tell, this is an issue with my reference selections. Is there a specific reference that I should be looking for/enabling to make this work?

    Thanks,
    Tim
    Last edited by flohrstw; 08-13-2014 at 10:56 AM.

  11. #11
    You can address controls in the body or header by modifying my earlier suggestion e.g.

    Dim oCC As ContentControl
    Dim oStory As Range
        For Each oStory In ActiveDocument.StoryRanges
            For Each oCC In oStory.ContentControls
                If oCC.Title = "IR" Then
                    oCC.Range.Text = "Value"
                End If
                'etc
            Next oCC
        Next oStory
    As for the merits of content controls over form fields, bookmarks, docvariables etc as a means of entering data, I have to say that I am not a fan, and Greg and I have argued the merits privately for some time; however they do have a particular advantage in such an application as this in that they can be locked against deletion, while leaving the document fully editable, which makes it harder for a user to screw up the document, by accidentally deleting a field or a bookmark.
    Last edited by gmayor; 08-13-2014 at 10:39 PM.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  12. #12
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location
    If this:
    .SelectContentControlByTitle("Company").Item(1).Range.Text = txtBus.Value

    is the first line of code then your are missing the document object.

    If a new document. Try the following procedures. Run the code to insert the CC first then the code to add text to it.
    Sub ScratchMacro()
    'A basic Word macro coded by Greg Maxey
      With ActiveDocument
        .ContentControls.Add (wdContentControlText), Selection.Range
        .ContentControls(1).Title = "Company"
      End With
    End Sub
    Sub ScratchMacroII()
    'A basic Word macro coded by Greg Maxey
      With ActiveDocument
        .SelectContentControlsByTitle("Company").Item(1).Range.Text = "this is some text"
      End With
    End Sub
    Grahams method will work, but you are looping through all of the CCs in the document to find the one you want. If you already know the one you want then you can write to it explicitly using its title and item number, tag and item number, index number or ID.
    Greg

    Visit my website: http://gregmaxey.com

Posting Permissions

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