Word VBA >> Find where ContentControl is located

by John » Tue, 14 Oct 2008 00:42:36 GMT

Hi,

I have a content control in a table and am using the
Document_ControlControlOnExit event to do some post processing. Can anyone
tell me how I can get a reference to the table or better, the row, that the
control is sitting in? I thought the Parent property might return the table
but it only returns the Document.

Any clues?

Thanks

John



Word VBA >> Find where ContentControl is located

by Greg Maxey » Tue, 14 Oct 2008 01:45:42 GMT


John,

Can you provide details on what specifically you are trying to do?

General you can use the CC.ID or CC.Title to perform processing regarless of
where the CC is physically located.

To get the unique ID of a CC you can select it in the document and then run
this bit of code:

Option Explicit
Sub GetUniqueID()
Dim i As Long
Dim currentCC As Word.ContentControl
Dim oCC As Word.ContentControl
'You have to click the CC tab to select a specific control
Set currentCC = Selection.ContentControls(1)
MsgBox currentCC.ID
End Sub

You can then work with the CC by ID as follows:

Sub WorkWithCCByID()
Dim oCC As ContentControl
Set oCC = ActiveDocument.ContentControls("59279876") 'the number is the
unique ID found above
oCC.Range.Text = "Some text here"
End Sub

Or if you have assigned a title to a CC you can work with it like this:

Sub WorkWithCCbyTitle()
Dim oCC As ContentControl
Set oCC =
ActiveDocument.SelectContentControlsByTitle("CC_ClientName").Item(1)
oCC.Range.Text = "Some text here"
End Sub

You can use the ContentControl (abreviated to CC here) argument in the
onexit event to work with any CC as follows:

Private Sub Document_ContentControlOnExit(ByVal CC As ContentControl, Cancel
As Boolean)
'Work by referencing ID
Select Case CC.ID
Case Is = "59279876"
CC.Range.Text = "Some New Text here"
Case Else
MsgBox CC.ID
End Select
'Work by referencing Title
Select Case CC.Title
Case Is = "CC_ClientName"
If CC.Range.Text = "Some text here" Then
CC.Range.Text = "Some new text here"
End If
Case Else
MsgBox CC.ID
End Select
End Sub

Note that there is a very annoying bug in the CCOnExit event. It will
commonly fire twice (on entry and on exit) if the Developer tab is not the
active ribbon tab. The only suitable work around that I have found so far
is to ensure that the Developer tab is the active tab when navigating
through CCs. You can do this with the Windows API keybd_event and the
ContentControlOnEnter event as follows. Basically this just an automated
way of pressing Alt, L, Alt to call the Developer tab.

Option Explicit
Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal _
bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
Private Const KEYEVENTF_KEYUP = &H2
Private Const VK_MENU = &H12
Private Const VK_L = &H4C

Private Sub Document_ContentControlOnEnter(ByVal ContentControl As
ContentControl)
MakeDevTabActive
End Sub

Public Sub MakeDevTabActive()
keybd_event VK_MENU, 0, 0, 0 'Press ALT
keybd_event VK_MENU, 0, KEYEVENTF_KEYUP, 0 'Release ALT
keybd_event VK_L, 0, 0, 0 'Press L
keybd_event VK_L, 0, 2, 0 'Release L
keybd_event VK_MENU, 0, 0, 0 'Press ALT
keybd_event VK_MENU, 0, KEYEVENTF_KEYUP, 0 'Release ALT
End Sub














--
Greg Maxey - Word MVP

My web site http://gregmaxey.mvps.org
Word MVP web site http://word.mvps.org

McCain/Palin '08 !!!





Word VBA >> Find where ContentControl is located

by John » Tue, 14 Oct 2008 06:26:25 GMT

ello Greg,

Thanks very much for the comprehsive reply. I'm particularly interested in
the OnExit event firing twice.

With my current issue I have a table with a drop down content control in
each row. I'm trying to get the row that the respective content control
OnExit event was fired in so that I can then change the row backround color,
hence my looking at the Pernet property. Is that possible do you think?

Best regards

John

"Greg Maxey" < XXXX@XXXXX.COM > wrote in message
news: XXXX@XXXXX.COM ...



Find where ContentControl is located

by Greg Maxey » Tue, 14 Oct 2008 07:19:20 GMT

o you want to change the current row background color based on what user
selects in that rows CC.

