PDA

View Full Version : Using VBA forms to insert data into MS Word 2007 Content Controls



brownchoc
02-20-2009, 06:26 PM
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.

macropod
02-20-2009, 11:48 PM
Hi Brownchoc,

If all you're doing is pushing data onto the document, why not simply use bookmarks and update their content? Doing so makes it much easier to port the document to other Word versions (even with the compatability patch, Word 97-2003 versions can't do anything with content controls [ie they're treated as plain text]).

cjcant
06-02-2011, 07:16 AM
Hi,

I am fairly new to programming and I want to insert data from excel into bookmarks in word but I do not understand much of the code and do not know how to edit it to my own ends. Can you help?

macropod
06-02-2011, 03:00 PM
Please don't hijack existing threads. You've provided no information that anyone could use to help you. Populating Content Controls from a userform has nothing to do with populating Word bookmarks from Excel.

gmaxey
06-02-2011, 03:12 PM
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




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.

gmaxey
06-02-2011, 05:37 PM
When a CC is created it is assigned a unique ID. Probably the most robust means of referencing CCs is to use the ID. You can get the ID with the Document_ContentControlOnExit event:

Option Explicit
Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
MsgBox ContentControl.ID
End Sub

Sub ScratchMacro()
'A quick macro scratch pad created by Greg Maxey
'Use whatever the msgbox returned as the number here:
ActiveDocument.ContentControls("3060960396").Range.Text = "I am CC ID 3060960396"
End Sub




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.