PDA

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



flohrstw
08-12-2014, 06:37 AM
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

gmayor
08-12-2014, 06:55 AM
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

gmaxey
08-12-2014, 09:08 AM
I think I would use:
With ActiveDocument
.SelectContentControlByTitle("Court").Item(1).Range.Text = cboCourt.Value
'ect.
End With

macropod
08-12-2014, 02:59 PM
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.

gmaxey
08-12-2014, 03:28 PM
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?

macropod
08-12-2014, 03:39 PM
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.

gmaxey
08-12-2014, 03:54 PM
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.

flohrstw
08-13-2014, 08:39 AM
I tried Greg's suggestion but ran into an issue with the DAO reference so I attempted Graham's fix below.


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

gmaxey
08-13-2014, 08:55 AM
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

flohrstw
08-13-2014, 09:19 AM
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

gmayor
08-13-2014, 09:46 PM
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.

gmaxey
08-14-2014, 05:07 AM
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.