C# Snippets


Delete

public void TOC_Delete(ref Word.Document objDocument, string sDocumentType, bool bInformUser = false)
{
bool bdocumentprotected;
Word.Range oRange;
int iStart;
int iEnd;

try
{
Tracer_Add2("SUBROUTINE", System.Reflection.MethodBase.GetCurrentMethod().Name + " start");
if (My.Settings.ERROR_OCCURRED == true)
return;

if ((modSpecific.Document_IsValidAndProtected(objDocument) == true))
{
bdocumentprotected = true;
modWordObjectModel.Document_Unprotect(objDocument, gsDOCUMENTPASSWORD, false);
}

if (objDocument.TablesOfContents.Count > 0 & modWordObjectModel.Bookmark_Exists(objDocument, "BC_TOC_START") == false)
{
modMessages.TOC_BookmarkMissingDeleteManually("BC_TOC_START");
return;
}

if (objDocument.TablesOfContents.Count > 0 & modWordObjectModel.Bookmark_Exists(objDocument, "BC_TOC_END") == false)
{
modMessages.TOC_BookmarkMissingDeleteManually("BC_TOC_END");
return;
}

if ((modWordObjectModel.TOC_Exists(objDocument) == true))
{
iStart = objDocument.Bookmarks("BC_TOC_START").Start;
iEnd = objDocument.Bookmarks("BC_TOC_END").End;

oRange = objDocument.Range(Start: (object)iStart, End: (object)iEnd);
oRange.Select();
oRange.Delete();

objDocument.Bookmarks("BC_TOC_START").Delete();
objDocument.Bookmarks("BC_TOC_END").Delete();

gApplicationWord.Selection.Delete(Unit: Word.WdUnits.wdCharacter, Count: 1);

if ((bInformUser == true))
modMessages.TOC_Deleted();
}
else
modMessages.TOC_DoesNotExist();
}

catch (Exception ex)
{
modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod(), null/* TODO Change to default(_) if this is not a reference type */, ex);
}
finally
{
if ((bdocumentprotected == true))
modWordObjectModel.Document_Protect(objDocument, gsDOCUMENTPASSWORD, false);
}
}
Public Sub TOC_Delete(ByRef objDocument As Word.Document, _
ByVal sDocumentType As String, _
Optional ByVal bInformUser As Boolean = False)

Dim bdocumentprotected As Boolean
Dim oRange As Word.Range
Dim iStart As Integer
Dim iEnd As Integer

Try
Call Tracer_Add2("SUBROUTINE", System.Reflection.MethodBase.GetCurrentMethod.Name & " start")
If My.Settings.ERROR_OCCURRED = True Then Exit Sub

If (modSpecific.Document_IsValidAndProtected(objDocument) = True) Then
bdocumentprotected = True
Call modWordObjectModel.Document_Unprotect(objDocument, gsDOCUMENTPASSWORD, False)
End If

If objDocument.TablesOfContents.Count > 0 And _
modWordObjectModel.Bookmark_Exists(objDocument, "BC_TOC_START") = False Then

Call modMessages.TOC_BookmarkMissingDeleteManually("BC_TOC_START")
Exit Sub
End If

If objDocument.TablesOfContents.Count > 0 And _
modWordObjectModel.Bookmark_Exists(objDocument, "BC_TOC_END") = False Then

Call modMessages.TOC_BookmarkMissingDeleteManually("BC_TOC_END")
Exit Sub
End If

If (modWordObjectModel.TOC_Exists(objDocument) = True) Then

iStart = objDocument.Bookmarks("BC_TOC_START").Start
iEnd = objDocument.Bookmarks("BC_TOC_END").End

oRange = objDocument.Range(Start:=CType(iStart, Object), _
End:=CType(iEnd, Object))
oRange.Select()
oRange.Delete()

objDocument.Bookmarks("BC_TOC_START").Delete()
objDocument.Bookmarks("BC_TOC_END").Delete()

gApplicationWord.Selection.Delete(Unit:=Word.WdUnits.wdCharacter, Count:=1)

If (bInformUser = True) Then
Call modMessages.TOC_Deleted()
End If

Else
Call modMessages.TOC_DoesNotExist()
End If


Catch ex As System.Exception
Call modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod, Nothing, ex)
Finally
If (bdocumentprotected = True) Then
Call modWordObjectModel.Document_Protect(objDocument, gsDOCUMENTPASSWORD, False)
End If
End Try
End Sub

Exists

public bool References_CaptionExists(ref Word.Document objDocument, string sCaptionName)
{
Word.CaptionLabel objCaptionLabel;

try
{
Tracer_Add2("SUBROUTINE", System.Reflection.MethodBase.GetCurrentMethod().Name + " start");
if (My.Settings.ERROR_OCCURRED == true)
return;

return false;
foreach (var objCaptionLabel in gApplicationWord.CaptionLabels)
{
if (objCaptionLabel.Name == sCaptionName)
return true;
}
}
catch (Exception ex)
{
modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod(), null/* TODO Change to default(_) if this is not a reference type */, ex);
}
}
Public Function References_CaptionExists(ByRef objDocument As Word.Document, _
ByVal sCaptionName As String) As Boolean

Dim objCaptionLabel As Word.CaptionLabel

Try
Call Tracer_Add2("SUBROUTINE", System.Reflection.MethodBase.GetCurrentMethod.Name & " start")
If My.Settings.ERROR_OCCURRED = True Then Exit Function

Return False
For Each objCaptionLabel In gApplicationWord.CaptionLabels
If objCaptionLabel.Name = sCaptionName Then
Return True
End If
Next objCaptionLabel

Catch ex As System.Exception
Call modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod, Nothing, ex)
End Try
End Function

Exists

public bool TOC_Exists(ref Word.Document objDocument, bool bInformUser = false)
{
try
{
Tracer_Add2("SUBROUTINE", System.Reflection.MethodBase.GetCurrentMethod().Name + " start");
if (My.Settings.ERROR_OCCURRED == true)
return;

if ((objDocument.TablesOfContents.Count > 0))
return true;
else
{
if ((bInformUser == true))
modMessages.TOC_DoesNotExist();
return false;
}
}
catch (Exception ex)
{
modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod(), null/* TODO Change to default(_) if this is not a reference type */, ex);
}
}
Public Function TOC_Exists(ByRef objDocument As Word.Document, _
Optional ByVal bInformUser As Boolean = False) As Boolean
Try
Call Tracer_Add2("SUBROUTINE", System.Reflection.MethodBase.GetCurrentMethod.Name & " start")
If My.Settings.ERROR_OCCURRED = True Then Exit Function

If (objDocument.TablesOfContents.Count > 0) Then
Return True
Else
If (bInformUser = True) Then
Call modMessages.TOC_DoesNotExist()
End If
Return False
End If

Catch ex As System.Exception
Call modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod, Nothing, ex)
End Try
End Function

Figure_Insert

