C# - Snippets


Colours_FromColor

public static Word.WdColor FromColor(System.Drawing.Color objColor)
{
FromColor = (Word.WdColor)Microsoft.VisualBasic.RGB(objColor.R, objColor.G, objColor.B);
}
VB

Colours_FromRGB

public static Word.WdColor FromRGB(int iRGBRed, int iRGBGreen, int iRGBBlue)
{
FromRGB = (Word.WdColor)Microsoft.VisualBasic.RGB(iRGBRed, iRGBGreen, iRGBBlue);
}
VB

Frame_CorrectPosition

public bool Frame_CorrectPosition(ref Word.Document objDocument, Word.Frame objFrame, float sngHorizontalPosition, float sngHorizontalDistanceFromText, float sngVerticalPosition, float sngVerticalDistanceFromText)
{
try
{
Tracer_Add2("SUBROUTINE", System.Reflection.MethodBase.GetCurrentMethod().Name + " start");
if (My.Settings.ERROR_OCCURRED == true)
return;

if (((objFrame.HorizontalPosition > sngHorizontalPosition - 2) & (objFrame.HorizontalPosition < sngHorizontalPosition + 2)) & ((objFrame.HorizontalDistanceFromText > sngHorizontalDistanceFromText - 2) & (objFrame.HorizontalDistanceFromText < sngHorizontalDistanceFromText + 2)) & ((objFrame.VerticalPosition > sngVerticalPosition - 2) & (objFrame.VerticalPosition < sngVerticalPosition + 2)) & ((objFrame.VerticalDistanceFromText > sngVerticalDistanceFromText - 2) & (objFrame.VerticalDistanceFromText < sngVerticalDistanceFromText + 2)))
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);
return false;
}
}
VB

Frame_Exists

public int Frame_Exists(ref Word.Document objDocument, Word.Range objRange, float sngWidthMin, float sngWidthMax, string sContainsText)
{

// this returns the frame number

int iframeno;
Word.Frame objframe;
string stext;

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

if (objRange.Frames.Count > 0)
{
for (iframeno = 1; iframeno <= objRange.Frames.Count; iframeno++)
{
objframe = objRange.Frames(iframeno);
stext = objframe.Range.Text;

if (Frame_IsNotTableObject(objframe))
{
if ((objframe.Width > sngWidthMin) & (objframe.Width < sngWidthMax) & (stext.IndexOf(sContainsText) > -1))
return iframeno;
}
}
}

return 0;
}
catch (Exception ex)
{
modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod(), null/* TODO Change to default(_) if this is not a reference type */, ex);
return -1;
}
}
VB

Frame_Insert

Inserts a frame at the current position in the active document.
Public Sub Frame_Insert(ByRef objDocument As Word.Document, _
ByVal sngWidth As Single, _
ByVal sngHeight As Single)

Dim objtextboxshape As Word.Shape

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

objtextboxshape = objDocument.Shapes.AddTextbox(Office.MsoTextOrientation.msoTextOrientationHorizontal, _
50, 134.9, 100, 50)

objtextboxshape.ConvertToFrame()

With objDocument.Frames(1)
.WidthRule = Word.WdFrameSizeRule.wdFrameExact
.Width = sngWidth 'CentimetersToPoints(4.28)

.HeightRule = Word.WdFrameSizeRule.wdFrameAuto
.Height = sngHeight

.HorizontalPosition = 0 'CentimetersToPoints(1.5)

.RelativeHorizontalPosition = Word.WdRelativeHorizontalPosition.wdRelativeHorizontalPositionMargin
.HorizontalDistanceFromText = gApplicationWord.CentimetersToPoints(0.76)

.VerticalPosition = 0 'CentimetersToPoints(0.15)
.RelativeVerticalPosition = Word.WdRelativeVerticalPosition.wdRelativeVerticalPositionParagraph
.VerticalDistanceFromText = 0 'CentimetersToPoints(0.32)

.LockAnchor = False
.TextWrap = True

