C# Snippets


ApplySimpleRowFormatting

public bool Table_ApplySimpleRowFormatting(Word.Table objTable, int iNoOfHeadingRows, string sRowType, bool bUseAlternateShading)
{
const string sPROCNAME = "Table_ApplySimpleRowFormatting";

Word.Row objRow;
Word.Cell objCell;

bool blnIsAltRow;
int intStartRow;
int intLastRow;
int intCurrentRowIndex;
string strTargetStyle;
;/* Cannot convert OnErrorGoToStatementSyntax, CONVERSION ERROR: Conversion for OnErrorGoToLabelStatement not implemented, please report this issue in 'On Error GoTo ErrorHandler' at character 638
at ICSharpCode.CodeConverter.CSharp.VisualBasicConverter.MethodBodyVisitor.DefaultVisit(SyntaxNode node) in /home/runner/work/CodeConverter/CodeConverter/.temp/codeconverter/ICSharpCode.CodeConverter/CSharp/MethodBodyVisitor.cs:line 41
at Microsoft.CodeAnalysis.VisualBasic.VisualBasicSyntaxVisitor`1.Visit(SyntaxNode node)
at ICSharpCode.CodeConverter.CSharp.CommentConvertingMethodBodyVisitor.DefaultVisit(SyntaxNode node) in /home/runner/work/CodeConverter/CodeConverter/.temp/codeconverter/ICSharpCode.CodeConverter/CSharp/CommentConvertingMethodBodyVisitor.cs:line 27

Input:

On Error GoTo ErrorHandler

*/
strTargetStyle = "";

if ((sRowType == "Standard Row"))
{
strTargetStyle = "B-Table Text";
intStartRow = iNoOfHeadingRows + 1;
intLastRow = objTable.Rows.Count;
}
if ((sRowType == "Heading Row"))
{
strTargetStyle = "B-Table Heading";
intStartRow = 1;
intLastRow = iNoOfHeadingRows;
}

intCurrentRowIndex = intStartRow;
blnIsAltRow = true;

if (Table_HasVerticallyMergedCells(objTable) == false)
{
foreach (var objRow in objTable.Range.Rows)
{
if ((objRow.Index >= intStartRow) & (objRow.Index <= intLastRow))
{
if (intCurrentRowIndex < objRow.Index)
{
intCurrentRowIndex = intCurrentRowIndex + 1;

if ((sRowType == "Standard Row"))
blnIsAltRow = !blnIsAltRow;
}

{
var withBlock = objRow.Cells;
withBlock.Shading.Texture = Word.WdTextureIndex.wdTextureNone;
withBlock.Shading.ForegroundPatternColor = Word.WdColor.wdColorAutomatic;
withBlock.Shading.BackgroundPatternColor = Word.WdColor.wdColorAutomatic;
}

objRow.Range.Style = strTargetStyle;

if ((sRowType == "Heading Row"))
{
{
var withBlock = objRow.Cells.Shading;
withBlock.ForegroundPatternColor = Word.WdColor.wdColorAutomatic;
withBlock.BackgroundPatternColor = Information.RGB(131, 163, 175);
}
{
var withBlock = objRow.Cells.Borders(Word.WdBorderType.wdBorderBottom);
withBlock.LineStyle = Word.WdLineStyle.wdLineStyleSingle;
withBlock.Color = Word.WdColor.wdColorWhite;
withBlock.LineWidth = Word.WdLineWidth.wdLineWidth150pt;
}
}

if ((sRowType == "Standard Row") & (bUseAlternateShading == true))
{
if (blnIsAltRow)
{
objRow.Cells.Shading.ForegroundPatternColor = Word.WdColor.wdColorAutomatic;
objRow.Cells.Shading.BackgroundPatternColor = Information.RGB(213, 227, 235);
}
}

// Add the bottom border
if ((objRow.Index == objTable.Range.Rows.Count))
{
{
var withBlock = objRow.Cells.Borders(Word.WdBorderType.wdBorderBottom);
withBlock.LineStyle = Word.WdLineStyle.wdLineStyleSingle;
withBlock.LineWidth = Word.WdLineWidth.wdLineWidth075pt;
withBlock.Color = Information.RGB(131, 163, 175);
}
}
}
}
}
else
foreach (var objCell in objTable.Range.Cells)
{
if ((objCell.RowIndex >= intStartRow) & (objCell.RowIndex <= intLastRow))
{
if (intCurrentRowIndex < objCell.RowIndex)
{
intCurrentRowIndex = intCurrentRowIndex + 1;

if ((sRowType == "Standard Row"))
blnIsAltRow = !blnIsAltRow;
}

{
var withBlock = objCell;
withBlock.Shading.Texture = Word.WdTextureIndex.wdTextureNone;
withBlock.Shading.ForegroundPatternColor = Word.WdColor.wdColorAutomatic;
withBlock.Shading.BackgroundPatternColor = Word.WdColor.wdColorAutomatic;
}

objCell.Range.Style = strTargetStyle;

if ((sRowType == "Heading Row"))
{
{
var withBlock = objCell.Shading;
withBlock.ForegroundPatternColor = Word.WdColor.wdColorAutomatic;
withBlock.BackgroundPatternColor = (Word.WdColor)Microsoft.VisualBasic.RGB(131, 163, 175);
}
{
var withBlock = objCell.Borders(Word.WdBorderType.wdBorderBottom);
withBlock.LineStyle = Word.WdLineStyle.wdLineStyleSingle;
withBlock.Color = Word.WdColor.wdColorWhite;
withBlock.LineWidth = Word.WdLineWidth.wdLineWidth150pt;
}
}

if ((sRowType == "Standard Row") & (bUseAlternateShading == true))
{
if (blnIsAltRow)
{
objCell.Shading.ForegroundPatternColor = Word.WdColor.wdColorAutomatic;
objCell.Shading.BackgroundPatternColor = (Word.WdColor)Microsoft.VisualBasic.RGB(213, 227, 235);
}
}

// Add the bottom border
if ((objCell.RowIndex == objTable.Range.Rows.Count))
{
{
var withBlock = objCell.Borders(Word.WdBorderType.wdBorderBottom);
withBlock.LineStyle = Word.WdLineStyle.wdLineStyleSingle;
withBlock.LineWidth = Word.WdLineWidth.wdLineWidth075pt;
withBlock.Color = (Word.WdColor)Microsoft.VisualBasic.RGB(131, 163, 175);
}
}
}
}

{
var withBlock = objTable.Range.Cells(1);
withBlock.LeftPadding = gApplicationWord.InchesToPoints(0.01);
withBlock.RightPadding = gApplicationWord.InchesToPoints(0.01);
withBlock.TopPadding = 0;
withBlock.BottomPadding = 0;
}

return;
ErrorHandler:
;
Error_Handle(msMODULENAME, sPROCNAME, Information.Err.Number, Information.Err.Description);
}
Public Function Table_ApplySimpleRowFormatting(ByVal objTable As Word.Table, _
ByVal iNoOfHeadingRows As Integer, _
ByVal sRowType As String, _
ByVal bUseAlternateShading As Boolean) As Boolean