public void Figure_Insert(ref Word.Document objDocument)
{
try
{
Tracer_Add2("SUBROUTINE", System.Reflection.MethodBase.GetCurrentMethod().Name + " start");
if (My.Settings.ERROR_OCCURRED == true)
return;

gApplicationWord.Selection.TypeText(Text: "Figures");
gApplicationWord.Selection.Style = objDocument.Styles("B-TOC Section Heading No Break");
gApplicationWord.Selection.Collapse();

{
var withBlock = objDocument.Styles("TOC 4");
withBlock.AutomaticallyUpdate = true;
withBlock.BaseStyle = "B-TOC Figures";
withBlock.NextParagraphStyle = "Normal";
}

{
var withBlock = objDocument;
withBlock.TablesOfContents.Add(Range: gApplicationWord.Selection.Range, RightAlignPageNumbers: true, UseHeadingStyles: false, IncludePageNumbers: true, AddedStyles: "B-Chart Title,2,B-Chart Title. Full Width,2,B-Table Title,2,B-Table Title. Full Width,2,B-Chart Holder Title,2", UseHyperlinks: true, HidePageNumbersInWeb: true, UseOutlineLevels: false);

withBlock.TablesOfContents.Format = Word.WdTocFormat.wdTOCTemplate;
}
}
catch (Exception ex)
{
modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod(), null/* TODO Change to default(_) if this is not a reference type */, ex);
}
}
Public Sub Figure_Insert(ByRef objDocument As Word.Document)

Try
Call Tracer_Add2("SUBROUTINE", System.Reflection.MethodBase.GetCurrentMethod.Name & " start")
If My.Settings.ERROR_OCCURRED = True Then Exit Sub

gApplicationWord.Selection.TypeText(Text:="Figures")
gApplicationWord.Selection.Style = objDocument.Styles("B-TOC Section Heading No Break")
gApplicationWord.Selection.Collapse()

With objDocument.Styles("TOC 4")
.AutomaticallyUpdate = True
.BaseStyle = "B-TOC Figures"
.NextParagraphStyle = "Normal"
End With

With objDocument
.TablesOfContents.Add(Range:=gApplicationWord.Selection.Range, _
RightAlignPageNumbers:=True, _
UseHeadingStyles:=False, _
IncludePageNumbers:=True, _
AddedStyles:="B-Chart Title,2,B-Chart Title. Full Width,2,B-Table Title,2,B-Table Title. Full Width,2,B-Chart Holder Title,2", _
UseHyperlinks:=True, _
HidePageNumbersInWeb:=True, _
UseOutlineLevels:=False)

.TablesOfContents.Format = Word.WdTocFormat.wdTOCTemplate
End With

Catch ex As System.Exception
Call modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod, Nothing, ex)
End Try
End Sub

Insert

public Word.Range Caption_Insert(ref Word.Document objDocument, string sStyleName)
{
try
{
Tracer_Add2("SUBROUTINE", System.Reflection.MethodBase.GetCurrentMethod().Name + " start");
if (My.Settings.ERROR_OCCURRED == true)
{
return null/* TODO Change to default(_) if this is not a reference type */; return;
}

if ((objDocument == null))
objDocument = Document_GetActive();

if (modWordObjectModel.Caption_Exists(objDocument, "Figure") == false)
gApplicationWord.CaptionLabels.Add(Name: "Figure");

{
var withBlock = gApplicationWord.CaptionLabels("Figure");
withBlock.NumberStyle = Word.WdCaptionNumberStyle.wdCaptionNumberStyleArabic;
withBlock.IncludeChapterNumber = false;
}

{
var withBlock = gApplicationWord.Selection;
withBlock.InsertCaption(Label: "Figure", TitleAutoText: "InsertCaption1", Title: "", Position: Word.WdCaptionPosition.wdCaptionPositionBelow);

withBlock.Style = objDocument.Styles(sStyleName);
withBlock.TypeText(Text: ": [Caption]");
withBlock.HomeKey(Unit: Word.WdUnits.wdLine, Extend: Word.WdMovementType.wdExtend);

return gApplicationWord.Selection.Range;

withBlock.EndKey(Unit: Word.WdUnits.wdLine);
}
}
catch (Exception ex)
{
modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod(), null/* TODO Change to default(_) if this is not a reference type */, ex);
return null/* TODO Change to default(_) if this is not a reference type */;
}
}
Public Function Caption_Insert(ByRef objDocument As Word.Document, _
ByVal sStyleName As String) As Word.Range

Try
Call Tracer_Add2("SUBROUTINE", System.Reflection.MethodBase.GetCurrentMethod.Name & " start")
If My.Settings.ERROR_OCCURRED = True Then Return Nothing : Exit Function

If (objDocument Is Nothing) Then objDocument = Document_GetActive()

If modWordObjectModel.Caption_Exists(objDocument, "Figure") = False Then
gApplicationWord.CaptionLabels.Add(Name:="Figure")
End If

With gApplicationWord.CaptionLabels("Figure")
.NumberStyle = Word.WdCaptionNumberStyle.wdCaptionNumberStyleArabic
.IncludeChapterNumber = False
End With

With gApplicationWord.Selection
.InsertCaption(Label:="Figure", _
TitleAutoText:="InsertCaption1", _
Title:="", _
Position:=Word.WdCaptionPosition.wdCaptionPositionBelow)

.Style = objDocument.Styles(sStyleName)
.TypeText(Text:=": [Caption]")
.HomeKey(Unit:=Word.WdUnits.wdLine, _
Extend:=Word.WdMovementType.wdExtend)

Return gApplicationWord.Selection.Range

.EndKey(Unit:=Word.WdUnits.wdLine)
End With

Catch ex As System.Exception
Call modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod, Nothing, ex)
Return Nothing
End Try
End Function

Insert

public void HeaderFooter_FootnoteInsert(ref Word.Document objDocument)
{
bool bDocumentWasProtected;

try
{
Tracer_Add2("SUBROUTINE", System.Reflection.MethodBase.GetCurrentMethod().Name + " start");
if (My.Settings.ERROR_OCCURRED == true)
return;

if ((modSpecific.Document_IsValidAndProtected(objDocument) == true))
{
bDocumentWasProtected = true;
modWordObjectModel.Document_Unprotect(objDocument, gsDOCUMENTPASSWORD, false);
}

gApplicationWord.Selection.Footnotes.Add(Range: gApplicationWord.Selection.Range, Reference: "");
gApplicationWord.Selection.Expand(Word.WdUnits.wdParagraph);

if ((gApplicationWord.Selection.Sections(1).Index == 1))
{
if ((Style_ExistsInDocument(objDocument, "FootnoteWide") == true))
gApplicationWord.Selection.Style = objDocument.Styles("FootnoteWide");
gApplicationWord.Selection.Sections(1).PageSetup.BottomMargin = gApplicationWord.InchesToPoints(0.88);
}

if ((gApplicationWord.Selection.Sections(1).Index > 1))
{
if ((Style_ExistsInDocument(objDocument, "Footnote Text") == true))
gApplicationWord.Selection.Style = objDocument.Styles("Footnote Text");
gApplicationWord.Selection.Sections(1).PageSetup.BottomMargin = gApplicationWord.InchesToPoints(0.88);
}

gApplicationWord.Selection.Collapse(Word.WdCollapseDirection.wdCollapseStart);
gApplicationWord.Selection.Move(Word.WdUnits.wdWord);
}
catch (Exception ex)
{
modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod(), null/* TODO Change to default(_) if this is not a reference type */, ex);
}
finally
{
if ((bDocumentWasProtected == true))
modWordObjectModel.Document_Protect(objDocument, gsDOCUMENTPASSWORD, false);
}
}
Public Sub HeaderFooter_FootnoteInsert(ByRef objDocument As Word.Document)
Dim bDocumentWasProtected As Boolean

