Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Addressing Content Controls in Word 2007

Status
Not open for further replies.

scottintexas

Programmer
Feb 27, 2006
12
US
This may be too new for any answers but since I am trying to make this work I might as well ask one of the masters here. I have placed a ContentControl (Text) on my document. The Tag is txtTractID. I want to grab the text the user puts in that control and put it in the second text control. Then I put this code to work.
Code:
Dim objTract As ContentControl
Dim objDoc As Document
Dim objTract2 As ContentControl

Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
    If ContentControl.Tag = "txtTract" Then
        Set objTract2 = objDoc.ContentControls("txtTract2")
        objTract2.Range.Text = ContentControl.Range.Text
    End If
End Sub

Private Sub Document_New()
    Set objDoc = ActiveDocument
End Sub

The Set objTract2 throws an error. I just can't figure out how to set the object. I found the Word Developer Help a little lacking in help. Maybe I'm just not getting it. Any help would be greatly appreciated.

ScottInTexas
It's probably as hard as it looks.
 
Code:
Set objTract2 = objDoc.ContentControls("txtTract2")
will ONLY work if Document_New has executed. The object objDoc is set there.

If Document_New nevers fires - and I am not sure why you are using Document_New - then objDoc is not Set, and any use of it will cause an error.

Gerry
My paintings and sculpture
 
I set the objDoc at the opening of the new document because this is a tempate that, once started, there is some data that is not to be changed, namely the tract number. This tract nymber should be entered once by the user in the first text content control. Then it is to be placed in a content control in the footer. What I am trying to do is make the placement of the footer text automatic and locked. If there is a better way then I would appreciate the input.

The error is 91 - Object Variable or with block variable not set.

ScottInTexas
It's probably as hard as it looks.
 
I do not understand. If this is a .DOT (template) file, and you are using File > New to clone a new document, then yes, Document_New will fire.

However, why is the user setting anything? I do not understand a value that is set by the user but then "not to be changed". Why is this not in the template?

If I undertsand correctly, you are:

1. cloning a new document
2. the user enters text into the first text control
3. this moves that text into another text control in the footer.

Is this correct? I am not using 2007, but it would seem to me that if this code is in the ThisDocument module of the template, and it is Private, then it will not work properly for the cloned document.

Try putting the globval variable objDoc in another module, making it public. Have the procedure Document_ContentControlOnExit in that other module, NOT ThisDocument. The ThisDocument module works on the template file, not the cloned document.

Document_New should create the object, the other module will be available from the cloned document and should execute properly.

Gerry
My paintings and sculpture
 
Thanks Fumei for your response.

Yes you are nearly right about the function of the document. When the new document is cloned the user must enter his/her tract # in the first field. This tract number will also appear in the footer and in the legal description on the first page. I don't want it to be changed once it is entered. I may be approaching the whole problem wrong, but then that would be a function of not understanding automated templates as well as I should. I am used to doing things in VB and VBA but never in a word environment.
This document will also have other automated content, but not until I get this addressing issue solved. I understand what you are saying about the location of the global and the procedure and will try that as soon as I get this little procedure to work.

ScottInTexas
It's probably as hard as it looks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top