#1
|
|||
|
|||
ActiveDocument.ContentControls(ID) not working
I am using ActiveDocument.ContentControl(ID) method very frequently and it works finie.
But due to some reason, it does not work in Word 2007 in attached document. ID of that Content Control is 2068831631 But I following statement give error saying, Requested member of collection does not exist Code:
ActiveDocument.ContentControls("2068831631").Range.Select Thanks. |
#2
|
||||
|
||||
Use
Code:
Dim oCC As ContentControl For Each oCC In ActiveDocument.ContentControls If oCC.ID = "2068831631" Then oCC.Range.Select Exit For End If Next oCC
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#3
|
|||
|
|||
I have thought of this code but it might not be good for performance.
Is there any reason why ActiveDocument.ContentControls not working? it works for all other content controls in document, having issue with only this one. And also this content controls works fine on Word 2010 and 2013. So the issue is specific to Word 2007 and only this content control. Thanks for you help. |
#4
|
|||
|
|||
Tejas,
I am not a mathematician or even a general numbers guy. However, If you look at a bunch of CCs created with Word 2007 I believe the majority of them will have IDs 9 digits long (some may have 10 digits) but none with the first left hand number greater than 1. With Word 2010, CCs are commonly 10 digits long an the left hand number can be greater than 1. There is nothing wrong with the CC, only the Word 2007 object doesn't associate the "larger" ID to the CC. Yes the CCs ID is the ID but that ID doesn't mean anything to the CC collection. You will have to error handle e.g.< Code:
Sub DemoIssue() Dim oCC As ContentControl On Error GoTo Err_InvalidID Set oCC = ActiveDocument.ContentControls(ActiveDocument.ContentControls(1).ID) oCC.Range.Select lbl_Exit: Exit Sub Err_InvalidID: Set oCC = W2007CCInvalidID(ActiveDocument.ContentControls(1).ID) Resume Next End Sub Function W2007CCInvalidID(ByRef strID As String) As ContentControl Dim lngValidator As Long Dim oRngStory As Range Dim oCCTest As ContentControl, oShp As Shape 'Handles the ContentControl ID errors introduced with Word 2010. lngValidator = ActiveDocument.Sections(1).Headers(1).Range.StoryType For Each oRngStory In ActiveDocument.StoryRanges 'Iterate through all linked stories Select Case oRngStory.StoryType Case 1 To 11 Do On Error Resume Next For Each oCCTest In oRngStory.ContentControls If oCCTest.ID = strID Then Set W2007CCInvalidID = oCCTest Exit Function End If Next oCCTest Select Case oRngStory.StoryType Case 6, 7, 8, 9, 10, 11 If oRngStory.ShapeRange.Count > 0 Then For Each oShp In oRngStory.ShapeRange On Error GoTo Err_HasText If oShp.TextFrame.HasText Then For Each oCCTest In oShp.TextFrame.TextRange.ContentControls If oCCTest.ID = strID Then Set W2007CCInvalidID = oCCTest Exit Function End If Next oCCTest End If Err_HasText_ReEntry: Next oShp End If Case Else 'Do Nothing End Select On Error GoTo 0 'Get next linked story (if any) Set oRngStory = oRngStory.NextStoryRange Loop Until oRngStory Is Nothing Case Else End Select Next Set oRngStory = Nothing Set oCCTest = Nothing Set oShp = Nothing lbl_Exit: Exit Function Err_HasText: Resume Err_HasText_ReEntry End Function |
Tags |
content control, content controls, vba |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
TOC Not Working | Gary91 | Word | 3 | 01-21-2015 05:52 AM |
Updating grouped ContentControls in Word 2010 | MGerhard | Word VBA | 3 | 08-04-2014 02:34 AM |
wordapp.ActiveDocument.SaveAs Not Working | KSReynolds | Mail Merge | 1 | 07-18-2014 04:03 PM |
Using With ActiveDocument.Tables() | SuzeG | Word VBA | 1 | 01-08-2014 02:00 PM |
Array into ComboBox + Macro-Text into ActiveDocument | Vivi | Word VBA | 1 | 01-27-2010 07:03 AM |