Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

word 2010 VBA to update the Table of Contents 1

Status
Not open for further replies.

remeng

Technical User
Jul 27, 2006
520
US
All;

I have a section of VBA code that worked a while ago, but doesn't any longer. The code hasn't changed. The code is designed to update the Table of Contents within the document. The code works when the document is unlocked but doesn't when it is locked. The rest of the document which utilizes code has not been effected at all. What could be causing the issue and what might the solution be?

Security in place:
VBA Password
Document Password

When the Document password is unlocked the code works
When the VBA password is set it does not effect if the code works or doesn't

Code:
Sub Document_Open()

'Application.ScreenUpdating = False

If ISPM15_Box.Value = True Or FCA_Overseas.Value = True Then

ActiveDocument.Bookmarks("AppendixD").Range.Font.Hidden = False


Else

ActiveDocument.Bookmarks("AppendixD").Range.Font.Hidden = True


End If

Call TofC_Update

'Application.ScreenUpdating = True

End Sub


'________________________________________________________

Private Sub TofC_Update()

' Updates table of contents


ActiveDocument.TablesOfContents(1).Update

Application.ScreenUpdating = True


End Sub

Thanks,

Mike
 
I need to be able to keep the form locked and update it in the locked state. I cannot have the user able to modify the document. The big issue other than that is why it was working before when locked and isn't working now.

Mike
 
I cannot have the user able to modify the document

Then there is no need to "update the Table of Contents" Is there....? [ponder]

Have fun.

---- Andy

There is a great need for a sarcasm font.
 
Andy: There is a difference between the code that drives the process updating the document and the user editing it...

Remeng: Try
Code:
Sub Document_Open()
Application.ScreenUpdating = False
Dim pState As Long: Const Pwd As String = "ThePassword"
With ActiveDocument
  pState = False
  If .ProtectionType <> wdNoProtection Then
    pState = .ProtectionType
    .Unprotect Pwd
  End If
  If (ISPM15_Box.Value = True) Or (FCA_Overseas.Value = True) Then
    .Bookmarks("AppendixD").Range.Font.Hidden = False
  Else
    .Bookmarks("AppendixD").Range.Font.Hidden = True
  End If
  .TablesOfContents(1).Update
  If pState <> wdNoProtection Then .Protect Type:=pState, NoReset:=True, Password:=Pwd
End With
Application.ScreenUpdating = True
End Sub
Substitute your own 'filling in forms' password for 'ThePassword'.

PS: It's about time you took the hint to post VBA questions in the appropriate forum.

Cheers
Paul Edstein
[MS MVP - Word]
 
Hi Paul,

I will use the other thread location in the future after this one is resolved. I received a run time error 5485 "The Password is incorrect" on this section of code.

Code:
If .ProtectionType <> wdNoProtection Then
    pState = .ProtectionType
    .Unprotect Pwd           <------------------
  End If

What could be the cause of it?

Thanks,

Mike
 
Well I resolve the password issue when the document is already open. The table of contents now updates as it should.

When I open the document though it requires the document password. How can I prevent this from happening?

updated on open code:

Code:
ub Document_Open()

Application.ScreenUpdating = False


    If ActiveDocument.ProtectionType <> wdNoProtection Then
    
        ActiveDocument.Unprotect Password:="logspec"
        
    End If

    If ISPM15_Box.Value = True Or FCA_Overseas.Value = True Then

        ActiveDocument.Bookmarks("AppendixD").Range.Font.Hidden = False

    Else

        ActiveDocument.Bookmarks("AppendixD").Range.Font.Hidden = True

    End If

Call TofC_Update


Application.ScreenUpdating = True

End Sub


Private Sub TofC_Update()

' Updates table of contents

Application.ScreenUpdating = False


 If ActiveDocument.ProtectionType <> wdNoProtection Then
    
        ActiveDocument.Unprotect Password:="logspec"
        
    End If

ActiveDocument.TablesOfContents(1).Update


 If ActiveDocument.ProtectionType <> wdNoProtection Then
    
        myDoc.Password = "logspec"
        
    End If

ActiveDocument.Protect Type:=wdAllowOnlyFormFields, Password:="logspec"



End Sub

Just as I think I am done, something else causes an issue...

Mike
 
Update: So I determine that the document permissions some how changed to request the password when it opens. I have changed that so now the document opens without that request. My questions, what VBA code could have changed that?

Thanks,

Mike
 
I think it was in the old code that has since been corrected. Is there a property that would do that?

Mike
 
The code I posted has nothing to do with supplying a password to open the document. If the document is password-protected for opening or editing, you might want to ask yourself why you're doing that to a document to be used by others. The code I posted only addresses the need to unlock the form to update the TOC, for which the 'filling in forms' password is required.

Cheers
Paul Edstein
[MS MVP - Word]
 
Hi Paul,

I agree I am sure it wasn't your code, but it might have been something I tried in the past. I just don't know what it might have been.

Thank you for the continued help.

Mike
 
I have a MAJOR problem. When the form re-locks, all of the text entry boxes reset to the default input!

My theory is that when it relocks it is causing the form to reset due to the locking process.

Code:
Private Sub TofC_Update()

' Updates table of contents

Application.ScreenUpdating = False


 If ActiveDocument.ProtectionType <> wdNoProtection Then
    
        ActiveDocument.Unprotect Password:="logspec"
        
    End If

ActiveDocument.TablesOfContents(1).Update


 If ActiveDocument.ProtectionType <> wdNoProtection Then
    
        myDoc.Password = "logspec"
        
    End If

ActiveDocument.Protect Type:=wdAllowOnlyFormFields, Password:="logspec"



End Sub

when stepping through the document, here is the offending code.

Code:
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, Password:="logspec"

What can I do?????

Thanks!
 
ok... wow I figured it out... thank goodness

original code
Code:
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, Password:="logspec"

corrected code
Code:
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, noreset:=True, Password:="logspec"

this is what was missing

ActiveDocument.Protect Type:=wdAllowOnlyFormFields, noreset:=True, Password:="logspec
 
Maybe you should have had a closer read of the code I posted, which had that parameter. Maybe then you wouldn't have had this problem...

Cheers
Paul Edstein
[MS MVP - Word]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top