C# Snippets
CanBeChanged
public bool Section_CanBeChanged(ref Word.Document objDocument, string sDocumentTemplate_Code, bool bInformUser = true)
{
bool blnIsValid;
Word.Section oSection;
try
{
Tracer_Add2("SUBROUTINE", System.Reflection.MethodBase.GetCurrentMethod().Name + " start");
if (My.Settings.ERROR_OCCURRED == true)
return;
blnIsValid = true;
oSection = gApplicationWord.Selection.Sections(1);
// Return True
if (((oSection.Index == 1) & (sDocumentTemplate_Code == clsIdentifyDocumentAs.TemplateCode_Report.ToString)))
{
if ((bInformUser == true))
modMessages.Pages_CannotChangeOrientationCoverPage();
return false;
}
if ((oSection.Index == 2 & objDocument.TablesOfContents.Count > 0))
{
if ((bInformUser == true))
modMessages.TOC_CannotChangeOrientation();
return false;
}
if (((oSection.Index == objDocument.Sections.Count) & (sDocumentTemplate_Code == clsIdentifyDocumentAs.TemplateCode_Report.ToString)))
{
if ((bInformUser == true))
modMessages.Sections_CannotChangeOrientationLastSection();
return false;
}
if ((oSection.Index == (objDocument.Sections.Count - 1) & oSection.Footers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary).Range.Paragraphs.Count > 3))
{
if ((bInformUser == true))
modMessages.Message_General_PageCannotChangeOrientationDisclosurePages();
return false;
}
if ((gApplicationWord.Selection.Sections.Count > 1))
{
if ((bInformUser == true))
modMessages.Sections_CannotChangeOrientationMultipleSections();
return false;
}
return blnIsValid;
}
catch (Exception ex)
{
modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod(), null/* TODO Change to default(_) if this is not a reference type */, ex);
}
}
Public Function Section_CanBeChanged(ByRef objDocument As Word.Document, _
ByVal sDocumentTemplate_Code As String, _
Optional ByVal bInformUser As Boolean = True) As Boolean
Dim blnIsValid As Boolean
Dim oSection As Word.Section
Try
Call Tracer_Add2("SUBROUTINE", System.Reflection.MethodBase.GetCurrentMethod.Name & " start")
If My.Settings.ERROR_OCCURRED = True Then Exit Function
blnIsValid = True
oSection = gApplicationWord.Selection.Sections(1)
'Return True
If ((oSection.Index = 1) And (sDocumentTemplate_Code = clsIdentifyDocumentAs.TemplateCode_Report.ToString)) Then
If (bInformUser = True) Then
Call modMessages.Pages_CannotChangeOrientationCoverPage()
End If
Return False
End If
If (oSection.Index = 2 And _
objDocument.TablesOfContents.Count > 0) Then
If (bInformUser = True) Then
Call modMessages.TOC_CannotChangeOrientation()
End If
Return False
End If
If ((oSection.Index = objDocument.Sections.Count) And _
(sDocumentTemplate_Code = clsIdentifyDocumentAs.TemplateCode_Report.ToString)) Then
If (bInformUser = True) Then
Call modMessages.Sections_CannotChangeOrientationLastSection()
End If
Return False
End If
If (oSection.Index = (objDocument.Sections.Count - 1) And _
oSection.Footers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary).Range.Paragraphs.Count > 3) Then
If (bInformUser = True) Then
Call modMessages.Message_General_PageCannotChangeOrientationDisclosurePages()
End If
Return False
End If
If (gApplicationWord.Selection.Sections.Count > 1) Then
If (bInformUser = True) Then
Call modMessages.Sections_CannotChangeOrientationMultipleSections()
End If
Return False
End If
Return blnIsValid
Catch ex As System.Exception
Call modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod, Nothing, ex)
End Try
End Function
ChangeToLandscape
public void App_ToolbarLandscapeSection()
{
try
{
clsError.ErrorFlagChange(false);
gbDEBUG = clsError.DebugFlag("Debug");
clsSection.OrientationSet("Landscape");
gApplication.ActiveDocument.PageSetup.LeftMargin = gApplication.MillimetersToPoints(20);
gApplication.ActiveDocument.PageSetup.RightMargin = gApplication.MillimetersToPoints(15);
int isectiono;
int ipageno;
Word.Shape objcolouredbarshape;
isectiono = System.Convert.ToInt32(gApplication.Selection.Information(Word.WdInformation.wdActiveEndSectionNumber));
ipageno = System.Convert.ToInt32(gApplication.Selection.Information(Word.WdInformation.wdActiveEndPageNumber));
gApplication.ActiveWindow.ActivePane.View.SeekView = Word.WdSeekView.wdSeekCurrentPageFooter;
gApplication.Selection.WholeStory();
objcolouredbarshape = gApplication.Selection.Range.ShapeRange(1);
{
var withBlock = objcolouredbarshape.TextFrame.TextRange.ParagraphFormat.TabStops;
withBlock.ClearAll();
withBlock.Add(Position: gApplication.MillimetersToPoints(287), Alignment: (System.Object)Word.WdTabAlignment.wdAlignTabRight, Leader: Word.WdTabLeader.wdTabLeaderSpaces);
}
gApplication.ActiveWindow.ActivePane.View.SeekView = Word.WdSeekView.wdSeekMainDocument;
}
catch (Runtime.InteropServices.COMException objCOMException)
{
gobjCOMException = objCOMException;
}
catch (Exception objException)
{
gobjException = objException;
}
finally
{
if (gbDEBUG == true | ((IsNothing(gobjCOMException) == false | IsNothing(gobjException) == false)))
clsError.Handle2("App_ToolbarLandscapeSection", msMODULENAME, "change the page orientation to landscape.", gobjCOMException, gobjException);
}
}
Public Sub App_ToolbarLandscapeSection()
Try
clsError.ErrorFlagChange(False)
gbDEBUG = clsError.DebugFlag("Debug")
Call clsSection.OrientationSet("Landscape")
gApplication.ActiveDocument.PageSetup.LeftMargin = gApplication.MillimetersToPoints(20)
gApplication.ActiveDocument.PageSetup.RightMargin = gApplication.MillimetersToPoints(15)
Dim isectiono As Integer
Dim ipageno As Integer
Dim objcolouredbarshape As Word.Shape
isectiono = CInt(gApplication.Selection.Information(Word.WdInformation.wdActiveEndSectionNumber))
ipageno = CInt(gApplication.Selection.Information(Word.WdInformation.wdActiveEndPageNumber))
gApplication.ActiveWindow.ActivePane.View.SeekView = Word.WdSeekView.wdSeekCurrentPageFooter
gApplication.Selection.WholeStory()
objcolouredbarshape = gApplication.Selection.Range.ShapeRange(1)
With objcolouredbarshape.TextFrame.TextRange.ParagraphFormat.TabStops
.ClearAll()
.Add(Position:=gApplication.MillimetersToPoints(287), _
Alignment:=CType(Word.WdTabAlignment.wdAlignTabRight, System.Object), _
Leader:=Word.WdTabLeader.wdTabLeaderSpaces)
End With
gApplication.ActiveWindow.ActivePane.View.SeekView = Word.WdSeekView.wdSeekMainDocument
Catch objCOMException As System.Runtime.InteropServices.COMException
gobjCOMException = objCOMException
Catch objException As Exception
gobjException = objException
Finally
If gbDEBUG = True Or _
((IsNothing(gobjCOMException) = False Or IsNothing(gobjException) = False)) Then
Call clsError.Handle2("App_ToolbarLandscapeSection", msMODULENAME, _
"change the page orientation to landscape.", _
gobjCOMException, gobjException)
End If
End Try
End Sub
ChangeToPortrait
public void App_ToolbarPortraitSection()
{
try
{
clsError.ErrorFlagChange(false);
gbDEBUG = clsError.DebugFlag("Debug");
clsSection.OrientationSet("Portrait");
gApplication.ActiveDocument.PageSetup.LeftMargin = gApplication.MillimetersToPoints(20);
gApplication.ActiveDocument.PageSetup.RightMargin = gApplication.MillimetersToPoints(15);
int isectiono;
int ipageno;
Word.Shape objcolouredbarshape;
isectiono = System.Convert.ToInt32(gApplication.Selection.Information(Word.WdInformation.wdActiveEndSectionNumber));
ipageno = System.Convert.ToInt32(gApplication.Selection.Information(Word.WdInformation.wdActiveEndPageNumber));
gApplication.ActiveWindow.ActivePane.View.SeekView = Word.WdSeekView.wdSeekCurrentPageFooter;
gApplication.Selection.WholeStory();
objcolouredbarshape = gApplication.Selection.Range.ShapeRange(1);
{
var withBlock = objcolouredbarshape.TextFrame.TextRange.ParagraphFormat.TabStops;
withBlock.ClearAll();
withBlock.Add(Position: gApplication.MillimetersToPoints(200), Alignment: (System.Object)Word.WdTabAlignment.wdAlignTabRight, Leader: Word.WdTabLeader.wdTabLeaderSpaces);
}
gApplication.ActiveWindow.ActivePane.View.SeekView = Word.WdSeekView.wdSeekMainDocument;
}
catch (Runtime.InteropServices.COMException objCOMException)
{
gobjCOMException = objCOMException;
}
catch (Exception objException)
{
gobjException = objException;
}
finally
{
if (gbDEBUG == true | ((IsNothing(gobjCOMException) == false | IsNothing(gobjException) == false)))
clsError.Handle2("App_ToolbarPortraitSection", msMODULENAME, "change the page orientation to Portrait.", gobjCOMException, gobjException);
}
}
Public Sub App_ToolbarPortraitSection()
Try
clsError.ErrorFlagChange(False)
gbDEBUG = clsError.DebugFlag("Debug")
Call clsSection.OrientationSet("Portrait")
gApplication.ActiveDocument.PageSetup.LeftMargin = gApplication.MillimetersToPoints(20)
gApplication.ActiveDocument.PageSetup.RightMargin = gApplication.MillimetersToPoints(15)
Dim isectiono As Integer
Dim ipageno As Integer
Dim objcolouredbarshape As Word.Shape
isectiono = CInt(gApplication.Selection.Information(Word.WdInformation.wdActiveEndSectionNumber))
ipageno = CInt(gApplication.Selection.Information(Word.WdInformation.wdActiveEndPageNumber))
gApplication.ActiveWindow.ActivePane.View.SeekView = Word.WdSeekView.wdSeekCurrentPageFooter
gApplication.Selection.WholeStory()
objcolouredbarshape = gApplication.Selection.Range.ShapeRange(1)
With objcolouredbarshape.TextFrame.TextRange.ParagraphFormat.TabStops
.ClearAll()
.Add(Position:=gApplication.MillimetersToPoints(200), _
Alignment:=CType(Word.WdTabAlignment.wdAlignTabRight, System.Object), _
Leader:=Word.WdTabLeader.wdTabLeaderSpaces)
End With
gApplication.ActiveWindow.ActivePane.View.SeekView = Word.WdSeekView.wdSeekMainDocument
Catch objCOMException As System.Runtime.InteropServices.COMException
gobjCOMException = objCOMException
Catch objException As Exception
gobjException = objException
Finally
If gbDEBUG = True Or _
((IsNothing(gobjCOMException) = False Or IsNothing(gobjException) = False)) Then
Call clsError.Handle2("App_ToolbarPortraitSection", msMODULENAME, _
"change the page orientation to Portrait.", _
gobjCOMException, gobjException)
End If
End Try
End Sub
Insert
public static void Insert()
{
try
{
if (clsError.ErrorFlag() == true)
return;
gApplicationWord.Selection.InsertBreak(Type: Word.WdBreakType.wdSectionBreakNextPage);
}
catch (Runtime.InteropServices.COMException objCOMException)
{
mobjCOMException = objCOMException;
}
catch (Exception objException)
{
mobjException = objException;
}
finally
{
if (gbDEBUG_WORD == true | ((!mobjCOMException == null) | (!mobjException == null)))
clsError.Handle("Insert", msCLASSNAME, "insert the new section break.", mobjCOMException, mobjException);
}
}
Public Shared Sub Insert()
Try
If clsError.ErrorFlag() = True Then Exit Sub
gApplicationWord.Selection.InsertBreak(Type:=Word.WdBreakType.wdSectionBreakNextPage)
Catch objCOMException As System.Runtime.InteropServices.COMException
mobjCOMException = objCOMException
Catch objException As Exception
mobjException = objException
Finally
If gbDEBUG_WORD = True Or _
((Not mobjCOMException Is Nothing) Or (Not mobjException Is Nothing)) Then
Call clsError.Handle("Insert", msCLASSNAME, _
"insert the new section break.", _
mobjCOMException, mobjException)
End If
End Try
End Sub
IsAtBeginningOfSection
public bool ParaIsAtBeginningOfSection(Word.Paragraph oPara)
{
int intPreviousSection;
int intCurrentSection;
try
{
Tracer_Add2("SUBROUTINE", System.Reflection.MethodBase.GetCurrentMethod().Name + " start");
if (My.Settings.ERROR_OCCURRED == true)
return;
intCurrentSection = oPara.Range.Sections(1).Index;
if (intCurrentSection == 1)
return false;
else
{
intPreviousSection = oPara.Previous(1).Range.Sections.Last.Index;
if (intPreviousSection < intCurrentSection)
return true;
else
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 ParaIsAtBeginningOfSection(ByVal oPara As Word.Paragraph) As Boolean
Dim intPreviousSection As Integer
Dim intCurrentSection As Integer
Try
Call Tracer_Add2("SUBROUTINE", System.Reflection.MethodBase.GetCurrentMethod.Name & " start")
If My.Settings.ERROR_OCCURRED = True Then Exit Function
intCurrentSection = oPara.Range.Sections(1).Index
If intCurrentSection = 1 Then
Return False
Else
intPreviousSection = oPara.Previous(1).Range.Sections.Last.Index
If intPreviousSection < intCurrentSection Then
Return True
Else
Return False
End If
End If
Catch ex As System.Exception
Call modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod, Nothing, ex)
End Try
End Function
OrientationGet
public static string OrientationGet()
{
try
{
if (clsError.ErrorFlag() == true)
return;
if (gApplicationWord.Selection.PageSetup.Orientation == Word.WdOrientation.wdOrientPortrait)
OrientationGet = "Portrait";
if (gApplicationWord.Selection.PageSetup.Orientation == Word.WdOrientation.wdOrientLandscape)
OrientationGet = "Landscape";
}
catch (Runtime.InteropServices.COMException objCOMException)
{
mobjCOMException = objCOMException;
}
catch (Exception objException)
{
mobjException = objException;
}
finally
{
if (gbDEBUG_WORD == true | ((!mobjCOMException == null) | (!mobjException == null)))
clsError.Handle("OrientationChange", msCLASSNAME, "return the page orientation for the current section.", mobjCOMException, mobjException);
}
}
Public Shared Function OrientationGet() As String
Try
If clsError.ErrorFlag() = True Then Exit Function
If gApplicationWord.Selection.PageSetup.Orientation = _
Word.WdOrientation.wdOrientPortrait Then
OrientationGet = "Portrait"
End If
If gApplicationWord.Selection.PageSetup.Orientation = _
Word.WdOrientation.wdOrientLandscape Then
OrientationGet = "Landscape"
End If
Catch objCOMException As System.Runtime.InteropServices.COMException
mobjCOMException = objCOMException
Catch objException As Exception
mobjException = objException
Finally
If gbDEBUG_WORD = True Or _
((Not mobjCOMException Is Nothing) Or (Not mobjException Is Nothing)) Then
Call clsError.Handle("OrientationChange", msCLASSNAME, _
"return the page orientation for the current section.", _
mobjCOMException, mobjException)
End If
End Try
End Function
OrientationSet
public static void OrientationSet(string sOrientationType)
{
try
{
if (clsError.ErrorFlag() == true)
return;
if (sOrientationType == "Portrait")
gApplicationWord.Selection.PageSetup.Orientation = Word.WdOrientation.wdOrientPortrait;
if (sOrientationType == "Landscape")
gApplicationWord.Selection.PageSetup.Orientation = Word.WdOrientation.wdOrientLandscape;
}
catch (Runtime.InteropServices.COMException objCOMException)
{
mobjCOMException = objCOMException;
}
catch (Exception objException)
{
mobjException = objException;
}
finally
{
if (gbDEBUG_WORD == true | ((!mobjCOMException == null) | (!mobjException == null)))
clsError.Handle("OrientationSet", msCLASSNAME, "change the page orientation of the current section.", mobjCOMException, mobjException);
}
}
Public Shared Sub OrientationSet(ByVal sOrientationType As String)
Try
If clsError.ErrorFlag() = True Then Exit Sub
If sOrientationType = "Portrait" Then
gApplicationWord.Selection.PageSetup.Orientation = _
Word.WdOrientation.wdOrientPortrait
End If
If sOrientationType = "Landscape" Then
gApplicationWord.Selection.PageSetup.Orientation = _
Word.WdOrientation.wdOrientLandscape
End If
Catch objCOMException As System.Runtime.InteropServices.COMException
mobjCOMException = objCOMException
Catch objException As Exception
mobjException = objException
Finally
If gbDEBUG_WORD = True Or _
((Not mobjCOMException Is Nothing) Or (Not mobjException Is Nothing)) Then
Call clsError.Handle("OrientationSet",msCLASSNAME, _
"change the page orientation of the current section.", _
mobjCOMException, mobjException)
End If
End Try
End Sub
Section_DoesNotExist
public static void Section_DoesNotExist(int iTotalSections)
{
if (clsError.ErrorFlag() == true)
return;
System.Windows.Forms.MessageBox.Show("This document does not contain iTotalSections sections." + gsCRLF + "The active document only contains " + gApplicationWord.ActiveDocument.Sections.Count + " sections.", gsSOLUTION_NAME_WORD, Windows.Forms.MessageBoxButtons.OK, Windows.Forms.MessageBoxIcon.Information);
clsError.ErrorFlagChange(true);
}
Public Shared Sub Section_DoesNotExist(ByVal iTotalSections As Integer)
If clsError.ErrorFlag() = True Then Exit Sub
Call System.Windows.Forms.MessageBox.Show( _
"This document does not contain iTotalSections sections." & _
gsCRLF & _
"The active document only contains " & gApplicationWord.ActiveDocument.Sections.Count & " sections.", _
gsSOLUTION_NAME_WORD, _
Windows.Forms.MessageBoxButtons.OK, _
Windows.Forms.MessageBoxIcon.Information)
clsError.ErrorFlagChange(True)
End Sub
Section_IsProtectedCantGetPageWidth
public void Section_IsProtectedCantGetPageWidth()
{
string smessage = "";
smessage = "Unable to return the page width of this section." + System.Environment.NewLine + System.Environment.NewLine + "This section of the document is protected.";
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 Section_IsProtectedCantGetPageWidth()
Dim smessage As String = ""
smessage = "Unable to return the page width of this section." & _
System.Environment.NewLine & _
System.Environment.NewLine & _
"This section of the document is protected."
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
© 2026 Better Solutions Limited. All Rights Reserved. © 2026 Better Solutions Limited Top