.Borders(Word.WdBorderType.wdBorderTop).LineStyle = Word.WdLineStyle.wdLineStyleNone
.Borders(Word.WdBorderType.wdBorderLeft).LineStyle = Word.WdLineStyle.wdLineStyleNone
.Borders(Word.WdBorderType.wdBorderBottom).LineStyle = Word.WdLineStyle.wdLineStyleNone
.Borders(Word.WdBorderType.wdBorderRight).LineStyle = Word.WdLineStyle.wdLineStyleNone
End With

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

Frame_IsNotTableObject

public bool Frame_IsNotTableObject(Word.Frame objFrame)
{
float sngwidth;
try
{
sngwidth = objFrame.Width;
return true;
}
catch (Exception ex)
{
return false;
}
}
VB

Frame_Return

public Word.Frame Frame_Return(ref Word.Document objDocument, Word.Range objRange, float sngWidthMin, float sngWidthMax)
{
int iframeno;
Word.Frame objframe;

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 */;

if ((objRange.Frames.Count == 0))
// display message
return null/* TODO Change to default(_) if this is not a reference type */;

for (iframeno = 1; iframeno <= objRange.Frames.Count; iframeno++)
{
objframe = objRange.Frames(iframeno);

if ((objframe.Width > sngWidthMin & objframe.Width < sngWidthMax))
{
return objframe;
return;
}
}

return null/* TODO Change to default(_) if this is not a reference type */;
}
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 */;
}
}
VB

Frame_Update

public void Frame_Update(ref Word.Document objDocument, Word.Frame objFrame, string sText)
{
Word.Range objRange;

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

objRange = objFrame.Range;
objRange.Text = sText;
}
catch (Exception ex)
{
modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod(), null/* TODO Change to default(_) if this is not a reference type */, ex);
}
}
VB

Picture_Insert

public bool Picture_Insert(Word.Shapes objShapesCollection, string sFolderPath, string sFileName, float sngLeft, float sngTop)
{
try
{
objShapesCollection.AddPicture(FileName: sFolderPath + sFileName, LinkToFile: false, Left: (object)sngLeft, Top: (object)sngTop);
}
catch (Exception ex)
{
modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod(), null/* TODO Change to default(_) if this is not a reference type */, ex);
}
}
VB

Shape_AddPicture

public Word.Shape Shape_AddPicture(Word.Shapes objShapesCollection, string sFolderPath, string sFileName, float sngWidth, float sngHeight, float sngHorizontalLeft, float sngVerticalTop, Word.Range rgeAnchor)
{
Word.Shape objshape;

try
{
gApplicationWord.ScreenUpdating = false;

if ((rgeAnchor == null))
// no anchor
objshape = objShapesCollection.AddPicture(FileName: sFolderPath + sFileName, LinkToFile: false, Left: (object)sngHorizontalLeft, Top: (object)sngVerticalTop, Width: (object)sngWidth, Height: (object)sngHeight);
else
// with anchor
objshape = objShapesCollection.AddPicture(FileName: sFolderPath + sFileName, LinkToFile: false, Left: (object)sngHorizontalLeft, Top: (object)sngVerticalTop, Width: (object)sngWidth, Height: (object)sngHeight, Anchor: (object)rgeAnchor);

// Documentation says the relatives are these but they are not
// Width = Word.WdRelativeHorizontalPosition.wdRelativeHorizontalPositionPage, _
// Height = Word.WdRelativeVerticalPosition.wdRelativeVerticalPositionPage)

// Width = Word.WdRelativeHorizontalPosition.wdRelativeHorizontalPositionColumn, _
// Height = Word.WdRelativeVerticalPosition.wdRelativeVerticalPositionPage)

objshape.RelativeVerticalPosition = Word.WdRelativeVerticalPosition.wdRelativeVerticalPositionPage;
objshape.Top = sngVerticalTop;

objshape.RelativeHorizontalPosition = Word.WdRelativeHorizontalPosition.wdRelativeHorizontalPositionPage;
objshape.Left = sngHorizontalLeft;

return objshape;
}
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 */;
}
finally
{
gApplicationWord.ScreenUpdating = true;
}
}
VB

