VBA 5097 Error when updating the Content Controls from the user form

Nadia Nikoniuk 0 Reputation points
2023-01-21T02:49:28.16+00:00
Hi,

I have a user form which updates around 650 Content Controls in the Word Document. It works fine when the Track Changes function is disabled. When the user would like to Track Changes they are getting an error message 5097 in SubmitEndorsement function. Content Control labels are not locked in the file.

Private Sub b_Submit_Click() 
On Error GoTo ErrHandler 
For Each openDoc In Application.Documents 
openDoc.Activate 
If ActiveDocument.SelectContentControlsByTag("UniqueDocN").Count > 0 Then 
If ActiveDocument.SelectContentControlsByTag("UniqueDocN").Item(1).Range.Text = UniqueDocNumber Then 

SubmitEndorsement 

End If 
End If 
Next openDoc 
Adios: 
Exit Sub 
ErrHandler: 
MsgBox "Uh oh - an error has occurred with Function UserForm_Submit ." 
Resume Adios 
Resume 
End Sub


Sub SubmitEndorsement()
On Error GoTo ErrHandler
Call updateControl("CCProgramme", "txt_Programme")
Adios:
Exit Sub
ErrHandler:
MsgBox "Uh oh - an error has occurred with Function SubmitEndorsement."
Resume Adios
Resume
End Sub
Sub updateControl(ctl_name As String, txtBox_name As String) 
With ActiveDocument 
If UserForm.Controls(txtBox_name).value <> .SelectContentControlsByTag(ctl_name).Item(1).Range.Text Then 
     .SelectContentControlsByTag(ctl_name).Item(1).Range.Text = UserForm.Controls(txtBox_name).value 
    End If 
End With 
End Sub

Microsoft 365
Microsoft 365
Formerly Office 365, is a line of subscription services offered by Microsoft which adds to and includes the Microsoft Office product line.
3,766 questions
Word
Word
A family of Microsoft word processing software products for creating web, email, and print documents.
657 questions
Office Development
Office Development
Office: A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.Development: The process of researching, productizing, and refining new or existing technologies.
3,479 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Oskar Shon 866 Reputation points MVP
    2023-02-05T16:04:51.8866667+00:00

    Why try checking exists SelectContentControlsByTag("UniqueDocN")

    and then you want to use another "CCProgramme", "txt_Programme" to modified

    Regards

    0 comments No comments

  2. Nadia Nikoniuk 0 Reputation points
    2023-02-05T18:42:05.32+00:00

    SelectContentControlsByTag("UniqueDocN") is checking the uniqueId to update the correct file. For example I have 2 files with the content control "UniqueDocN" open and only the file which has a correct "UniqueDocN" will be updated.

    0 comments No comments