PDA

View Full Version : Advice on Inserting Text Dependent on Formfield/Content Control



Shred Dude
02-07-2011, 11:06 AM
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

Paul_Hossler
02-07-2011, 11:42 AM
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

fumei
02-07-2011, 12:31 PM
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).

Shred Dude
02-07-2011, 02:55 PM
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:

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

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!

gmaxey
02-07-2011, 08:02 PM
Keith,

There is a lot more to building blocks than just autotext:
http://gregmaxey.mvps.org/Word2007_Building_Blocks_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").BuildingBlocks("Test").Insert Selection.Range

Tinbendr
02-08-2011, 06:16 AM
Another approach is to have all possible options in the document and "reveal" the parts as needed with hide/unhide font property..

David

fumei
02-08-2011, 11:46 AM
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.

Shred Dude
02-08-2011, 02:05 PM
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.

Tinbendr
02-08-2011, 05:30 PM
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. :rotlaugh:

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

Good luck!
David

fumei
02-10-2011, 01:54 PM
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.

gmaxey
02-10-2011, 06:57 PM
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:


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("One").Insert oCC.Range
Case "Two"
oTmp.BuildingBlockTypes(wdTypeCustom1).Categories("Demo").BuildingBlocks("Two").Insert oCC.Range
End Select
oCC.LockContents = True
End Select
End Sub


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






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.

Shred Dude
02-11-2011, 01:09 AM
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.

?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



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? :dunno

Thanks again...

gmaxey
02-11-2011, 11:48 AM
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)