C# Snippets


Selection_CharactersExtend

public static void CharactersExtend(string sDirection, int iNoOfChars)
{
try
{
if (clsError.ErrorFlag() == true)
return;

if (sDirection == "Left")
gApplicationWord.Selection.MoveLeft(Unit: Word.WdUnits.wdCharacter, Count: (System.Object)iNoOfChars, Extend: Word.WdMovementType.wdExtend);

if (sDirection == "Right")
gApplicationWord.Selection.MoveRight(Unit: Word.WdUnits.wdCharacter, Count: (System.Object)iNoOfChars, Extend: Word.WdMovementType.wdExtend);

if (sDirection == "Up")
gApplicationWord.Selection.MoveUp(Unit: Word.WdUnits.wdCharacter, Count: (System.Object)iNoOfChars, Extend: Word.WdMovementType.wdExtend);

if (sDirection == "Down")
gApplicationWord.Selection.MoveDown(Unit: Word.WdUnits.wdCharacter, Count: (System.Object)iNoOfChars, Extend: Word.WdMovementType.wdExtend);
}
catch (Runtime.InteropServices.COMException objCOMException)
{
mobjCOMException = objCOMException;
}
catch (Exception objException)
{
mobjException = objException;
}

finally
{
if (gbDEBUG_WORD == true | ((!mobjCOMException == null) | (!mobjException == null)))
clsError.Handle("CharactersExtend", msCLASSNAME, "extend the current selection " + iNoOfChars + " characters to the '" + sDirection + "'.", mobjCOMException, mobjException);
}
}
Public Shared Sub CharactersExtend(ByVal sDirection As String, _
ByVal iNoOfChars As Integer)

Try
If clsError.ErrorFlag() = True Then Exit Sub

If sDirection = "Left" Then
gApplicationWord.Selection.MoveLeft(Unit:=Word.WdUnits.wdCharacter, _
Count:=CType(iNoOfChars, System.Object), _
Extend:=Word.WdMovementType.wdExtend)
End If

If sDirection = "Right" Then
gApplicationWord.Selection.MoveRight(Unit:=Word.WdUnits.wdCharacter, _
Count:=CType(iNoOfChars, System.Object), _
Extend:=Word.WdMovementType.wdExtend)
End If

If sDirection = "Up" Then
gApplicationWord.Selection.MoveUp(Unit:=Word.WdUnits.wdCharacter, _
Count:=CType(iNoOfChars, System.Object), _
Extend:=Word.WdMovementType.wdExtend)
End If

If sDirection = "Down" Then
gApplicationWord.Selection.MoveDown(Unit:=Word.WdUnits.wdCharacter, _
Count:=CType(iNoOfChars, System.Object), _
Extend:=Word.WdMovementType.wdExtend)
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("CharactersExtend", msCLASSNAME, _
"extend the current selection " & iNoOfChars & _
" characters to the '" & sDirection & "'.", _
mobjCOMException, mobjException)
End If
End Try
End Sub

Selection_CharactersMove

public static void CharactersMove(string sDirection, int iNoOfChars)
{
try
{
if (clsError.ErrorFlag() == true)
return;

if (sDirection == "Left")
gApplicationWord.Selection.MoveLeft(Unit: Word.WdUnits.wdCharacter, Count: (System.Object)iNoOfChars, Extend: Word.WdMovementType.wdMove);

if (sDirection == "Right")
gApplicationWord.Selection.MoveRight(Unit: Word.WdUnits.wdCharacter, Count: (System.Object)iNoOfChars, Extend: Word.WdMovementType.wdMove);

if (sDirection == "Up")
gApplicationWord.Selection.MoveUp(Unit: Word.WdUnits.wdCharacter, Count: (System.Object)iNoOfChars, Extend: Word.WdMovementType.wdMove);

if (sDirection == "Down")
gApplicationWord.Selection.MoveDown(Unit: Word.WdUnits.wdCharacter, Count: (System.Object)iNoOfChars, Extend: Word.WdMovementType.wdMove);
}
catch (Runtime.InteropServices.COMException objCOMException)
{
mobjCOMException = objCOMException;
}
catch (Exception objException)
{
mobjException = objException;
}

finally
{
if (gbDEBUG_WORD == true | ((!mobjCOMException == null) | (!mobjException == null)))
clsError.Handle("CharactersMove", msCLASSNAME, "move " + iNoOfChars + " characters to the '" + sDirection + "'.", mobjCOMException, mobjException);
}
}
Public Shared Sub CharactersMove(ByVal sDirection As String, _
ByVal iNoOfChars As Integer)

Try
If clsError.ErrorFlag() = True Then Exit Sub