Private Sub Document_ContentControlOnExit(ByVal CC As ContentControl, Cancel
As Boolean)
Dim i As Long
i = CC.Range.Information(wdEndOfRangeRowNumber)
Select Case CC.Range.Text
Case Is = "Red"
Selection.Tables(1).Rows(i).Shading.BackgroundPatternColor = wdColorRed
Case Is = "Blue"
Selection.Tables(1).Rows(i).Shading.BackgroundPatternColor = wdColorBlue
Case Is = "Green"
Selection.Tables(1).Rows(i).Shading.BackgroundPatternColor =
wdColorGreen
End Select
End Sub

Note: that the event will fire twice because of the bug, but in this case it
is transparent.




John wrote:

--
Greg Maxey - Word MVP

My web site http://gregmaxey.mvps.org
Word MVP web site http://word.mvps.org

McCain/Palin '08 !!!




Find where ContentControl is located

by John » Tue, 14 Oct 2008 16:24:54 GMT

erfect! Thanks very much for your time Greg.

Best regards

John

"Greg Maxey" < XXXX@XXXXX.COM > wrote in message
news: XXXX@XXXXX.COM ...



Find where ContentControl is located

by John » Tue, 14 Oct 2008 23:48:25 GMT

i Greg,

This all worked out fine, with one exception - I found that if, after
selecting the control, your next click was outside of the table, the
Selection.Tables(1) ... returned a different table. I guess by the time the
event has fired the Selection no longer refers to the table that housed the
control?

In the end I've gone for CC.Range.Tables(1). and although this appears to
work and returns the CC's table I'm mildly puzzled that the CC.Range
property has any tables at all. After all, I embedded the CC in a table
cell, not the other way around.

Anyway, no doubt there's a good reason for it somewhere :)

Thanks again

John


"John" < XXXX@XXXXX.COM > wrote in message
news: XXXX@XXXXX.COM ...



Find where ContentControl is located

by Greg Maxey » Wed, 15 Oct 2008 02:40:39 GMT

ohn,

You are right. This does seem to work correctly:

Private Sub Document_ContentControlOnExit(ByVal CC As ContentControl, Cancel
As Boolean)
Dim i As Long
i = CC.Range.Information(wdEndOfRangeRowNumber)
Select Case CC.Range.Text
Case Is = "Red"
CC.Range.Tables(1).Rows(i).Shading.BackgroundPatternColor = wdColorRed
Case Is = "Blue"
CC.Range.Tables(1).Rows(i).Shading.BackgroundPatternColor = wdColorBlue
Case Is = "Green"
CC.Range.Tables(1).Rows(i).Shading.BackgroundPatternColor = wdColorGreen
End Select
End Sub

I don't have a technical explanation. But then I suppose it makes as much
sense as Selection.Tables(1). After all the selection is in the table and
not the other way around ;-)




John wrote:

--
Greg Maxey - Word MVP

My web site http://gregmaxey.mvps.org
Word MVP web site http://word.mvps.org

McCain/Palin '08 !!!




Similar Threads

1. locate a file using vba - I am stack

2. Where do I locate send when I am finish typing up my email in outl


3. I am trying to locate a template compatible with avery label #5366 - Word Document management

4. Automatic highlighting of text located with "Find"

In Office Word 2003, is there a way to run a search for a word in a document 
and have each occurrence of that word automatically highlighted?

5. Find text in doc, and paste chart where text is located - Office Word Programming

6. Find text in doc, and paste chart where text is located Options

I have the basics but I am obviously missing something.

find text:


myRange.Find.Execute FindText:="%PLOT_1"


paste plot:
        Set WDApp = GetObject(, "Word.Application") ' Reference
active
document
        Set WDDoc = WDApp.ActiveDocument ' Copy chart as a picture
        ActiveChart.CopyPicture Appearance:=xlScreen, Size:=xlScreen,
_
        Format:=xlPicture ' Paste chart at cursor position
        WDApp.Selection.PasteSpecial Link:=False,
DataType:=wdPasteMetafilePicture, _
        Placement:=wdInLine, DisplayAsIcon:=False ' Clean up
        Set WDDoc = Nothing
        Set WDApp = Nothing


basically: i need a way to set Placement:= location(%PLOT_1)


but i am unsure how to do this.
any help would be great.
thanks!

7. Word 2003 Using Find to locate field brackets - Word Document management

8. Excel VBA .Find method - am I clueless?