Consulting

Results 1 to 13 of 13

Thread: Advice on Inserting Text Dependent on Formfield/Content Control

  1. #1

    Advice on Inserting Text Dependent on Formfield/Content Control

    I'm seeking some advice on how best to approach this situation. I'm sure it's quite common.

    I'm automating the preparation of a two page document in Word 2007.

    Page one contains various data that I've chosen to place in ContentControls. The second Page is a unique full page of "terms and conditions" dependent on one of three possible choices in a drop down content control on the first page.

    My question is how to best approach this from a programming point of view. Ideally, I'd like to distribute one .dotm file from which these documents will be created. At the end of the document creation process, I intend to save the completed 2-page document as a PDF.

    Should I store each of the three possible "Second Pages" in the template file and then upon the content control changing, move the appropriate page into the second page position, and then only save the first two pages as the PDF? Or somehow make them visible based on the selection in the content control, while keeping the other ones hidden?

    Or do I need three separate supporting files, which I would choose from to append to the first page to create the whole document?

    Just looking for some professional advice on how best to approach this scenario.

    Thanks in advance,

    Shred

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,711
    Location
    I'd create my macro template and add Autotext enteries (now called Quick Parts) assoicated with the template

    Depending on user's choices, just insert the correct auto text into a bookmark.

    I find it easier to maintain / update autotext than a lot of other methods

    Paul

  3. #3
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    I concur. If all your possibilities are in the template then it is straightforward to add them as appropriate (depending on your choice on the first page).

  4. #4
    Thanks guys, for pointing me in the right direction. Once again I learned some new things!

    Having to go to Word Options | Customize to turn on the AutoText icon for Commands not displayed in the Ribbon was a good thing to figure out.

    After that I created a a simple test macro template and added two Auto Text entries to the Building blocks Gallery.

    Then did some rough code like this:

    [VBA]Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)

    If ContentControl.Tag = "choice" Then

    Dim choice As String

    choice = ActiveDocument.SelectContentControlsByTag("choice").Item(1).Range.Text

    ActiveDocument.AttachedTemplate.AutoTextEntries("Terms " & choice).Insert _
    Where:=ActiveDocument.Bookmarks("Terms").Range

    End If
    End Sub[/VBA]

    That worked. I'll clean it up for my circumstances.

    Any other pointers on how to approach this? Was glad to figure out AutoText, very cool feature to this Word rookie!

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

    There is a lot more to building blocks than just autotext:
    http://gregmaxey.mvps.org/Word2007_B...s_AutoText.htm

    You can be very specific with which buildingblock you want to insert by using the template, gallery type, catetory and name:

    Dim oTmp as Template
    Set oTmp = "Whatever"
    oTmp.BuildingBlockTypes(wdTypeAutoText).Categories("General").BuildingBlock s("Test").Insert Selection.Range
    Greg

    Visit my website: http://gregmaxey.com

  6. #6
    VBAX Expert Tinbendr's Avatar
    Joined
    Jun 2005
    Location
    North Central Mississippi (The Pines)
    Posts
    993
    Location
    Another approach is to have all possible options in the document and "reveal" the parts as needed with hide/unhide font property..

    David

    David


  7. #7
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Or have all of them in, bookmarked, and remove them as needed.

    Remember that you can insert AutoText anywhere. They are inserted at the given range.

    Where:=Selection.Range
    Where:= ActiveDocument.Boomarks("Yadda").Range

    Dim ThatCell As Range
    Set ThatCell = ActiveDocument.Tables(4).Cell(1,2).Range
    Where:=ThatCell

    Anything that has a range.

  8. #8
    Thanks for all the input here. I'm started to gain a much deeper appreciation for Word's power.

    Along the lines of having everything in there to begin with and then removing them, I had run that through my mind and thought that might not be a good idea in my case.

    For example, if the required text was a function of the selection in a drop down control, what if the user changed their selection? Had you already "removed" all but the required bit for the earlier choice, how would you be able to "reveal" the newly required choice?

    Toggling visibility with the Font property would work I guess. From what I understand so far however, I think I'm liking the sounds of the Building Blocks Gallery, and the Autotext. As I understand it, that would allow you to store all the possible Ranges of text as elements of the AutoTextEntries collection, and then use them when and where needed with no (little) risk of the user inadvertently deleting something that might be needed later.

    Thanks again for all the great input! I've got a long way to go on really being able to leverage Word.

  9. #9
    VBAX Expert Tinbendr's Avatar
    Joined
    Jun 2005
    Location
    North Central Mississippi (The Pines)
    Posts
    993
    Location
    Quote Originally Posted by Shred Dude
    what if the user changed their selection? how would you be able to "reveal" the newly required choice?
    I didn't say it was a good option, just another option.

    I think you're decision for BB/Autotext is a good one.

    Good luck!
    David

    David


  10. #10
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    And I concur. However, you mentioned the user changing their selection.

    You still have the same issue, regardless of whether it is inductive (adding via AutoText or BuildingBlocks), or deductive (removing via bookmarks).

    If they make a selection, and that causes the insertion of content via AutoText, then once it is inserted it is NOT identified as such. It becomes just like any other chunk of text. That is, unless you explicitly make it identified.

    Other BuildingBlock content...well, I will let Greg handle that.

    On the other hand, if it is an adding (even with "ordinary" AutoText), and it is inserted at a bookmark, then it IS identifiable, and can be removed if the user changes the selection of the driopdown.

    The point being is you have to really think this through.

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

    Building Block Gallery

    Keith,

    You could certainly use a dropdown CC on page 1 and a buildiing block gallery CC on page 2.

    First create your builiding blocks (the chuncks of text to appear on page 2). For this example lets save them in the template using the Custom1 gallery, a new category "Demo", with Titles "One, Two and "Three."

    Add a dropdown CC on page one and name it "Pick." Add the list entries "One, Two, and Three"

    On page 2 insert a building block gallery and set its properties to Custom1 Gallery, Category "Demo" title "Picked"

    Use this code:


    [VBA]Private Sub Document_ContentControlOnExit(ByVal CC As ContentControl, Cancel As Boolean)
    Dim oCC As ContentControl
    Dim oTmp as Template
    Select Case CC.Title
    Case "Pick"
    Set oCC = ActiveDocument.SelectContentControlsByTitle("Picked").Item(1)
    Set oTmp = ActiveDocument.AttachedTemplate
    oCC.LockContents = False
    Select Case CC.Range.Text
    Case "One"
    oTmp.BuildingBlockTypes(wdTypeCustom1).Categories("Demo").BuildingBlocks("O ne").Insert oCC.Range
    Case "Two"
    oTmp.BuildingBlockTypes(wdTypeCustom1).Categories("Demo").BuildingBlocks("T wo").Insert oCC.Range
    End Select
    oCC.LockContents = True
    End Select
    End Sub
    [/VBA]

    Actually I think just a rich text control named "Picked" would do just as well in this case.





    Quote Originally Posted by hred Dude
    Thanks for all the input here. I'm started to gain a much deeper appreciation for Word's power.

    Along the lines of having everything in there to begin with and then removing them, I had run that through my mind and thought that might not be a good idea in my case.

    For example, if the required text was a function of the selection in a drop down control, what if the user changed their selection? Had you already "removed" all but the required bit for the earlier choice, how would you be able to "reveal" the newly required choice?

    Toggling visibility with the Font property would work I guess. From what I understand so far however, I think I'm liking the sounds of the Building Blocks Gallery, and the Autotext. As I understand it, that would allow you to store all the possible Ranges of text as elements of the AutoTextEntries collection, and then use them when and where needed with no (little) risk of the user inadvertently deleting something that might be needed later.

    Thanks again for all the great input! I've got a long way to go on really being able to leverage Word.
    Greg

    Visit my website: http://gregmaxey.com

  12. #12
    Quote Originally Posted by fumei
    And I concur. However, you mentioned the user changing their selection.

    You still have the same issue, regardless of whether it is inductive (adding via AutoText or BuildingBlocks), or deductive (removing via bookmarks).

    If they make a selection, and that causes the insertion of content via AutoText, then once it is inserted it is NOT identified as such. It becomes just like any other chunk of text. That is, unless you explicitly make it identified.

    Other BuildingBlock content...well, I will let Greg handle that.

    On the other hand, if it is an adding (even with "ordinary" AutoText), and it is inserted at a bookmark, then it IS identifiable, and can be removed if the user changes the selection of the driopdown.

    The point being is you have to really think this through.
    Gerry:

    Thanks for the feedback.

    I have a question about your comment on not being able to identify the text after it's been placed in the document, unless at a bookmark.

    What about ContentControls? It seems I can get to that text.
    [VBA]
    ?activedocument.SelectContentControlsByTag("testdata").Item(1).Range.Text
    12345

    activedocument.SelectContentControlsByTag("testdata").Item(1).Range.Text = "New Stuff"
    ?activedocument.SelectContentControlsByTag("testdata").Item(1).Range.Text
    New Stuff

    [/VBA]

    Reading through these forums I find much less posted on Content Controls than other methods. I'm new to doing more in depth stuff in Word, and as such have grabbed onto the "newer" stuff I guess. Maybe I just like the way the Content Controls look.

    Am I constraining myself by using them in a template as opposed to Bookmarks, for example to designate the destination(s) for text I'd be bringing in programatically from other sources?

    Maybe I'm still thinking of Templates in a less than ideal way. If I only want to use the template to create a document programatically, then no value to the visual nature of the ContentControls I guess. But, if I want to throw a bunch of data onto a form via code, and then let the user cruise around on it and make some edits before wrapping things up, then the Content Controls seem like a good thing. You can put descriptive Titles on them, but still have a concise Tag property to use in code, can do Date Pickers, and as I'm just beginning to learn more about can have Building Block Galleries to select from.

    Are the Content Controls just not as widely adopted as Bookmarks, or are there some serious limitations I'm not seeing?

    Thanks again...

  13. #13
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,334
    Location
    Keith,

    CCs were introduced with Word2007. Like me in many ways, many people resist change. A bookmark is a defined range in a document so is a content control. While the methods are different, whatever you can do with a bookmark your can pretty much do with a CC. There are a few exceptions and the overall advantage that CCs has over a bookmark it that it has more properties.

    For example:

    A tag property
    Locking properties (can't delete and can't edit content)
    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
  •