PDA

View Full Version : Referencing a rich text content control



Who_else
03-31-2016, 06:29 PM
Hello there!

I'm very new to the word VBA scene, have done a bit of Frankenstein work with Excel's VBA and have built some handy things,
I cannot seem to think of a way to reference a content aware section, Usually I just record macro, do what I need, and steal that bit and tailor it for a bigger macro project, but when I record and select the content aware text, it records nothing in VBA.... So I'm at a loss.
What I am needing to do is as follows:

I have a leave request document that is used by the whole office,

in that I want all of the sections to be able to be sections to fill that are then checked by a macro (when a submit button is clicked) that will check their surname and save it in their personal file, as well as check their position (from a drop down list) and email it to their direct team leader.
any help is greatly appreciated.
Thanks in advance.

gmayor
03-31-2016, 10:17 PM
There are a number of ways to reference the content control e.g. by name or tag - see http://www.vbaexpress.com/forum/showthread.php?39873-Content-Control-and-VBA

or you could cycle through the content control collection


Sub Macro1()
'www.gmayor.com
Dim oDoc As Document
Dim oCC As ContentControl
Dim sText As String
Dim bFound As Boolean
Set oDoc = ActiveDocument
For Each oCC In oDoc.ContentControls
If oCC.Title = "Name" Then
If Not oCC.Range.Text = oCC.PlaceholderText Then
'do something with the content control
bFound = True
MsgBox oCC.Range.Text
Exit For 'stop looking
End If
End If
Next oCC
If Not bFound Then MsgBox "Control not found"
lbl_Exit:
Set oDoc = Nothing
Set oCC = Nothing
Exit Sub
End Sub

gmaxey
04-01-2016, 04:18 AM
There is nothing wrong with Graham's methods. Just be aware that when using the title or tag method that you must know the item number of the CC:


ActiveDocument.SelectContentControlsByTitle("Some Title").Item(1).Range.Text = "XYZ"
ActiveDocument.SelectContentControlsByTag("Some Tag").Item(1).Range.Text = "XYZ"

a basic loop method will not determine CCs outside the main text story (i.e., in headers and footers


When a CC is created it gets a unique ID. You can find that ID using my CC Tools Add-In:


http://gregmaxey.mvps.org/word_tip_pages/content_control_tools.html


or selecting the CC title tab and running the following in the immediate window:
Debug.Print Selection.Range.ContentControls(1).ID

So if a document has a particular CC which has the title "Some Title" the tag "Some Tag" and the ID for the first such CC ("Item 1') is 3426135183 as shown in the graphic then each line in the following code references the same CC

Edit: For some reason, I am unable to insert a graphic in this post.

Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
ActiveDocument.SelectContentControlsByTitle("Some Title").Item(1).Range.Text = "XYZ"
ActiveDocument.SelectContentControlsByTag("Some Tag").Item(1).Range.Text = "ABC"
ActiveDocument.ContentControls("3426135183").Range.Text = "123"
lbl_Exit:
Exit Sub
End Sub