chetan Allipur

chetan Allipur

  • NA
  • 541
  • 167.6k

How to update the fields in word using VSTO

Jul 10 2017 1:01 AM
How to update the fields in word using VSTO.
 
Here is my code. I am passing 1 in TandFoption
 
public void CleanTandFcaptionFileds(int TandFoption)
{
Microsoft.Office.Interop.Word.Application app = Globals.ThisAddIn.Application;
Microsoft.Office.Interop.Word.Document doc1 = app.ActiveDocument;
Selection wordSelection = Globals.ThisAddIn.Application.Selection;
int fld_count;
int f;
bool Check;
fld_count = 0;
fld_count = doc1.Fields.Count;
if ((fld_count != 0))
{
if (((TandFoption == 1) || (TandFoption == 2)))
{
Check = true;
f = 0;
// TODO: On Error GoTo Warning!!!: The statement is not translatable
//for (; ((Check = false) == false);)
//{
while ((f < fld_count))
{
f = (f + 1);
if ((((doc1.Fields[f].Type.GetHashCode() == 64) && (doc1.Fields[f].Code.Text.Trim().Substring(0, 23) == "DOCVARIABLE TablePrefix"))
|| (doc1.Fields[f].Code.Text.Trim().Substring(0, 23) == "DOCVARIABLE FigurePrefi")))
{
doc1.Fields[f].Select();
if ((doc1.Fields[f].Next.Type.GetHashCode() == 10))
{
doc1.Fields[f].Next.Delete();
fld_count = (fld_count - 1);
wordSelection.MoveRight(WdUnits.wdCharacter, 1, WdMovementType.wdMove);
if (((wordSelection.Words.First.ToString() == "-") || ((wordSelection.Words.First.ToString() == ".-") || ((wordSelection.Words.First.ToString() == ".--") || (wordSelection.Words.First.ToString() == "--")))))
{
wordSelection.Delete(WdUnits.wdCharacter, 1);
}
}
if ((doc1.Fields[f].Next.Type.GetHashCode() == 12))
{
wordSelection.MoveRight(WdUnits.wdCharacter, 1, WdMovementType.wdMove);
if (((wordSelection.Words.First.ToString() == "-") || ((wordSelection.Words.First.ToString() == ".-") || ((wordSelection.Words.First.ToString() == ".--") || (wordSelection.Words.First.ToString() == "--")))))
{
wordSelection.Delete(WdUnits.wdCharacter, 1);
}
if ((doc1.Fields[f].Code.Text.Trim().Substring(12, 5) == "Table"))
{
if ((doc1.Fields[f].Next.Code.Text.Trim() != "SEQ Table \\* MERGEFORMAT"))
{
doc1.Fields[f].Next.Code.Text = "SEQ Table \\* MERGEFORMAT";
}
}
if ((doc1.Fields[f].Code.Text.Trim().Substring(12, 6) == "Figure"))
{
if ((doc1.Fields[f].Next.Code.Text.Trim() != "SEQ Figure \\* MERGEFORMAT"))
{
doc1.Fields[f].Next.Code.Text = "SEQ Figure \\* MERGEFORMAT";
}
}
}
}
fld_count = doc1.Fields.Count;
if ((f == fld_count))
{
Check = false;
break;
}
}
//}
}
if ((TandFoption == 3))
{
Check = true;
f = 0;
// TODO: On Error GoTo Warning!!!: The statement is not translatable
//for (; ((Check == false) == false);)
//{
while ((f < fld_count))
{
f = (f + 1);
if ((((doc1.Fields[f].Type.GetHashCode() == 64) && (doc1.Fields[f].Code.Text.Trim().Substring(0, 23) == "DOCVARIABLE TablePrefix"))
|| (doc1.Fields[f].Code.Text.Trim().Substring(0, 23) == "DOCVARIABLE FigurePrefix")))
{
doc1.Fields[f].Select();
while (((doc1.Fields[f].Next.Type.GetHashCode() == 10)
|| (doc1.Fields[f].Next.Type == (WdFieldType)12)))
{
doc1.Fields[f].Select();
doc1.Fields[f].Next.Delete();
if ((f + 1 > doc1.Fields.Count))
{
break;
}
}
wordSelection.MoveRight(WdUnits.wdCharacter, 1, WdMovementType.wdMove);
if (((wordSelection.Words.First.ToString() == "-") || ((wordSelection.Words.First.ToString() == ".-") || ((wordSelection.Words.First.ToString() == ".--")
|| (wordSelection.Words.First.ToString() == "--")))))
{
wordSelection.Delete(WdUnits.wdCharacter, 1);
}
if ((doc1.Fields[f].Code.Text.Trim().Substring(12, 5) == "Table"))
{
UpdateTandFcaptionFields("Table", false, true, true);
}
if ((doc1.Fields[f].Code.Text.Trim().Substring(12, 6) == "Figure"))
{
UpdateTandFcaptionFields("Figure", false, true, true);
}
}
fld_count = doc1.Fields.Count;
if ((f == fld_count))
{
Check = false;
break; //Warning!!! Review that break works as 'Exit Do' as it could be in a nested instruction like switch
}
}
//MessageBox.Show("Some Table/Figure captions may not have been updated!" + ("\r\n" + ("Please make sure to verify the all Table/Figure captions." + ("\r\n" + "(especially the last few Tables and Figures)."))), "S-Cubed Templates", MessageBoxButtons.OK, MessageBoxIcon.Information);
//}
}
}
}
 
Please anyone Help me.It is urgent. 

Answers (1)