Try
Call Tracer_Add2("SUBROUTINE", System.Reflection.MethodBase.GetCurrentMethod.Name & " start")
If My.Settings.ERROR_OCCURRED = True Then Exit Sub

If (modSpecific.Document_IsValidAndProtected(objDocument) = True) Then
bDocumentWasProtected = True
Call modWordObjectModel.Document_Unprotect(objDocument, gsDOCUMENTPASSWORD, False)
End If

gApplicationWord.Selection.Footnotes.Add(Range:=gApplicationWord.Selection.Range, _
Reference:="")
gApplicationWord.Selection.Expand(Word.WdUnits.wdParagraph)

If (gApplicationWord.Selection.Sections(1).Index = 1) Then
If (Style_ExistsInDocument(objDocument, "FootnoteWide") = True) Then
gApplicationWord.Selection.Style = objDocument.Styles("FootnoteWide")
End If
gApplicationWord.Selection.Sections(1).PageSetup.BottomMargin = gApplicationWord.InchesToPoints(0.88)
End If

If (gApplicationWord.Selection.Sections(1).Index > 1) Then
If (Style_ExistsInDocument(objDocument, "Footnote Text") = True) Then
gApplicationWord.Selection.Style = objDocument.Styles("Footnote Text")
End If
gApplicationWord.Selection.Sections(1).PageSetup.BottomMargin = gApplicationWord.InchesToPoints(0.88)
End If

gApplicationWord.Selection.Collapse(Word.WdCollapseDirection.wdCollapseStart)
gApplicationWord.Selection.Move(Word.WdUnits.wdWord)

Catch ex As System.Exception
Call modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod, Nothing, ex)
Finally
If (bDocumentWasProtected = True) Then
Call modWordObjectModel.Document_Protect(objDocument, gsDOCUMENTPASSWORD, False)
End If
End Try
End Sub

Insert

public void TOC_Insert(ref Word.Document objDocument, int iLevel, bool bIncludeFigures)
{
Word.WdPageFit objCurrentPageFit;
int iCurrentViewPercentage;
bool bdocumentprotected;

try
{
Tracer_Add2("SUBROUTINE", System.Reflection.MethodBase.GetCurrentMethod().Name + " start");
if (My.Settings.ERROR_OCCURRED == true)
return;

if ((modSpecific.Document_IsValidAndProtected(objDocument) == true))
{
bdocumentprotected = true;
modWordObjectModel.Document_Unprotect(objDocument, gsDOCUMENTPASSWORD, false);
}

objCurrentPageFit = gApplicationWord.ActiveWindow.ActivePane.View.Zoom.PageFit;
iCurrentViewPercentage = gApplicationWord.ActiveWindow.ActivePane.View.Zoom.Percentage;

gApplicationWord.ActiveWindow.ActivePane.View.Zoom.PageFit = Word.WdPageFit.wdPageFitFullPage;

if ((objDocument.TablesOfContents.Count > 0))
{
modMessages.TOC_AlreadyExists();
return;
}

if ((gApplicationWord.Selection.PageSetup.RightMargin < 200))
{
modWordObjectModel.TOC_InsertLevel(objDocument, iLevel);

if ((bIncludeFigures == true))
modWordObjectModel.TOC_InsertFigures(objDocument);
}

if ((gApplicationWord.Selection.PageSetup.RightMargin > 200))
modWordObjectModel.TOC_InsertLevel(objDocument, iLevel);

gApplicationWord.Selection.Bookmarks.Add(Range: gApplicationWord.Selection.Range, Name: "BC_TOC_END");

gApplicationWord.ActiveWindow.ActivePane.View.Zoom.PageFit = objCurrentPageFit;
gApplicationWord.ActiveWindow.ActivePane.View.Zoom.Percentage = iCurrentViewPercentage;
}
catch (Exception ex)
{
modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod(), null/* TODO Change to default(_) if this is not a reference type */, ex);
}
finally
{
if ((bdocumentprotected == true))
modWordObjectModel.Document_Protect(objDocument, gsDOCUMENTPASSWORD, false);
}
}
Public Sub TOC_Insert(ByRef objDocument As Word.Document, _
ByVal iLevel As Integer, _
ByVal bIncludeFigures As Boolean)

Dim objCurrentPageFit As Word.WdPageFit
Dim iCurrentViewPercentage As Integer
Dim bdocumentprotected As Boolean

Try
Call Tracer_Add2("SUBROUTINE", System.Reflection.MethodBase.GetCurrentMethod.Name & " start")
If My.Settings.ERROR_OCCURRED = True Then Exit Sub

If (modSpecific.Document_IsValidAndProtected(objDocument) = True) Then
bdocumentprotected = True
Call modWordObjectModel.Document_Unprotect(objDocument, gsDOCUMENTPASSWORD, False)
End If

objCurrentPageFit = gApplicationWord.ActiveWindow.ActivePane.View.Zoom.PageFit
iCurrentViewPercentage = gApplicationWord.ActiveWindow.ActivePane.View.Zoom.Percentage

gApplicationWord.ActiveWindow.ActivePane.View.Zoom.PageFit = Word.WdPageFit.wdPageFitFullPage

If (objDocument.TablesOfContents.Count > 0) Then
Call modMessages.TOC_AlreadyExists()
Exit Sub
End If

If (gApplicationWord.Selection.PageSetup.RightMargin < 200) Then
Call modWordObjectModel.TOC_InsertLevel(objDocument, iLevel)

If (bIncludeFigures = True) Then
Call modWordObjectModel.TOC_InsertFigures(objDocument)
End If
End If

If (gApplicationWord.Selection.PageSetup.RightMargin > 200) Then
Call modWordObjectModel.TOC_InsertLevel(objDocument, iLevel)
End If

gApplicationWord.Selection.Bookmarks.Add(Range:=gApplicationWord.Selection.Range, Name:="BC_TOC_END")

gApplicationWord.ActiveWindow.ActivePane.View.Zoom.PageFit = objCurrentPageFit
gApplicationWord.ActiveWindow.ActivePane.View.Zoom.Percentage = iCurrentViewPercentage

Catch ex As System.Exception
Call modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod, Nothing, ex)
Finally
If (bdocumentprotected = True) Then
Call modWordObjectModel.Document_Protect(objDocument, gsDOCUMENTPASSWORD, False)
End If
End Try
End Sub

InsertFigures

