Using VBA forms to insert data into MS Word 2007 Content Controls
Hello All, I need urgent help pls! I am new to working with VBA + Word 2007.
I am currently working with Word 2007 template document. On the template, I created few text fields using Content Controls(i.e. rich text control) instead.
I labelled the controls as Name, Title etc.
I have also created a simple VBA Userform with text fields i.e. Name, Title.
Now, rather than entering text/data directly into the template document when it is loaded, I want to use the VBA userform to enter the data so that the data gets transferred to the text areas on the document when a command button is clicked.
This is the requirement from my place of work and i need help pls. I have tried numerous suggestions from msdn site, but to no avail. I'm always getting run time errors e.g. "run-time error 5941: the requested member of the collection does not exist"
Private Sub CommandButton1_Click()
ActiveDocument.ContentControls("Title").Range.Text = TextBox1.Text
End Sub
Any help or code would be greatly appreciated, thanks.
Write UF value to CC range
Since MS (rather stupidly in my opinion) allows multiple CCs with the title "Name" or "Title" or "Whatever" you have to explicitly identify the one you want to work with. One way is to use the Item index:
Private Sub CommandButton1_Click()
With ActiveDocument
.SelectContentControlsByTitle("Name").Item(1).Range.Text = Me.TextBox1
.SelectContentControlsByTitle("Title").Item(1).Range.Text = Me.TextBox2
End With
End Sub
Quote:
Originally Posted by brownchoc
Hello All, I need urgent help pls! I am new to working with VBA + Word 2007.
I am currently working with Word 2007 template document. On the template, I created few text fields using Content Controls(i.e. rich text control) instead.
I labelled the controls as Name, Title etc.
I have also created a simple VBA Userform with text fields i.e. Name, Title.
Now, rather than entering text/data directly into the template document when it is loaded, I want to use the VBA userform to enter the data so that the data gets transferred to the text areas on the document when a command button is clicked.
This is the requirement from my place of work and i need help pls. I have tried numerous suggestions from msdn site, but to no avail. I'm always getting run time errors e.g. "run-time error 5941: the requested member of the collection does not exist"
Private Sub CommandButton1_Click()
ActiveDocument.ContentControls("Title").Range.Text = TextBox1.Text
End Sub
Any help or code would be greatly appreciated, thanks.