How to lock/unlock ContentControls in ALL Footers
Hello,
i would like to have 2 functions to lock and unlock Contentcontrols in all Footers in my Word Document.
The syntax to group and lock is:
Code:
Selection.Range.ContentControls.Add (wdContentControlGroup)
Selection.ParentContentControl.LockContentControl = True
I have written this, but I have a problem with footers on different pages:
Code:
Application.ScreenUpdating = False
Dim Sctn As Section, HdFt As HeaderFooter
With ActiveDocument
For Each Sctn In .Sections
MsgBox (Sctn.Index)
For Each HdFt In Sctn.Headers
With HdFt
If .LinkToPrevious = False Then
With .Range
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
For Each fieldX In .Fields
If InStr(1, fieldX.Code.Text, "KLASSIFIZIERUNG", 1) > 0 Then
fieldX.Select
Selection.Range.ContentControls.Add (wdContentControlGroup)
Selection.ParentContentControl.LockContentControl = True
End If
Next fieldX
End With
End If
End With
Next
For Each HdFt In Sctn.Footers
With HdFt
If .LinkToPrevious = False Then
With .Range
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
For Each fieldX In .Fields
If InStr(1, fieldX.Code.Text, "KLASSIFIZIERUNG", 1) > 0 Then
fieldX.Select
Selection.Range.ContentControls.Add (wdContentControlGroup)
Selection.ParentContentControl.LockContentControl = True
End If
Next fieldX
End With
End If
End With
Next
Next
End With
Also another approach did not work:
Code:
Dim oHFKL As HeaderFooter
Dim fieldX As Field
ActiveWindow.ActivePane.View.SeekView = wdSeekFirstPageFooter
For Each fieldX In ActiveDocument.Sections(0).Footers(wdHeaderFooterFirstPage).Range.Fields
If InStr(1, fieldX.Code.Text, "KLASSIFIZIERUNG", 1) > 0 Then
fieldX.Select
Selection.Range.ContentControls.Add (wdContentControlGroup)
Selection.ParentContentControl.LockContentControl = True
End If
Next fieldX
as I do not know if I have to use wdHeaderFooterFirstPage or wdHeaderFooterPrimaryPage in the section 0,1,2,..and so on..
I also get a display problem (ActiveWindow.ActivePane.View.Type switches to 1 and does not remain in status 3) when using this function above.
The fields in my footers are VBA Docvariables with "KLASSIFIZIERUNG" (engl: classification) within, so thats why I check the field.Code.Text by Instr()
Any help? Any ideas?
best regards,
Andreas
Thanks, but the View is still changing during this sub()
Quote:
Originally Posted by
gmaxey
Code:
Sub LockCC()
SetCCStateInFooters True
lbl_Exit:
Exit Sub
End Sub
Sub UnlockCC()
SetCCStateInFooters False
lbl_Exit:
Exit Sub
End Sub
Public Sub SetCCStateInFooters(bState As Boolean)
Dim rngStory As Word.Range
Dim lngJunk As Long
Dim oShp As Shape, oCanShp As Shape
lngJunk = ActiveDocument.Sections(1).Headers(1).Range.StoryType
Dim oCC As ContentControl
For Each rngStory In ActiveDocument.StoryRanges
'Iterate through all linked stories
Do
On Error Resume Next
Select Case rngStory.StoryType
Case 8, 9, 11
For Each oCC In rngStory.ContentControls
oCC.LockContentControl = bState
Next oCC
If rngStory.ShapeRange.Count > 0 Then
For Each oShp In rngStory.ShapeRange
If oShp.TextFrame.HasText Then
For Each oCC In oShp.TextFrame.TextRange.ContentControls
oCC.LockContentControl = bState
Next oCC
End If
If oShp.Type = msoCanvas Then
For Each oCanShp In oShp.CanvasItems
If oCanShp.TextFrame.HasText Then
For Each oCC In oCanShp.TextFrame.TextRange.ContentControls
oCC.LockContentControl = bState
Next oCC
End If
Next oCanShp
End If
Next
End If
Case Else
'Do Nothing
End Select
On Error GoTo 0
'Get next linked story (if any)
Set rngStory = rngStory.NextStoryRange
Loop Until rngStory Is Nothing
Next
lbl_Exit:
Exit Sub
End Sub
Hi,
the code works fine, but If I am in the printView View the code changes to Draftview during execution and back to printView afterwards. How can I prevent this?
best regards,
Andreas
Object or Method not found...
Quote:
Originally Posted by
gmayor
You don't need to select controls or story ranges in order to process them. Selecting items in the footer will open the footer, hence the problem you had.
You need to use instead ranges.
You can process the document range instead
Code:
With ActiveDocument.Range
.LanguageID = wdGerman
.NoProofing = True
End With
Greg's code creates ranges for the headers and footers so you can process those ranges also
Code:
Select Case rngStory.StoryType Case 8, 9, 11
With rngStory
.LanguageID = wdGerman
.NoProofing = True
End With
For Each oCC In rngStory.ContentControls
oCC.LockContentControl = bState
Next oCC
Hello,
thanks for your response. It works almost perfect I guess, BUT:
Code:
oCC.LockContentControl = bState
brings an error:
Error while compiling:
Method or Dataobject not found
And how may I group the oCC, I was using:
Code:
Selection.Range.ContentControls.Add (wdContentControlGroup)
without "select"?
Thanks a lot for your help!!
best regards,
Andreas