public void TOC_InsertFigures(ref Word.Document objDocument)
{
try
{
Tracer_Add2("SUBROUTINE", System.Reflection.MethodBase.GetCurrentMethod().Name + " start");
if (My.Settings.ERROR_OCCURRED == true)
return;

gApplicationWord.Selection.TypeText(Text: "Figures");
gApplicationWord.Selection.Style = objDocument.Styles("B-TOC Section Heading No Break");
gApplicationWord.Selection.Collapse();

{
var withBlock = objDocument.Styles("TOC 4");
withBlock.AutomaticallyUpdate = true;
withBlock.BaseStyle = "B-TOC Figures";
withBlock.NextParagraphStyle = "Normal";
}

{
var withBlock = objDocument;
withBlock.TablesOfContents.Add(Range: gApplicationWord.Selection.Range, RightAlignPageNumbers: true, UseHeadingStyles: false, IncludePageNumbers: true, AddedStyles: "B-Chart Title,2,B-Chart Title. Full Width,2,B-Table Title,2,B-Table Title. Full Width,2,B-Chart Holder Title,2", UseHyperlinks: true, HidePageNumbersInWeb: true, UseOutlineLevels: false);
}
}
catch (Exception ex)
{
modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod(), null/* TODO Change to default(_) if this is not a reference type */, ex);
}
}
Public Sub TOC_InsertFigures(ByRef objDocument As Word.Document)

Try
Call Tracer_Add2("SUBROUTINE", System.Reflection.MethodBase.GetCurrentMethod.Name & " start")
If My.Settings.ERROR_OCCURRED = True Then Exit Sub

gApplicationWord.Selection.TypeText(Text:="Figures")
gApplicationWord.Selection.Style = objDocument.Styles("B-TOC Section Heading No Break")
gApplicationWord.Selection.Collapse()

With objDocument.Styles("TOC 4")
.AutomaticallyUpdate = True
.BaseStyle = "B-TOC Figures"
.NextParagraphStyle = "Normal"
End With

With objDocument
.TablesOfContents.Add(Range:=gApplicationWord.Selection.Range, _
RightAlignPageNumbers:=True, _
UseHeadingStyles:=False, _
IncludePageNumbers:=True, _
AddedStyles:="B-Chart Title,2,B-Chart Title. Full Width,2,B-Table Title,2,B-Table Title. Full Width,2,B-Chart Holder Title,2", _
UseHyperlinks:=True, _
HidePageNumbersInWeb:=True, _
UseOutlineLevels:=False)

End With

Catch ex As System.Exception
Call modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod, Nothing, ex)
End Try
End Sub

InsertLevel

public void TOC_InsertLevel(ref Word.Document objDocument, int iLevel)
{
string sTOC = "";

try
{
Tracer_Add2("SUBROUTINE", System.Reflection.MethodBase.GetCurrentMethod().Name + " start");
if (My.Settings.ERROR_OCCURRED == true)
return;

if ((objDocument == null))
objDocument = Document_GetActive();

modWordObjectModel.TOC_StylesUpdate(objDocument);

if (objDocument.Bookmarks.Exists("BC_TOC_START") == true)
objDocument.Bookmarks("BC_TOC_START").Delete();
if (objDocument.Bookmarks.Exists("BC_TOC_END") == true)
objDocument.Bookmarks("BC_TOC_END").Delete();

switch (iLevel)
{
case 1:
{
sTOC = "B-Section Divider,1,B-Section Divider Landscape,1,B-SectionHeading no break,1,B-Section Heading,1,B-Heading 1. Full Width,1,B-Article Heading,1";
break;
}

case 2:
{
sTOC = "B-Section Divider,1,B-Section Divider Landscape,1,B-Heading 1,2,B-SectionHeading no break,1,B-Section Heading,1,B-Heading 1. Full Width,1,B-Article Heading,1";
break;
}

case 3:
{
sTOC = "B-Section Divider,1,B-Heading 3. Full Width,3,B-Heading 3,3,B-Heading 2. Full Width,3,B-Heading 2,3,B-Heading 1,2,B-SectionHeading no break,1,B-Section Heading,1,B-Heading 1. Full Width,1,B-Article Heading,1";
break;
}
}

// TOC Inner Pages

switch (true)
{
case object _ when (gApplicationWord.Selection.PageSetup.RightMargin < 200) & modWordObjectModel.Selection_IsAtBeginningOfSection() == true:
{
gApplicationWord.Selection.InsertBreak(Type: Word.WdBreakType.wdSectionBreakNextPage);
gApplicationWord.Selection.MoveUp(Word.WdUnits.wdLine, 1);
gApplicationWord.Selection.Bookmarks.Add(Range: gApplicationWord.Selection.Range, Name: "BC_TOC_START");
gApplicationWord.Selection.TypeText(Text: "Contents");
gApplicationWord.Selection.Style = objDocument.Styles("B-TOC Section Heading No Break");
gApplicationWord.Selection.Collapse();

{
var withBlock = objDocument.Styles("TOC 3");
withBlock.AutomaticallyUpdate = true;
withBlock.BaseStyle = "B-Toc 3";
withBlock.NextParagraphStyle = "Normal";
}
{
var withBlock = objDocument.Styles("TOC 2");
withBlock.AutomaticallyUpdate = true;
withBlock.BaseStyle = "B-TOC 2";
withBlock.NextParagraphStyle = "Normal";
}
{
var withBlock = objDocument.Styles("TOC 1");
withBlock.AutomaticallyUpdate = true;
withBlock.BaseStyle = "B-TOC 1";
withBlock.NextParagraphStyle = "Normal";
}

{
var withBlock = objDocument;
withBlock.TablesOfContents.Add(Range: gApplicationWord.Selection.Range, RightAlignPageNumbers: true, UseHeadingStyles: false, IncludePageNumbers: true, AddedStyles: (object)sTOC, UseHyperlinks: true, HidePageNumbersInWeb: true, UseOutlineLevels: false);
}

break;
}

case object _ when (gApplicationWord.Selection.PageSetup.RightMargin < 200) & modWordObjectModel.Selection_IsAtBeginningOfSection() == false:
{
{
var withBlock = gApplicationWord.Selection;
withBlock.MoveUp(Unit: Word.WdUnits.wdLine, Count: 1);
withBlock.MoveEnd(Word.WdUnits.wdParagraph);
withBlock.Collapse(Word.WdCollapseDirection.wdCollapseEnd);
withBlock.Range.Select();
withBlock.InsertBreak(Type: Word.WdBreakType.wdSectionBreakNextPage);
withBlock.MoveUp(Unit: Word.WdUnits.wdLine, Count: 1);
withBlock.MoveEnd(Word.WdUnits.wdParagraph);
withBlock.Collapse(Word.WdCollapseDirection.wdCollapseEnd);
withBlock.Range.Select();
withBlock.Bookmarks.Add(Range: gApplicationWord.Selection.Range, Name: "BC_TOC_START");
withBlock.MoveDown(Unit: Word.WdUnits.wdLine, Count: 1);
withBlock.Expand(Word.WdUnits.wdParagraph);
withBlock.Collapse(Word.WdCollapseDirection.wdCollapseStart);
withBlock.InsertBreak(Type: Word.WdBreakType.wdSectionBreakNextPage);
withBlock.MoveUp(Unit: Word.WdUnits.wdLine, Count: 1);
withBlock.TypeText(Text: "Contents");
withBlock.Style = objDocument.Styles("B-TOC Section Heading No Break");
withBlock.Collapse();
}

{
var withBlock = objDocument.Styles("TOC 3");
withBlock.AutomaticallyUpdate = true;
withBlock.BaseStyle = "B-Toc 3";
withBlock.NextParagraphStyle = "Normal";
}

{
var withBlock = objDocument.Styles("TOC 2");
withBlock.AutomaticallyUpdate = true;
withBlock.BaseStyle = "B-TOC 2";
withBlock.NextParagraphStyle = "Normal";
}

{
var withBlock = objDocument.Styles("TOC 1");
withBlock.AutomaticallyUpdate = true;
withBlock.BaseStyle = "B-TOC 1";
withBlock.NextParagraphStyle = "Normal";
}

// Application.DoEvents()
{
var withBlock = objDocument;
withBlock.TablesOfContents.Add(Range: gApplicationWord.Selection.Range, RightAlignPageNumbers: true, UseHeadingStyles: false, IncludePageNumbers: true, AddedStyles: (object)sTOC, UseHyperlinks: true, HidePageNumbersInWeb: true, UseOutlineLevels: false);

withBlock.TablesOfContents.Format = Word.WdTocFormat.wdTOCTemplate;
}

break;
}

case object _ when (gApplicationWord.Selection.PageSetup.RightMargin > 200):
{
gApplicationWord.Selection.Bookmarks.Add(Range: gApplicationWord.Selection.Range, Name: "BC_TOC_START");

{
var withBlock = objDocument.Styles("TOC 3");
withBlock.AutomaticallyUpdate = true;
withBlock.BaseStyle = "A-Toc 3";
withBlock.NextParagraphStyle = "Normal";
}
{
var withBlock = objDocument.Styles("TOC 2");
withBlock.AutomaticallyUpdate = true;
withBlock.BaseStyle = "A-TOC 2";
withBlock.NextParagraphStyle = "Normal";
}
{
var withBlock = objDocument.Styles("TOC 1");
withBlock.AutomaticallyUpdate = true;
withBlock.BaseStyle = "A-TOC 1";
withBlock.NextParagraphStyle = "Normal";
}

{
var withBlock = objDocument;
withBlock.TablesOfContents.Add(Range: gApplicationWord.Selection.Range, RightAlignPageNumbers: true, UseHeadingStyles: false, IncludePageNumbers: true, AddedStyles: (object)sTOC, UseHyperlinks: true, HidePageNumbersInWeb: true, UseOutlineLevels: false);
}

break;
}
}


modWordObjectModel.Fields_Update(objDocument);
}
catch (Exception ex)
{
modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod(), null/* TODO Change to default(_) if this is not a reference type */, ex, "There was a problem while creating the TOC", null/* Conversion error: Set to default value for this argument */, false);
}
}
Public Sub TOC_InsertLevel(ByRef objDocument As Word.Document, _
ByVal iLevel As Integer)

