C# Snippets


Footer_UpdateFirstPage

public void Footer_UpdateFirstPage(ref Word.Document objDocument, string sDocumentType)
{
Word.Range objRange;
Word.Style objStyle;
int iPageNumber;
string sDisclaimer;
string strOriginalStyle;

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

iPageNumber = modWordObjectModel.Document_LastPageWithContent(objDocument);

objRange = objDocument.Sections(1).Footers(Word.WdHeaderFooterIndex.wdHeaderFooterFirstPage).Range;
objStyle = (Word.Style)objRange.Style;
strOriginalStyle = objStyle.NameLocal.ToString;

if (strOriginalStyle == "Footer")
{
objRange = objDocument.Sections(1).Footers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary).Range;
objStyle = (Word.Style)objRange.Style;
strOriginalStyle = objStyle.NameLocal.ToString;
}

objRange.Delete();
objRange.MoveEndWhile(Cset: System.Convert.ToChar(7), Count: Word.WdConstants.wdBackward);

if ((iPageNumber > 0))
objRange.Text = sDisclaimer.Replace("[xx]", System.Convert.ToHexString(iPageNumber));

if (strOriginalStyle.IndexOf("FirstPageFooter") > -1 | strOriginalStyle.ToUpper().IndexOf("A") > -1)
objRange.Style = strOriginalStyle;
else
{
if ((sDocumentType == clsIdentifyDocumentAs.DocumentType_BarCap.ToString))
{
{
var withBlock = objRange.Font;
withBlock.Name = "Expert Sans Extra Bold";
withBlock.Size = 7.5;
withBlock.Spacing = -0.1;
}
}

if ((sDocumentType == clsIdentifyDocumentAs.DocumentType_Legacy.ToString))
{
{
var withBlock = objRange.Font;
withBlock.Name = "Arial";
withBlock.Size = 9;
}
{
var withBlock = objRange.Paragraphs(1);
withBlock.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
withBlock.Borders(Word.WdBorderType.wdBorderTop).LineStyle = Word.WdLineStyle.wdLineStyleSingle;
withBlock.Borders(Word.WdBorderType.wdBorderTop).LineWidth = Word.WdLineWidth.wdLineWidth050pt;
withBlock.Borders(Word.WdBorderType.wdBorderTop).Color = Word.WdColor.wdColorBlack;
}
}
}
}
catch (Exception ex)
{
modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod(), null/* TODO Change to default(_) if this is not a reference type */, ex);
}
finally
{
objDocviewUploadService2 = null;
}
}
Public Sub Footer_UpdateFirstPage(ByRef objDocument As Word.Document, _
ByVal sDocumentType As String)

Dim objRange As Word.Range
Dim objStyle As Word.Style
Dim iPageNumber As Integer
Dim sDisclaimer As String
Dim strOriginalStyle As String

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

iPageNumber = modWordObjectModel.Document_LastPageWithContent(objDocument)

objRange = objDocument.Sections(1).Footers(Word.WdHeaderFooterIndex.wdHeaderFooterFirstPage).Range
objStyle = CType(objRange.Style, Word.Style)
strOriginalStyle = objStyle.NameLocal.ToString

If strOriginalStyle = "Footer" Then
objRange = objDocument.Sections(1).Footers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary).Range
objStyle = CType(objRange.Style, Word.Style)
strOriginalStyle = objStyle.NameLocal.ToString
End If

objRange.Delete()
objRange.MoveEndWhile(Cset:=System.Convert.ToChar(7), Count:=Word.WdConstants.wdBackward)

If (iPageNumber > 0) Then
objRange.Text = sDisclaimer.Replace("[xx]", CType(iPageNumber, String))
End If

If strOriginalStyle.IndexOf("FirstPageFooter") > -1 Or _
strOriginalStyle.ToUpper.IndexOf("A") > -1 Then

objRange.Style = strOriginalStyle
Else

If (sDocumentType = clsIdentifyDocumentAs.DocumentType_BarCap.ToString) Then
With objRange.Font
.Name = "Expert Sans Extra Bold"
.Size = 7.5
.Spacing = -0.1
End With
End If

If (sDocumentType = clsIdentifyDocumentAs.DocumentType_Legacy.ToString) Then
With objRange.Font
.Name = "Arial"
.Size = 9
End With
With objRange.Paragraphs(1)
.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter
.Borders(Word.WdBorderType.wdBorderTop).LineStyle = Word.WdLineStyle.wdLineStyleSingle
.Borders(Word.WdBorderType.wdBorderTop).LineWidth = Word.WdLineWidth.wdLineWidth050pt
.Borders(Word.WdBorderType.wdBorderTop).Color = Word.WdColor.wdColorBlack
End With
End If
End If

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

Message_HeadersFooters_ExitFirst

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

smessage = "You must come out of the header/footer before you can do this.";

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 HeadersFooters_ExitFirst()

Dim smessage As String = ""

smessage = "You must come out of the header/footer before you can do this."

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

Page_HeaderInsert

public void Header_Insert(ref Word.Document objDocument, string sHeaderText)
{
int isectioncount;
Word.Section objSection;

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

for (isectioncount = 1; isectioncount <= objDocument.Sections.Count - 1; isectioncount++)
{
objSection = (Word.Section)objDocument.Sections(isectioncount);
objSection.Headers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary).Range.Tables(1).Cell(1, 3).Range.Text = sHeaderText;
}
}
catch (Exception ex)
{
modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod(), null/* TODO Change to default(_) if this is not a reference type */, ex);
}
}
Public Sub Header_Insert(ByRef objDocument As Word.Document, _
ByVal sHeaderText As String)

Dim isectioncount As Integer
Dim objSection As Word.Section

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

For isectioncount = 1 To objDocument.Sections.Count - 1

objSection = CType(objDocument.Sections(isectioncount), Word.Section)
objSection.Headers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary).Range.Tables(1).Cell(1, 3).Range.Text = sHeaderText

Next isectioncount

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