Shape_Delete

public void Shape_Delete(Word.Shapes objShapes, string sShapeName)
{
try
{
if ((Shape_Exists(objShapes, sShapeName) == true))
objShapes.Item(sShapeName).Delete();
}
catch (Exception ex)
{
modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod(), null/* TODO Change to default(_) if this is not a reference type */, ex);
}
}

Shape_Exists

public bool Shape_Exists(Word.Shapes objShapes, string sShapeName)
{
long lcount;

try
{
if ((objShapes == null))
return;

for (lcount = 1; lcount <= objShapes.Count; lcount++)
{
if ((objShapes(lcount).Name == sShapeName))
return true;
}

return false;
}
catch (Exception ex)
{
modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod(), null/* TODO Change to default(_) if this is not a reference type */, ex);
return false;
}
}

Shape_Format

public void Shape_Format(Word.Shape objShape)
{
try
{
{
var withBlock = objShape;
withBlock.LockAnchor = 1; //
withBlock.LockAspectRatio = Microsoft.Office.Core.MsoTriState.msoTrue;
withBlock.WrapFormat.AllowOverlap = 1;
}
}
catch (Exception ex)
{
modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod(), null/* TODO Change to default(_) if this is not a reference type */, ex);
}
}

Shape_Position

public void Shape_Position(Word.Shape objShape, float sngTop, Word.WdRelativeVerticalPosition wdRelativeVertical, float sngLeft, Word.WdRelativeHorizontalPosition wdRelativeHorizontal)
{
try
{
{
var withBlock = objShape;
withBlock.Top = sngTop;
withBlock.RelativeVerticalPosition = wdRelativeVertical;

withBlock.Left = sngLeft;
withBlock.RelativeHorizontalPosition = wdRelativeHorizontal;
}
}
catch (Exception ex)
{
modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod(), null/* TODO Change to default(_) if this is not a reference type */, ex);
}
}

Shape_Size

public void Shape_Size(Word.Shape objshape, float sngHeight, float sngWidth)
{
try
{
if ((objshape == null))
return;

{
var withBlock = objshape;
withBlock.Height = sngHeight;
withBlock.Width = sngWidth;
}
}
catch (Exception ex)
{
modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod(), null/* TODO Change to default(_) if this is not a reference type */, ex);
}
}

Shapes_Delete

public void Shapes_Delete(Word.Shapes objShapes, string sOnlyThisPrefix = "", System.Collections.ArrayList alArrayList = null)
{
string sshapename = "";

try
{
for (int icount = objShapes.Count; icount >= 1; icount += -1)
{
sshapename = objShapes.Item(icount).Name;

// if the shape has been grouped then individual shapes cannot be deleted
if ((alArrayList.Contains(sshapename) == true))
Tracer_Add2("SHAPES", sshapename + " NOT deleted", true);
else if ((sOnlyThisPrefix.Length == 0))
{
objShapes.Item(icount).Delete();
Tracer_Add2("SHAPES", sshapename + " deleted", true);
}
else if ((sshapename.Length > sOnlyThisPrefix.Length))
{
if ((sshapename.Substring(0, sOnlyThisPrefix.Length) == sOnlyThisPrefix))
{
objShapes.Item(icount).Delete();
Tracer_Add2("SHAPES", sshapename + " deleted", true);
}
}
}
}
catch (Exception ex)
{
modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod(), null/* TODO Change to default(_) if this is not a reference type */, ex);
}
}

Shapes_RangeContains

public bool Shapes_RangeContains(Word.Range objRange)
{
int iNoOfShapes;

try
{
iNoOfShapes = objRange.ShapeRange.Count;
if ((iNoOfShapes == 0))
return false;
if ((iNoOfShapes > 0))
return true;
}
catch (Exception ex)
{
return false;
}
}