Dim sTOC As String = ""

Try
Call Tracer_Add2("SUBROUTINE", System.Reflection.MethodBase.GetCurrentMethod.Name & " start")
If My.Settings.ERROR_OCCURRED = True Then Exit Sub

If (objDocument Is Nothing) Then objDocument = Document_GetActive()

modWordObjectModel.TOC_StylesUpdate(objDocument)

If objDocument.Bookmarks.Exists("BC_TOC_START") = True Then
objDocument.Bookmarks("BC_TOC_START").Delete()
End If
If objDocument.Bookmarks.Exists("BC_TOC_END") = True Then
objDocument.Bookmarks("BC_TOC_END").Delete()
End If

Select Case iLevel
Case 1 : sTOC = "B-Section Divider,1,B-Section Divider Landscape,1,B-SectionHeading no break,1,B-Section Heading,1,B-Heading 1. Full Width,1,B-Article Heading,1"
Case 2 : sTOC = "B-Section Divider,1,B-Section Divider Landscape,1,B-Heading 1,2,B-SectionHeading no break,1,B-Section Heading,1,B-Heading 1. Full Width,1,B-Article Heading,1"
Case 3 : sTOC = "B-Section Divider,1,B-Heading 3. Full Width,3,B-Heading 3,3,B-Heading 2. Full Width,3,B-Heading 2,3,B-Heading 1,2,B-SectionHeading no break,1,B-Section Heading,1,B-Heading 1. Full Width,1,B-Article Heading,1"
End Select

'TOC Inner Pages

Select Case True
Case (gApplicationWord.Selection.PageSetup.RightMargin < 200) And _
modWordObjectModel.Selection_IsAtBeginningOfSection() = True

gApplicationWord.Selection.InsertBreak(Type:=Word.WdBreakType.wdSectionBreakNextPage)
gApplicationWord.Selection.MoveUp(Word.WdUnits.wdLine, 1)
gApplicationWord.Selection.Bookmarks.Add(Range:=gApplicationWord.Selection.Range, Name:="BC_TOC_START")
gApplicationWord.Selection.TypeText(Text:="Contents")
gApplicationWord.Selection.Style = objDocument.Styles("B-TOC Section Heading No Break")
gApplicationWord.Selection.Collapse()

With objDocument.Styles("TOC 3")
.AutomaticallyUpdate = True
.BaseStyle = "B-Toc 3"
.NextParagraphStyle = "Normal"
End With
With objDocument.Styles("TOC 2")
.AutomaticallyUpdate = True
.BaseStyle = "B-TOC 2"
.NextParagraphStyle = "Normal"
End With
With objDocument.Styles("TOC 1")
.AutomaticallyUpdate = True
.BaseStyle = "B-TOC 1"
.NextParagraphStyle = "Normal"
End With

With objDocument
.TablesOfContents.Add(Range:=gApplicationWord.Selection.Range, _
RightAlignPageNumbers:=True, _
UseHeadingStyles:=False, _
IncludePageNumbers:=True, _
AddedStyles:=CType(sTOC, Object), _
UseHyperlinks:=True, _
HidePageNumbersInWeb:=True, _
UseOutlineLevels:=False)
End With

'TOC Inner Pages
Case (gApplicationWord.Selection.PageSetup.RightMargin < 200) And _
modWordObjectModel.Selection_IsAtBeginningOfSection() = False

With gApplicationWord.Selection
.MoveUp(Unit:=Word.WdUnits.wdLine, Count:=1)
.MoveEnd(Word.WdUnits.wdParagraph)
.Collapse(Word.WdCollapseDirection.wdCollapseEnd)
.Range.Select()
.InsertBreak(Type:=Word.WdBreakType.wdSectionBreakNextPage)
.MoveUp(Unit:=Word.WdUnits.wdLine, Count:=1)
.MoveEnd(Word.WdUnits.wdParagraph)
.Collapse(Word.WdCollapseDirection.wdCollapseEnd)
.Range.Select()
.Bookmarks.Add(Range:=gApplicationWord.Selection.Range, Name:="BC_TOC_START")
.MoveDown(Unit:=Word.WdUnits.wdLine, Count:=1)
.Expand(Word.WdUnits.wdParagraph)
.Collapse(Word.WdCollapseDirection.wdCollapseStart)
.InsertBreak(Type:=Word.WdBreakType.wdSectionBreakNextPage)
.MoveUp(Unit:=Word.WdUnits.wdLine, Count:=1)
.TypeText(Text:="Contents")
.Style = objDocument.Styles("B-TOC Section Heading No Break")
.Collapse()
End With