If sDirection = "Left" Then
gApplicationWord.Selection.MoveLeft(Unit:=Word.WdUnits.wdCharacter, _
Count:=CType(iNoOfChars, System.Object), _
Extend:=Word.WdMovementType.wdMove)
End If

If sDirection = "Right" Then
gApplicationWord.Selection.MoveRight(Unit:=Word.WdUnits.wdCharacter, _
Count:=CType(iNoOfChars, System.Object), _
Extend:=Word.WdMovementType.wdMove)
End If

If sDirection = "Up" Then
gApplicationWord.Selection.MoveUp(Unit:=Word.WdUnits.wdCharacter, _
Count:=CType(iNoOfChars, System.Object), _
Extend:=Word.WdMovementType.wdMove)
End If

If sDirection = "Down" Then
gApplicationWord.Selection.MoveDown(Unit:=Word.WdUnits.wdCharacter, _
Count:=CType(iNoOfChars, System.Object), _
Extend:=Word.WdMovementType.wdMove)
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("CharactersMove", msCLASSNAME, _
"move " & iNoOfChars & " characters to the '" & sDirection & "'.", _
mobjCOMException, mobjException)
End If
End Try
End Sub

Selection_ColourChange

public static void ColourChange(System.Drawing.Color objColor)
{
try
{
if (clsError.ErrorFlag() == true)
return;

gApplicationWord.Selection.Font.Color = (Word.WdColor)Microsoft.VisualBasic.RGB(objColor.R, objColor.G, objColor.B);
}
catch (Runtime.InteropServices.COMException objCOMException)
{
mobjCOMException = objCOMException;
}
catch (Exception objException)
{
mobjException = objException;
}

finally
{
if (gbDEBUG_WORD == true | ((!mobjCOMException == null) | (!mobjException == null)))
clsError.Handle("ColourChange", msCLASSNAME, "change the colour of the selection.", mobjCOMException, mobjException);
}
}
Public Shared Sub ColourChange(ByVal objColor As System.Drawing.Color)

Try
If clsError.ErrorFlag() = True Then Exit Sub

gApplicationWord.Selection.Font.Color = _
CType(Microsoft.VisualBasic.RGB(objColor.R, objColor.G, objColor.B), Word.WdColor)

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("ColourChange", msCLASSNAME, _
"change the colour of the selection.", _
mobjCOMException, mobjException)
End If
End Try
End Sub

Selection_HasAnyShapes

public static bool HasAnyShapes()
{
try
{
if (clsError.ErrorFlag() == true)
return;

if (gApplicationWord.Selection.Range.ShapeRange.Count > 0)
HasAnyShapes = true;
}
catch (Runtime.InteropServices.COMException objCOMException)
{
HasAnyShapes = false;
}
catch (Exception objException)
{
HasAnyShapes = false;
}

finally
{
}
}
Public Shared Function HasAnyShapes() As Boolean

Try
If clsError.ErrorFlag() = True Then Exit Function

If gApplicationWord.Selection.Range.ShapeRange.Count > 0 Then
HasAnyShapes = True
End If

Catch objCOMException As System.Runtime.InteropServices.COMException
HasAnyShapes = False
Catch objException As Exception
HasAnyShapes = False

Finally

End Try
End Function

Selection_StoryMoveTop

public static void StoryMoveTop()
{
try
{
if (clsError.ErrorFlag() == true)
return;

gApplicationWord.Selection.HomeKey(Unit: Word.WdUnits.wdStory);
}
catch (Runtime.InteropServices.COMException objCOMException)
{
mobjCOMException = objCOMException;
}
catch (Exception objException)
{
mobjException = objException;
}

finally
{
if (gbDEBUG_WORD == true | ((!mobjCOMException == null) | (!mobjException == null)))
clsError.Handle("StoryMoveTop", msCLASSNAME, "move to the top of the document.", mobjCOMException, mobjException);
}
}
Public Shared Sub StoryMoveTop()

Try
If clsError.ErrorFlag() = True Then Exit Sub

gApplicationWord.Selection.HomeKey(Unit:=Word.WdUnits.wdStory)

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("StoryMoveTop", msCLASSNAME, _
"move to the top of the document.", _
mobjCOMException, mobjException)
End If
End Try
End Sub

Selection_StyleChange

