Below code will work for update the table of content automatically with Macro popup
DocumentSettingsPart settingsPart = mainPart.Document.MainDocumentPart.GetPartsOfType<DocumentSettingsPart>().First();
UpdateFieldsOnOpen updateFields = new UpdateFieldsOnOpen();
updateFields.Val = BooleanValues.True;
settingsPart.Settings.PrependChild<UpdateFieldsOnOpen>(updateFields);
settingsPart.Settings.Save();