With objDocument.Styles("TOC 3")
.AutomaticallyUpdate = True
.BaseStyle = "B-Toc 3"
.NextParagraphStyle = "Normal"
End With

With objDocument.Styles("TOC 2")
.AutomaticallyUpdate = True
.BaseStyle = "B-TOC 2"
.NextParagraphStyle = "Normal"
End With

With objDocument.Styles("TOC 1")
.AutomaticallyUpdate = True
.BaseStyle = "B-TOC 1"
.NextParagraphStyle = "Normal"
End With

'Application.DoEvents()
With objDocument
.TablesOfContents.Add(Range:=gApplicationWord.Selection.Range, _
RightAlignPageNumbers:=True, _
UseHeadingStyles:=False, _
IncludePageNumbers:=True, _
AddedStyles:=CType(sTOC, Object), _
UseHyperlinks:=True, _
HidePageNumbersInWeb:=True, _
UseOutlineLevels:=False)

.TablesOfContents.Format = Word.WdTocFormat.wdTOCTemplate
End With

'TOC Cover Page
Case (gApplicationWord.Selection.PageSetup.RightMargin > 200)

gApplicationWord.Selection.Bookmarks.Add(Range:=gApplicationWord.Selection.Range, Name:="BC_TOC_START")

With objDocument.Styles("TOC 3")
.AutomaticallyUpdate = True
.BaseStyle = "A-Toc 3"
.NextParagraphStyle = "Normal"
End With
With objDocument.Styles("TOC 2")
.AutomaticallyUpdate = True
.BaseStyle = "A-TOC 2"
.NextParagraphStyle = "Normal"
End With
With objDocument.Styles("TOC 1")
.AutomaticallyUpdate = True
.BaseStyle = "A-TOC 1"
.NextParagraphStyle = "Normal"
End With

With objDocument
.TablesOfContents.Add(Range:=gApplicationWord.Selection.Range, _
RightAlignPageNumbers:=True, _
UseHeadingStyles:=False, _
IncludePageNumbers:=True, _
AddedStyles:=CType(sTOC, Object), _
UseHyperlinks:=True, _
HidePageNumbersInWeb:=True, _
UseOutlineLevels:=False)
End With


End Select


Call modWordObjectModel.Fields_Update(objDocument)

Catch ex As System.Exception
Call modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod, Nothing, ex, _
"There was a problem while creating the TOC", , False)
End Try
End Sub

StylesExists

public bool TOC_StylesExists(ref Word.Document objDocument, bool bInformUser = false)
{
Word.Style oStyle;

try
{
Tracer_Add2("SUBROUTINE", System.Reflection.MethodBase.GetCurrentMethod().Name + " start");
if (My.Settings.ERROR_OCCURRED == true)
return;

oStyle = objDocument.Styles("A-TOC 1");
oStyle = objDocument.Styles("A-TOC 2");
oStyle = objDocument.Styles("A-Toc 3");

return true;
}
catch (Exception ex)
{
if ((bInformUser == true))
// there are styles missing from this document
modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod(), null/* TODO Change to default(_) if this is not a reference type */, ex, "This document does not contain the following three styles: A-TOC 1, A-TOC 2, A-Toc 3");
return false;
}
}
Public Function TOC_StylesExists(ByRef objDocument As Word.Document, _
Optional ByVal bInformUser As Boolean = False) As Boolean

Dim oStyle As Word.Style

Try
Call Tracer_Add2("SUBROUTINE", System.Reflection.MethodBase.GetCurrentMethod.Name & " start")
If My.Settings.ERROR_OCCURRED = True Then Exit Function

oStyle = objDocument.Styles("A-TOC 1")
oStyle = objDocument.Styles("A-TOC 2")
oStyle = objDocument.Styles("A-Toc 3")

Return True

Catch ex As System.Exception
If (bInformUser = True) Then
'there are styles missing from this document
Call modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod, Nothing, ex, _
"This document does not contain the following three styles: A-TOC 1, A-TOC 2, A-Toc 3")
End If
Return False
End Try
End Function

StylesUpdate

public void TOC_StylesUpdate(ref Word.Document objDocument)
{
Word.Style oStyle;
Word.Style oStyle2;
Word.Style oStyle3;

try
{
Tracer_Add2("SUBROUTINE", System.Reflection.MethodBase.GetCurrentMethod().Name + " start");
if (My.Settings.ERROR_OCCURRED == true)
return;

if (!(Style_ExistsInDocument(objDocument, "A-TOC 1") & Style_ExistsInDocument(objDocument, "A-TOC 2") & Style_ExistsInDocument(objDocument, "A-TOC 3")))
{
oStyle = objDocument.Styles.Add("A-TOC 1");

{
var withBlock = oStyle;
withBlock.BaseStyle = "B-TOC 1";
withBlock.ParagraphFormat.LeftIndent = 0;
withBlock.ParagraphFormat.TabStops.ClearAll();
withBlock.ParagraphFormat.TabStops.Add(gApplicationWord.CentimetersToPoints(12.06), Word.WdTabAlignment.wdAlignTabRight, Word.WdTabLeader.wdTabLeaderSpaces);
}

oStyle2 = objDocument.Styles.Add("A-TOC 2");

{
var withBlock = oStyle2;
withBlock.BaseStyle = "B-TOC 2";
withBlock.ParagraphFormat.LeftIndent = 0;
withBlock.ParagraphFormat.TabStops.ClearAll();
withBlock.ParagraphFormat.TabStops.Add(gApplicationWord.CentimetersToPoints(12.06), Word.WdTabAlignment.wdAlignTabRight, Word.WdTabLeader.wdTabLeaderDots);
}

oStyle3 = objDocument.Styles.Add("A-Toc 3");

{
var withBlock = oStyle3;
withBlock.BaseStyle = "B-Toc 3";
withBlock.ParagraphFormat.LeftIndent = gApplicationWord.CentimetersToPoints(0.43);
withBlock.ParagraphFormat.TabStops.ClearAll();
withBlock.ParagraphFormat.TabStops.Add(gApplicationWord.CentimetersToPoints(12.06), Word.WdTabAlignment.wdAlignTabRight, Word.WdTabLeader.wdTabLeaderDots);
}
}
}
catch (Exception ex)
{
modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod(), null/* TODO Change to default(_) if this is not a reference type */, ex);
}
}
Public Sub TOC_StylesUpdate(ByRef objDocument As Word.Document)

Dim oStyle As Word.Style
Dim oStyle2 As Word.Style
Dim oStyle3 As Word.Style

Try
Call Tracer_Add2("SUBROUTINE", System.Reflection.MethodBase.GetCurrentMethod.Name & " start")
If My.Settings.ERROR_OCCURRED = True Then Exit Sub

If Not (Style_ExistsInDocument(objDocument, "A-TOC 1") And _
Style_ExistsInDocument(objDocument, "A-TOC 2") And _
Style_ExistsInDocument(objDocument, "A-TOC 3")) Then

oStyle = objDocument.Styles.Add("A-TOC 1")