Const sPROCNAME As String = "Table_ApplySimpleRowFormatting"

Dim objRow As Word.Row
Dim objCell As Word.Cell

Dim blnIsAltRow As Boolean
Dim intStartRow As Integer
Dim intLastRow As Integer
Dim intCurrentRowIndex As Integer
Dim strTargetStyle As String

On Error GoTo ErrorHandler

strTargetStyle = ""

If (sRowType = "Standard Row") Then
strTargetStyle = "B-Table Text"
intStartRow = iNoOfHeadingRows + 1
intLastRow = objTable.Rows.Count
End If
If (sRowType = "Heading Row") Then
strTargetStyle = "B-Table Heading"
intStartRow = 1
intLastRow = iNoOfHeadingRows
End If

intCurrentRowIndex = intStartRow
blnIsAltRow = True

If Table_HasVerticallyMergedCells(objTable) = False Then

For Each objRow In objTable.Range.Rows

If (objRow.Index >= intStartRow) And (objRow.Index <= intLastRow) Then

If intCurrentRowIndex < objRow.Index Then
intCurrentRowIndex = intCurrentRowIndex + 1

If (sRowType = "Standard Row") Then
blnIsAltRow = Not blnIsAltRow
End If
End If

With objRow.Cells
.Shading.Texture = Word.WdTextureIndex.wdTextureNone
.Shading.ForegroundPatternColor = Word.WdColor.wdColorAutomatic
.Shading.BackgroundPatternColor = Word.WdColor.wdColorAutomatic
End With

