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,
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
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).
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 _
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!
There is a lot more to building blocks than just autotext:
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
Another approach is to have all possible options in the document and "reveal" the parts as needed with hide/unhide font property..
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.
Dim ThatCell As Range
Set ThatCell = ActiveDocument.Tables(4).Cell(1,2).Range
Anything that has a range.
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.
I didn't say it was a good option, just another option.
Originally Posted by Shred Dude
I think you're decision for BB/Autotext is a good one.
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.
Building Block Gallery
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
Set oCC = ActiveDocument.SelectContentControlsByTitle("Picked").Item(1)
Set oTmp = ActiveDocument.AttachedTemplate
oCC.LockContents = False
Select Case CC.Range.Text
oTmp.BuildingBlockTypes(wdTypeCustom1).Categories("Demo").BuildingBlocks("O ne").Insert oCC.Range
oTmp.BuildingBlockTypes(wdTypeCustom1).Categories("Demo").BuildingBlocks("T wo").Insert oCC.Range
oCC.LockContents = True
Actually I think just a rich text control named "Picked" would do just as well in this case.
Originally Posted by hred Dude
Originally Posted by fumei
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.
activedocument.SelectContentControlsByTag("testdata").Item(1).Range.Text = "New Stuff"
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?
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.
A tag property
Locking properties (can't delete and can't edit content)