With oStyle
.BaseStyle = "B-TOC 1"
.ParagraphFormat.LeftIndent = 0
.ParagraphFormat.TabStops.ClearAll()
.ParagraphFormat.TabStops.Add(gApplicationWord.CentimetersToPoints(12.06), Word.WdTabAlignment.wdAlignTabRight, Word.WdTabLeader.wdTabLeaderSpaces)
End With

oStyle2 = objDocument.Styles.Add("A-TOC 2")

With oStyle2
.BaseStyle = "B-TOC 2"
.ParagraphFormat.LeftIndent = 0
.ParagraphFormat.TabStops.ClearAll()
.ParagraphFormat.TabStops.Add(gApplicationWord.CentimetersToPoints(12.06), Word.WdTabAlignment.wdAlignTabRight, Word.WdTabLeader.wdTabLeaderDots)
End With

oStyle3 = objDocument.Styles.Add("A-Toc 3")

With oStyle3
.BaseStyle = "B-Toc 3"
.ParagraphFormat.LeftIndent = gApplicationWord.CentimetersToPoints(0.43)
.ParagraphFormat.TabStops.ClearAll()
.ParagraphFormat.TabStops.Add(gApplicationWord.CentimetersToPoints(12.06), Word.WdTabAlignment.wdAlignTabRight, Word.WdTabLeader.wdTabLeaderDots)
End With

End If

Catch ex As System.Exception
Call modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod, Nothing, ex)
End Try
End Sub

TOC_AlreadyExists

public void TOC_AlreadyExists()
{
string smessage = "";

smessage = "A Table of Contents already exists in this document.";

System.Windows.Forms.MessageBox.Show(smessage, My.Settings.APP_WINFORMS_TITLE + " - TOC Already", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information);

smessage = smessage + " (" + System.Reflection.MethodBase.GetCurrentMethod().Name + ")";

Tracer_Add2("MESSAGE", smessage.Replace(System.Environment.NewLine, " ").Replace(" ", " "));
}
Public Sub TOC_AlreadyExists()

Dim smessage As String = ""

smessage = "A Table of Contents already exists in this document."

System.Windows.Forms.MessageBox.Show(smessage, _
My.Settings.APP_WINFORMS_TITLE & " - TOC Already", _
System.Windows.Forms.MessageBoxButtons.OK, _
System.Windows.Forms.MessageBoxIcon.Information)

smessage = smessage & " (" & System.Reflection.MethodBase.GetCurrentMethod.Name & ")"

Call Tracer_Add2("MESSAGE", smessage.Replace(System.Environment.NewLine, " ").Replace(" ", " "))

End Sub

TOC_BookmarkMissingDeleteManually

public void TOC_BookmarkMissingDeleteManually(string sBookmarkName)
{
string smessage = "";

smessage = "The bookmark '" + sBookmarkName + "' does not exist in this document." + System.Environment.NewLine + "You will have to remove the Table of Contents manually.";

System.Windows.Forms.MessageBox.Show(smessage, My.Settings.APP_WINFORMS_TITLE, System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information);

smessage = smessage + " (" + System.Reflection.MethodBase.GetCurrentMethod().Name + ")";

Tracer_Add2("MESSAGE", smessage.Replace(System.Environment.NewLine, " ").Replace(" ", " "));
}
Public Sub TOC_BookmarkMissingDeleteManually(ByVal sBookmarkName As String)

Dim smessage As String = ""

smessage = "The bookmark '" & sBookmarkName & "' does not exist in this document." & _
System.Environment.NewLine & _
"You will have to remove the Table of Contents manually."

System.Windows.Forms.MessageBox.Show(smessage, _
My.Settings.APP_WINFORMS_TITLE, _
System.Windows.Forms.MessageBoxButtons.OK, _
System.Windows.Forms.MessageBoxIcon.Information)

smessage = smessage & " (" & System.Reflection.MethodBase.GetCurrentMethod.Name & ")"

Call Tracer_Add2("MESSAGE", smessage.Replace(System.Environment.NewLine, " ").Replace(" ", " "))
End Sub

TOC_CannotChangeOrientation

public void TOC_CannotChangeOrientation()
{
string smessage = "";

smessage = "You cannot change the orientation of a TOC Page.";

System.Windows.Forms.MessageBox.Show(smessage, My.Settings.APP_WINFORMS_TITLE, System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information);

smessage = smessage + " (" + System.Reflection.MethodBase.GetCurrentMethod().Name + ")";

Tracer_Add2("MESSAGE", smessage.Replace(System.Environment.NewLine, " ").Replace(" ", " "));
}
Public Sub TOC_CannotChangeOrientation()

Dim smessage As String = ""

smessage = "You cannot change the orientation of a TOC Page."

System.Windows.Forms.MessageBox.Show(smessage, _
My.Settings.APP_WINFORMS_TITLE, _
System.Windows.Forms.MessageBoxButtons.OK, _
System.Windows.Forms.MessageBoxIcon.Information)

smessage = smessage & " (" & System.Reflection.MethodBase.GetCurrentMethod.Name & ")"

Call Tracer_Add2("MESSAGE", smessage.Replace(System.Environment.NewLine, " ").Replace(" ", " "))
End Sub

TOC_Deleted

public void TOC_Deleted()
{
string smessage = "";

smessage = "The Table of Contents has been removed from this document.";

System.Windows.Forms.MessageBox.Show(smessage, My.Settings.APP_WINFORMS_TITLE, System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information);

smessage = smessage + " (" + System.Reflection.MethodBase.GetCurrentMethod().Name + ")";

Tracer_Add2("MESSAGE", smessage.Replace(System.Environment.NewLine, " ").Replace(" ", " "));
}
Public Sub TOC_Deleted()

Dim smessage As String = ""

smessage = "The Table of Contents has been removed from this document."

System.Windows.Forms.MessageBox.Show(smessage, _
My.Settings.APP_WINFORMS_TITLE, _
System.Windows.Forms.MessageBoxButtons.OK, _
System.Windows.Forms.MessageBoxIcon.Information)

smessage = smessage & " (" & System.Reflection.MethodBase.GetCurrentMethod.Name & ")"

Call Tracer_Add2("MESSAGE", smessage.Replace(System.Environment.NewLine, " ").Replace(" ", " "))
End Sub

TOC_DoesNotExist

public void TOC_DoesNotExist()
{
string smessage = "";

smessage = "This document does not contain a Table of Contents.";

System.Windows.Forms.MessageBox.Show(smessage, My.Settings.APP_WINFORMS_TITLE + " - No TOC Found", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information);

smessage = smessage + " (" + System.Reflection.MethodBase.GetCurrentMethod().Name + ")";

Tracer_Add2("MESSAGE", smessage.Replace(System.Environment.NewLine, " ").Replace(" ", " "));
}
Public Sub TOC_DoesNotExist()

Dim smessage As String = ""

smessage = "This document does not contain a Table of Contents."

System.Windows.Forms.MessageBox.Show(smessage, _
My.Settings.APP_WINFORMS_TITLE & " - No TOC Found", _
System.Windows.Forms.MessageBoxButtons.OK, _
System.Windows.Forms.MessageBoxIcon.Information)

