macro for updating TOC page numbers

  • Thread starter Thread starter linda
  • Start date Start date
L

linda

I have a protected form, and I know that page numbers for the TOC sometimes
do not get updated correctly with protected forms. Does anyone have a
sample macro that would work for this? I am not good with macros, so any
helo would be apppreciated.

Thanks,

Linda.
 
linda said:
I have a protected form, and I know that page numbers for the TOC
sometimes do not get updated correctly with protected forms. Does
anyone have a sample macro that would work for this? I am not good
with macros, so any helo would be apppreciated.

Thanks,

Linda.

Hi Linda,

The macro needs to unprotect the document, do the update, and reprotect the
document. Here's sample code:

Public Sub UpdateTOC()
Dim oTOC As TableOfContents
Dim oldProt As WdProtectionType

'IMPORTANT: If the document protection
'includes a password, add the password
'as a parameter to the .Unprotect and
'.Protect calls.

'save the existing protection type
oldProt = ActiveDocument.ProtectionType
If oldProt <> wdNoProtection Then
ActiveDocument.Unprotect ' Password:="password"
End If

For Each oTOC In ActiveDocument.TablesOfContents
oTOC.Update
Next oTOC

'restore the old protection
If oldProt <> wdNoProtection Then
ActiveDocument.Protect Type:=oldProt, _
NoReset:=True
' , Password:="password"
End If
End Sub
 
Back
Top