public static void StyleChange(string sStyleName)
{
try
{
if (clsError.ErrorFlag() == true)
return;

if (gApplicationWord == null)
clszMessagesGeneral.Message("StyleChange - gApplicationWord is Nothing");

if (sStyleName.Length == 0)
clszMessagesGeneral.Message("sStyleName.Length = 0");

gApplicationWord.Selection.Style = gApplicationWord.ActiveDocument.Styles(sStyleName);
}
catch (Runtime.InteropServices.COMException objCOMException)
{
mobjCOMException = objCOMException;
}
catch (Exception objException)
{
mobjException = objException;
}

finally
{
if (gbDEBUG_WORD == true | ((!mobjCOMException == null) | (!mobjException == null)))
clsError.Handle("StyleChange", msCLASSNAME, "change the style of the selection.", mobjCOMException, mobjException);
}
}
Public Shared Sub StyleChange(ByVal sStyleName As String)

Try
If clsError.ErrorFlag() = True Then Exit Sub

If gApplicationWord Is Nothing Then
Call clszMessagesGeneral.Message("StyleChange - gApplicationWord is Nothing")
End If

If sStyleName.Length = 0 Then
Call clszMessagesGeneral.Message("sStyleName.Length = 0")
End If

gApplicationWord.Selection.Style = gApplicationWord.ActiveDocument.Styles(sStyleName)

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("StyleChange", msCLASSNAME, _
"change the style of the selection.", _
mobjCOMException, mobjException)
End If
End Try
End Sub

Text_TabInsert

public static void TabInsert(string sTabType, float sngMillimeters)
{
try
{
if (clsError.ErrorFlag() == true)
return;

Word.WdTabAlignment objTabAlignment;

switch (sTabType)
{
case "Left":
{
objTabAlignment = Word.WdTabAlignment.wdAlignTabLeft;
break;
}

case "Right":
{
objTabAlignment = Word.WdTabAlignment.wdAlignTabRight;
break;
}

case "Centre":
{
objTabAlignment = Word.WdTabAlignment.wdAlignTabCenter;
break;
}

case "Decimal":
{
objTabAlignment = Word.WdTabAlignment.wdAlignTabDecimal;
break;
}

case "Bar":
{
objTabAlignment = Word.WdTabAlignment.wdAlignTabBar;
break;
}

case "List":
{
objTabAlignment = Word.WdTabAlignment.wdAlignTabList;
break;
}
}

gApplicationWord.Selection.ParagraphFormat.TabStops.Add(Position: gApplicationWord.MillimetersToPoints(sngMillimeters), Alignment: (System.Object)objTabAlignment, Leader: Word.WdTabLeader.wdTabLeaderSpaces);
}

catch (Runtime.InteropServices.COMException objCOMException)
{
mobjCOMException = objCOMException;
}
catch (Exception objException)
{
mobjException = objException;
}

finally
{
if (gbDEBUG_WORD == true | ((!mobjCOMException == null) | (!mobjException == null)))
clsError.Handle("StoryMoveTop", msCLASSNAME, "move to the top of the document.", mobjCOMException, mobjException);
}
}
Public Shared Sub TabInsert(ByVal sTabType As String, _
ByVal sngMillimeters As Single)

Try
If clsError.ErrorFlag() = True Then Exit Sub

Dim objTabAlignment As Word.WdTabAlignment

Select Case sTabType
Case "Left" : objTabAlignment = Word.WdTabAlignment.wdAlignTabLeft
Case "Right" : objTabAlignment = Word.WdTabAlignment.wdAlignTabRight
Case "Centre" : objTabAlignment = Word.WdTabAlignment.wdAlignTabCenter
Case "Decimal" : objTabAlignment = Word.WdTabAlignment.wdAlignTabDecimal
Case "Bar" : objTabAlignment = Word.WdTabAlignment.wdAlignTabBar
Case "List" : objTabAlignment = Word.WdTabAlignment.wdAlignTabList
End Select

gApplicationWord.Selection.ParagraphFormat.TabStops.Add( _
Position:=gApplicationWord.MillimetersToPoints(sngMillimeters), _
Alignment:=CType(objTabAlignment, System.Object), _
Leader:=Word.WdTabLeader.wdTabLeaderSpaces)


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("StoryMoveTop", msCLASSNAME, _
"move to the top of the document.", _
mobjCOMException, mobjException)
End If
End Try
End Sub

TotalLinesInRange

public long Text_TotalLinesInRange(Word.Range oRange)
{
long ltotallines;

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

if (!(oRange == null))
ltotallines = oRange.ComputeStatistics(Word.WdStatistic.wdStatisticLines);

return ltotallines;
}
catch (Exception ex)
{
modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod(), null/* TODO Change to default(_) if this is not a reference type */, ex);
}
}
Public Function Text_TotalLinesInRange(ByVal oRange As Word.Range) As Long

Dim ltotallines As Long

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

If Not (oRange Is Nothing) Then
ltotallines = oRange.ComputeStatistics(Word.WdStatistic.wdStatisticLines)
End If

Return ltotallines

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

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