smessage = smessage & " (" & System.Reflection.MethodBase.GetCurrentMethod.Name & ")"

Call Tracer_Add2("MESSAGE", smessage.Replace(System.Environment.NewLine, " ").Replace(" ", " "))

End Sub

TOC_HasBeenUpdated

public void TOC_HasBeenUpdated()
{
string smessage = "";

smessage = "The Table of Contents in this document has been updated.";

System.Windows.Forms.MessageBox.Show(smessage, My.Settings.APP_WINFORMS_TITLE, System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information);

smessage = smessage + " (" + System.Reflection.MethodBase.GetCurrentMethod().Name + ")";

Tracer_Add2("MESSAGE", smessage.Replace(System.Environment.NewLine, " ").Replace(" ", " "));
}
Public Sub TOC_HasBeenUpdated()

Dim smessage As String = ""

smessage = "The Table of Contents in this document has been updated."

System.Windows.Forms.MessageBox.Show(smessage, _
My.Settings.APP_WINFORMS_TITLE, _
System.Windows.Forms.MessageBoxButtons.OK, _
System.Windows.Forms.MessageBoxIcon.Information)

smessage = smessage & " (" & System.Reflection.MethodBase.GetCurrentMethod.Name & ")"

Call Tracer_Add2("MESSAGE", smessage.Replace(System.Environment.NewLine, " ").Replace(" ", " "))

End Sub

UnlinkFields

public void Figures_UnlinkFields(ref Word.Document objDocument)
{
Word.Field oField;
int ifieldcount;

try
{
Tracer_Add2("SUBROUTINE", System.Reflection.MethodBase.GetCurrentMethod().Name + " start");
if (My.Settings.ERROR_OCCURRED == true)
return;

if ((objDocument.Fields.Count == 0))
return;

for (ifieldcount = objDocument.Fields.Count; ifieldcount >= 1; ifieldcount += -1)
{
oField = objDocument.Fields(ifieldcount);

if ((oField.Code.Text.IndexOf(@"SEQ Figure \* ARABIC") > -1) | (oField.Code.Text.IndexOf("REF _Ref") > -1))
objDocument.Fields(ifieldcount).Unlink();
}
}
catch (Exception ex)
{
modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod(), null/* TODO Change to default(_) if this is not a reference type */, ex);
}
}
Public Sub Figures_UnlinkFields(ByRef objDocument As Word.Document)

Dim oField As Word.Field
Dim ifieldcount As Integer

Try
Call Tracer_Add2("SUBROUTINE", System.Reflection.MethodBase.GetCurrentMethod.Name & " start")
If My.Settings.ERROR_OCCURRED = True Then Exit Sub

If (objDocument.Fields.Count = 0) Then
Exit Sub
End If

For ifieldcount = objDocument.Fields.Count To 1 Step -1
oField = objDocument.Fields(ifieldcount)

If (oField.Code.Text.IndexOf("SEQ Figure \* ARABIC") > -1) Or _
(oField.Code.Text.IndexOf("REF _Ref") > -1) Then

objDocument.Fields(ifieldcount).Unlink()
End If
Next ifieldcount

Catch ex As System.Exception
Call modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod, Nothing, ex)
End Try
End Sub

Update

public void TOC_Update(ref Word.Document objDocument, bool bInformUser = false)
{
Word.TableOfContents objTableOfContents;

try
{
Tracer_Add2("SUBROUTINE", System.Reflection.MethodBase.GetCurrentMethod().Name + " start");
if (My.Settings.ERROR_OCCURRED == true)
return;

if ((objDocument.TablesOfContents.Count > 0))
{
foreach (var objTableOfContents in objDocument.TablesOfContents)
objTableOfContents.Update();

if ((bInformUser == true))
modMessages.TOC_HasBeenUpdated();
}
else if ((bInformUser == true))
modMessages.TOC_DoesNotExist();
}
catch (Exception ex)
{
modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod(), null/* TODO Change to default(_) if this is not a reference type */, ex);
}
}
Public Sub TOC_Update(ByRef objDocument As Word.Document, _
Optional ByVal bInformUser As Boolean = False)

Dim objTableOfContents As Word.TableOfContents

Try
Call Tracer_Add2("SUBROUTINE", System.Reflection.MethodBase.GetCurrentMethod.Name & " start")
If My.Settings.ERROR_OCCURRED = True Then Exit Sub

If (objDocument.TablesOfContents.Count > 0) Then
For Each objTableOfContents In objDocument.TablesOfContents
objTableOfContents.Update()
Next objTableOfContents

If (bInformUser = True) Then
Call modMessages.TOC_HasBeenUpdated()
End If
Else
If (bInformUser = True) Then
Call modMessages.TOC_DoesNotExist()
End If
End If

Catch ex As System.Exception
Call modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod, Nothing, ex)
End Try
End Sub

UpdateFields

public void Figures_UpdateFields(ref Word.Document objDocument, bool bInformUser = false)
{
Word.Field oField;
int ifieldcount;

try
{
Tracer_Add2("SUBROUTINE", System.Reflection.MethodBase.GetCurrentMethod().Name + " start");
if (My.Settings.ERROR_OCCURRED == true)
return;

if ((objDocument.Fields.Count == 0))
return;

// update figure numbers & references
for (ifieldcount = 1; ifieldcount <= objDocument.Fields.Count; ifieldcount++)
{
if (modWordObjectModel.Field_CodeTextExists(objDocument, ifieldcount) == true)
{
oField = objDocument.Fields(ifieldcount);
if ((oField.Code.Text.IndexOf(@"SEQ Figure \* ARABIC") > -1) | (oField.Code.Text.IndexOf("REF _Ref") > -1))
objDocument.Fields(ifieldcount).Update();
}
}

if ((bInformUser == true))
modMessages.Figures_HaveBeenUpdated();
}
catch (Exception ex)
{
modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod(), null/* TODO Change to default(_) if this is not a reference type */, ex);
}
}
Public Sub Figures_UpdateFields(ByRef objDocument As Word.Document, _
Optional ByVal bInformUser As Boolean = False)

Dim oField As Word.Field
Dim ifieldcount As Integer

Try
Call Tracer_Add2("SUBROUTINE", System.Reflection.MethodBase.GetCurrentMethod.Name & " start")
If My.Settings.ERROR_OCCURRED = True Then Exit Sub

If (objDocument.Fields.Count = 0) Then
Exit Sub
End If

'update figure numbers & references
For ifieldcount = 1 To objDocument.Fields.Count

If modWordObjectModel.Field_CodeTextExists(objDocument, ifieldcount) = True Then

oField = objDocument.Fields(ifieldcount)
If (oField.Code.Text.IndexOf("SEQ Figure \* ARABIC") > -1) Or _
(oField.Code.Text.IndexOf("REF _Ref") > -1) Then

objDocument.Fields(ifieldcount).Update()
End If

End If
Next ifieldcount

If (bInformUser = True) Then
Call modMessages.Figures_HaveBeenUpdated()
End If

Catch ex As System.Exception
Call modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod, Nothing, ex)
End Try
End Sub

© 2026 Better Solutions Limited. All Rights Reserved. © 2026 Better Solutions Limited Top