objRow.Range.Style = strTargetStyle

If (sRowType = "Heading Row") Then
With objRow.Cells.Shading
.ForegroundPatternColor = Word.WdColor.wdColorAutomatic
.BackgroundPatternColor = RGB(131, 163, 175)
End With
With objRow.Cells.Borders(Word.WdBorderType.wdBorderBottom)
.LineStyle = Word.WdLineStyle.wdLineStyleSingle
.Color = Word.WdColor.wdColorWhite
.LineWidth = Word.WdLineWidth.wdLineWidth150pt
End With
End If

If (sRowType = "Standard Row") And (bUseAlternateShading = True) Then
If blnIsAltRow Then
objRow.Cells.Shading.ForegroundPatternColor = Word.WdColor.wdColorAutomatic
objRow.Cells.Shading.BackgroundPatternColor = RGB(213, 227, 235)
End If
End If

'Add the bottom border
If (objRow.Index = objTable.Range.Rows.Count) Then

With objRow.Cells.Borders(Word.WdBorderType.wdBorderBottom)
.LineStyle = Word.WdLineStyle.wdLineStyleSingle
.LineWidth = Word.WdLineWidth.wdLineWidth075pt
.Color = RGB(131, 163, 175)
End With
End If
End If
Next objRow

Else

For Each objCell In objTable.Range.Cells

If (objCell.RowIndex >= intStartRow) And (objCell.RowIndex <= intLastRow) Then
If intCurrentRowIndex < objCell.RowIndex Then
intCurrentRowIndex = intCurrentRowIndex + 1

If (sRowType = "Standard Row") Then
blnIsAltRow = Not blnIsAltRow
End If
End If

With objCell
.Shading.Texture = Word.WdTextureIndex.wdTextureNone
.Shading.ForegroundPatternColor = Word.WdColor.wdColorAutomatic
.Shading.BackgroundPatternColor = Word.WdColor.wdColorAutomatic
End With

objCell.Range.Style = strTargetStyle

If (sRowType = "Heading Row") Then
With objCell.Shading
.ForegroundPatternColor = Word.WdColor.wdColorAutomatic
.BackgroundPatternColor = CType(Microsoft.VisualBasic.RGB(131, 163, 175), Word.WdColor)
End With
With objCell.Borders(Word.WdBorderType.wdBorderBottom)
.LineStyle = Word.WdLineStyle.wdLineStyleSingle
.Color = Word.WdColor.wdColorWhite
.LineWidth = Word.WdLineWidth.wdLineWidth150pt
End With
End If

If (sRowType = "Standard Row") And (bUseAlternateShading = True) Then
If blnIsAltRow Then
objCell.Shading.ForegroundPatternColor = Word.WdColor.wdColorAutomatic
objCell.Shading.BackgroundPatternColor = CType(Microsoft.VisualBasic.RGB(213, 227, 235), Word.WdColor)
End If
End If

'Add the bottom border
If (objCell.RowIndex = objTable.Range.Rows.Count) Then
With objCell.Borders(Word.WdBorderType.wdBorderBottom)
.LineStyle = Word.WdLineStyle.wdLineStyleSingle
.LineWidth = Word.WdLineWidth.wdLineWidth075pt
.Color = CType(Microsoft.VisualBasic.RGB(131, 163, 175), Word.WdColor)
End With
End If
End If
Next objCell
End If

With objTable.Range.Cells(1)
.LeftPadding = gApplicationWord.InchesToPoints(0.01)
.RightPadding = gApplicationWord.InchesToPoints(0.01)
.TopPadding = 0
.BottomPadding = 0
End With

Exit Function
ErrorHandler:
Call Error_Handle(msMODULENAME, sPROCNAME, Err.Number, Err.Description)
End Function

Messages_NotInOne

public void Table_NotInOne()
{
System.Windows.Forms.MessageBox.Show("The selection is not within a table.", My.Settings.APP_WINFORMS_TITLE, System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information);
}
Public Sub Table_NotInOne()
System.Windows.Forms.MessageBox.Show( _
"The selection is not within a table.", _
My.Settings.APP_WINFORMS_TITLE, _
System.Windows.Forms.MessageBoxButtons.OK, _
System.Windows.Forms.MessageBoxIcon.Information)
End Sub

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