TextBox_Exists

public bool TextBox_Exists(ref Word.Document objDocument, Word.Range objRange, float sngHeightMin, string sContainsText)
{
Word.Shape objshape = null/* TODO Change to default(_) if this is not a reference type */;
string stext;

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

if (modWordObjectModel.Shapes_RangeContains(objRange))
{
objshape = objRange.ShapeRange(1);

if ((objshape.Type == Microsoft.Office.Core.MsoShapeType.msoTextBox))
{
objRange = objshape.TextFrame.TextRange;
stext = objRange.Text;

if ((objshape.Height > sngHeightMin) & (stext.IndexOf(sContainsText) > -1))
return true;
}
else
return false;
}
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);
return false;
}
}

TextBox_Insert

public void TextBox_Insert(ref Word.Document objDocument)
{
Word.Range objrange;
Word.Template objwordtemplate;
Word.Shape objtextboxshape;
string sauthorframetext;

try
{
objrange = objDocument.Sections(1).Range.Paragraphs(objDocument.Sections(1).Range.Paragraphs.Count - 1).Range;
objrange.Collapse(Word.WdCollapseDirection.wdCollapseEnd);
objwordtemplate = (Word.Template)objDocument.AttachedTemplate;
objwordtemplate.AutoTextEntries("BC-Cover Sidebar Text").Insert(objrange);

objtextboxshape = modWordObjectModel.TextBox_Return(objDocument, objDocument.Sections(1).Range, 410, 425, 150);
objrange = objtextboxshape.TextFrame.TextRange;

objrange.MoveStartUntil("@", Word.WdConstants.wdForward);
objrange.MoveStart(Word.WdUnits.wdParagraph, -3);

sauthorframetext = objtextboxshape.TextFrame.TextRange.Text;

objtextboxshape.TextFrame.TextRange.Text = sauthorframetext;
objtextboxshape.TextFrame.TextRange.Style = "A-Name";
}
catch (Exception ex)
{
modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod(), null/* TODO Change to default(_) if this is not a reference type */, ex);
}
}

TextBox_Return

public Word.Shape TextBox_Return(ref Word.Document objDocument, Word.Range objRange, float sngLeftMin, float sngLeftMax, float sngHeightMin)
{
int ishapeno;
Word.Shape objtextboxshape;

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 */;

if (modWordObjectModel.Range_ContainsShapes(objRange) == false)
// display a message
return null/* TODO Change to default(_) if this is not a reference type */;

for (ishapeno = 1; ishapeno <= objRange.ShapeRange.Count; ishapeno++)
{
objtextboxshape = objRange.ShapeRange(ishapeno);

if (objtextboxshape.Left > sngLeftMin & objtextboxshape.Left < sngLeftMax)
{
if (objtextboxshape.Height > sngHeightMin)
return objtextboxshape;
}
}

return null/* TODO Change to default(_) if this is not a reference type */;
}
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 */;
}
}

TextBox_Update

public void TextBox_Update(ref Word.Document objDocument, Word.Shape objTextBoxShape)
{
Word.Range objRange;

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

objRange = objTextBoxShape.TextFrame.TextRange;
objRange.Text = "this text";
}
catch (Exception ex)
{
modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod(), null/* TODO Change to default(_) if this is not a reference type */, ex);
}
}

TextBoxes_CorrectPosition

public bool TextBox_CorrectPosition(ref Word.Document objDocument, Word.Shape objTextBoxShape, float sngLeftMin, float sngLeftMax)
{
try
{
Tracer_Add2("SUBROUTINE", System.Reflection.MethodBase.GetCurrentMethod().Name + " start");
if (My.Settings.ERROR_OCCURRED == true)
return default(Boolean);

if ((objTextBoxShape.Left > sngLeftMin & objTextBoxShape.Left < sngLeftMax))
return true;

return false;
}
catch (Exception ex)
{
modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod(), null/* TODO Change to default(_) if this is not a reference type */, ex);
return false;
}
}

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