C# - Snippets


Shape_AddShape

public static Excel.Shape Shape_AddShape(
Office.MsoAutoShapeType objAutoShapeType,
float sngleft,
float sngtop,
float sngwidth,
float sngheight,
Excel.XlPlacement enumPlacement = Excel.XlPlacement.xlMove)
{
try
{
if (clsError.ErrorFlag() == true)
return null;

Excel.Worksheet objwsh;
Excel.Shapes objshapes;
Excel.Shape objshape;

objwsh = (Excel.Worksheet)gApplicationExcel.ActiveSheet;
objshapes = (Excel.Shapes)objwsh.Shapes;

objshape = objshapes.AddShape(
Type: objAutoShapeType,
Left: sngleft,
Top: sngtop,
Width: sngwidth,
Height: sngheight
);

objshape.Fill.Visible = Microsoft.Office.Core.MsoTriState.msoTrue;
objshape.Placement = enumPlacement;

Shape_AddShape = objshape;
}
catch (System.Runtime.InteropServices.COMException objCOMException)
{
mobjCOMException = objCOMException;
}
catch (Exception objException)
{
mobjException = objException;
}
finally
{
if (gbDEBUG_EXCEL == true ||
(mobjCOMException != null || mobjException != null))
{
clsError.Handle(
"AddShape",
"clsShape",
"add the shape '" + objAutoShapeType.ToString() + "'.",
mobjCOMException,
mobjException
);
}
}

return Shape_AddShape;
}

Shape_ColourBackgroundGet

public static int Shape_ColourBackgroundGet(Excel.Shape objShape)
{
try
{
if (clsError.ErrorFlag() == true)
return 0;

Shape_ColourBackgroundGet = objShape.Fill.BackColor.SchemeColor;
}
catch (System.Runtime.InteropServices.COMException objCOMException)
{
mobjCOMException = objCOMException;
}
catch (Exception objException)
{
mobjException = objException;
}
finally
{
if (gbDEBUG_EXCEL == true ||
(mobjCOMException != null || mobjException != null))
{
clsError.Handle(
"ColourBackgroundGet",
"clsShape",
"",
mobjCOMException,
mobjException
);
}
}

return Shape_ColourBackgroundGet;
}

Shape_CoverShape

public static Excel.Shape Shape_CoverShape(
float sngLeft,
float sngTop,
float sngWidth,
float sngHeight,
Excel.XlPlacement enumPlacement = Excel.XlPlacement.xlMove)
{
try
{
if (clsError.ErrorFlag() == true)
return null;

string sshapename;

Excel.Shape objshape;

objshape = AddShape(
Microsoft.Office.Core.MsoAutoShapeType.msoShapeRectangle,
sngLeft,
sngTop,
sngWidth,
sngHeight
);

clsShape.Format(objshape, 23);

objshape.Fill.Transparency = 1.0;
objshape.Line.Visible = Microsoft.Office.Core.MsoTriState.msoFalse;
objshape.ZOrder(Office.MsoZOrderCmd.msoBringToFront);

objshape.Placement = enumPlacement;

Shape_CoverShape = objshape;
}
catch (System.Runtime.InteropServices.COMException objCOMException)
{
mobjCOMException = objCOMException;
}
catch (Exception objException)
{
mobjException = objException;
}
finally
{
if (gbDEBUG_EXCEL == true ||
(mobjCOMException != null || mobjException != null))
{
clsError.Handle(
"CoverShape",
"clsShape",
"",
mobjCOMException,
mobjException
);
}
}

return Shape_CoverShape;
}

Shape_Format

public static void Shape_Format(
Excel.Shape objShape,
int iSchemeColour,
bool bIncludeLine = true)
{
try
{
if (clsError.ErrorFlag() == true)
return;

objShape.Fill.Solid();
objShape.Fill.ForeColor.SchemeColor = iSchemeColour;

if (bIncludeLine == false)
{
objShape.Line.Visible = Microsoft.Office.Core.MsoTriState.msoFalse;
}
}
catch (System.Runtime.InteropServices.COMException objCOMException)
{
mobjCOMException = objCOMException;
}
catch (Exception objException)
{
mobjException = objException;
}
finally
{
if (gbDEBUG_EXCEL == true ||
(mobjCOMException != null || mobjException != null))
{
clsError.Handle(
"Format",
"clsShape",
"",
mobjCOMException,
mobjException
);
}
}
}

Shape_FormatText

public static void Shape_FormatText(
Excel.Shape objShape,
string sFontName = "Arial",
int iFontSize = 10)
{
try
{
if (clsError.ErrorFlag() == true)
return;

var chars = objShape.TextFrame.Characters(
1,
objShape.TextFrame.Characters().Count
);

chars.Font.Name = sFontName;
chars.Font.Size = iFontSize;
}
catch (System.Runtime.InteropServices.COMException objCOMException)
{
mobjCOMException = objCOMException;
}
catch (Exception objException)
{
mobjException = objException;
}
finally
{
if (gbDEBUG_EXCEL == true ||
(mobjCOMException != null || mobjException != null))
{
clsError.Handle(
"FormatText",
"clsShape",
"",
mobjCOMException,
mobjException
);
}
}
}

Shape_GroupSelected

public bool Shape_GroupSelected(Excel.Shape objShape)
{
try
{
if (clsError.ErrorFlag() == true)
return false;

string sshapeselected;

sshapeselected = clsShape.Type2Return(objShape);

if (sshapeselected == "Group")
Shape_GroupSelected = true;

if (sshapeselected == "AutoShape")
Shape_GroupSelected = false;
}
catch (System.Runtime.InteropServices.COMException objCOMException)
{
mobjCOMException = objCOMException;
}
catch (Exception objException)
{
mobjException = objException;
}
finally
{
if (gbDEBUG_EXCEL == true ||
(mobjCOMException != null || mobjException != null))
{
clsError.Handle(
"GroupSelected",
"clsShape",
"determine if the shape '" + objShape.Name + "' is a group.",
mobjCOMException,
mobjException
);
}
}

return Shape_GroupSelected;

Shape_InsertImageFromResource

public static void insertImageFromResources(
string cellReference,
Excel.Worksheet worksheet,
string imageName,
int offsetPixel = 0,
bool alignCenter = false)
{
try
{
object o = Properties.Resources.ResourceManager.GetObject(imageName);
System.Drawing.Image image = null;
float left, top, cellHeight;
string fileName = Constants.FOLDER_LOCAL_PATH + imageName + ".png";

if (o == null)
{
return;
}

if (o is System.Drawing.Image)
{
image = o as System.Drawing.Image;
}

if (System.IO.Directory.Exists(Constants.FOLDER_LOCAL_PATH) == false)
{
System.IO.Directory.CreateDirectory(Constants.FOLDER_LOCAL_PATH);
}


System.IO.File.Delete(fileName);
image.Save(fileName, System.Drawing.Imaging.ImageFormat.Png);

if (worksheet == null)
{
return;
}

Excel.Range cells = worksheet.get_Range(cellReference);

- left = System.Convert.ToSingle(cells.Left);
top = System.Convert.ToSingle(cells.Top);
cellHeight = System.Convert.ToSingle(cells.Height);

if (alignCenter && cellHeight > image.Height)
{
top += (cellHeight - image.Height) / 2;
}

// cells.RowHeight = CommonConstant.SINGLE_COMPANY_ROW_HEIGHT_TWO;
cells.HorizontalAlignment = Excel.XlHAlign.xlHAlignLeft;
cells.VerticalAlignment = Excel.XlVAlign.xlVAlignCenter;


System.Windows.Forms.Clipboard.SetDataObject(image, true);

Excel.Shape oInsertedShape;
oInsertedShape = worksheet.Shapes.AddPicture(
fileName,
Microsoft.Office.Core.MsoTriState.msoFalse,
Microsoft.Office.Core.MsoTriState.msoTrue,
left, top, image.Width, image.Height);

oInsertedShape.LockAspectRatio = Microsoft.Office.Core.MsoTriState.msoTrue;
oInsertedShape.Width = System.Convert.ToSingle(image.Width * 0.75188);

if (imageName == "Horizontal_Bar")
{
oInsertedShape.Width = oInsertedShape.Width - 2;
}
if (imageName == "barclays_logo")
{
oInsertedShape.Top = oInsertedShape.Top + 5; // +12; // 14;
}
if (imageName == "absa_logo")
{
oInsertedShape.Top = oInsertedShape.Top + 5; // +12; // 14;
}
if (imageName == "Vertical_Bar")
{
oInsertedShape.Top = oInsertedShape.Top + 4;
}

cells.VerticalAlignment = Excel.XlVAlign.xlVAlignCenter;


}
catch (System.Exception ex)
{
ExceptionHandling.MessageShow(System.Reflection.MethodBase.GetCurrentMethod(), ex);
}
}


Shape_IsATextBox

public static bool Shape_IsATextBox(Excel.Shape objShape)
{
try
{
if (clsError.ErrorFlag() == true)
return false;

Shape_IsATextBox = true;

// tests to see if you can obtain the length of any text
if (objShape.TextFrame.Characters().Text.Length == 0)
{
}
}
catch (System.Runtime.InteropServices.COMException)
{
Shape_IsATextBox = false;
}

return Shape_IsATextBox;
}

Shape_LineAdd

public static string Shape_LineAdd(
float sngLeftStart,
float sngTopStart,
float sngLeftFinish,
float sngTopFinish)
{
try
{
if (clsError.ErrorFlag() == true)
return null;

Excel.Worksheet objwsh;
Excel.Shapes objshapes;

objwsh = (Excel.Worksheet)gApplicationExcel.ActiveSheet;
objshapes = (Excel.Shapes)objwsh.Shapes;

objshapes.AddLine(
sngLeftStart,
sngTopStart,
sngLeftFinish,
sngTopFinish
).Select();

Shape_LineAdd = clsShape.SelectedNameGet();
}
catch (System.Runtime.InteropServices.COMException objCOMException)
{
mobjCOMException = objCOMException;
}
catch (Exception objException)
{
mobjException = objException;
}
finally
{
if (gbDEBUG_EXCEL == true ||
(mobjCOMException != null || mobjException != null))
{
clsError.Handle(
"LineAdd",
"clsShape",
"",
mobjCOMException,
mobjException
);
}
}

return Shape_LineAdd;
}

Shape_LineHorizontalAdd

public static string Shape_LineHorizontalAdd(
float sngLeftStart,
float sngTopStart,
float sngLength)
{
try
{
if (clsError.ErrorFlag() == true)
return null;

Shape_LineHorizontalAdd = clsShape.LineAdd(
sngLeftStart,
sngTopStart,
sngLeftStart + sngLength,
sngTopStart
);
}
catch (System.Runtime.InteropServices.COMException objCOMException)
{
mobjCOMException = objCOMException;
}
catch (Exception objException)
{
mobjException = objException;
}
finally
{
if (gbDEBUG_EXCEL == true ||
(mobjCOMException != null || mobjException != null))
{
clsError.Handle(
"LineHorizontalAdd",
"clsShape",
"",
mobjCOMException,
mobjException
);
}
}

return Shape_LineHorizontalAdd;
}

Shape_LineHorizontalOrVertical

public static string Shape_LineHorizontalOrVertical(
Excel.Shape objShape)
{
try
{
if (clsError.ErrorFlag() == true)
return null;

if (objShape.Height == 0)
{
Shape_LineHorizontalOrVertical = "Horizontal";
return Shape_LineHorizontalOrVertical;
}

if (objShape.Width == 0)
{
Shape_LineHorizontalOrVertical = "Vertical";
return Shape_LineHorizontalOrVertical;
}
}
catch (System.Runtime.InteropServices.COMException objCOMException)
{
mobjCOMException = objCOMException;
}
catch (Exception objException)
{
mobjException = objException;
}
finally
{
if (gbDEBUG_EXCEL == true ||
(mobjCOMException != null || mobjException != null))
{
clsError.Handle(
"LineHorizontalOrVertical",
"clsShape",
"determine if this line is horizontal or vertical.",
mobjCOMException,
mobjException
);
}
}

return Shape_LineHorizontalOrVertical;
}

Shape_LineVerticalAdd

public static string Shape_LineVerticalAdd(
float sngLeftStart,
float sngTopStart,
float sngLength)
{
try
{
if (clsError.ErrorFlag() == true)
return null;

Shape_LineVerticalAdd = clsShape.LineAdd(
sngLeftStart,
sngTopStart,
sngLeftStart,
sngTopStart + sngLength
);
}
catch (System.Runtime.InteropServices.COMException objCOMException)
{
mobjCOMException = objCOMException;
}
catch (Exception objException)
{
mobjException = objException;
}
finally
{
if (gbDEBUG_EXCEL == true ||
(mobjCOMException != null || mobjException != null))
{
clsError.Handle(
"LineVerticalAdd",
"clsShape",
"",
mobjCOMException,
mobjException
);
}
}

return Shape_LineVerticalAdd;
}

Shape_OrderBringToFront

public static void Shape_OrderBringToFront(Excel.Shape objShape)
{
try
{
// seems to cause an error if the shape is already at the front ?
objShape.ZOrder(Microsoft.Office.Core.MsoZOrderCmd.msoBringToFront);
}
catch (Runtime.InteropServices.COMException objCOMException)
{
}
}

Shape_PositionReturn

public static Excel.Shape Shape_PositionReturn(
float sngLeft,
float sngTop,
float sngWidth,
float sngHeight,
float sngAccuracy)
{
try
{
if (clsError.ErrorFlag() == true)
return null;

Excel.Worksheet objwsh;
Excel.Shapes objshapes;
int ishapecount;
Excel.Shape objshape;

objwsh = (Excel.Worksheet)gApplicationExcel.ActiveSheet;
objshapes = (Excel.Shapes)objwsh.Shapes;

for (ishapecount = 1; ishapecount <= objshapes.Count; ishapecount++)
{
objshape = objshapes.Item(ishapecount);

if (Math.Abs(objshape.Left - sngLeft) < sngAccuracy &&
Math.Abs(objshape.Top - sngTop) < sngAccuracy &&
Math.Abs(objshape.Height - sngHeight) < sngAccuracy &&
Math.Abs(objshape.Width - sngWidth) < sngAccuracy)
{
Shape_PositionReturn = objshape;
return Shape_PositionReturn;
}
else
{
Shape_PositionReturn = null;
}
}
}
catch (System.Runtime.InteropServices.COMException objCOMException)
{
mobjCOMException = objCOMException;
}
catch (Exception objException)
{
mobjException = objException;
}
finally
{
if (gbDEBUG_EXCEL == true ||
(mobjCOMException != null || mobjException != null))
{
clsError.Handle(
"PositionReturn",
"clsShape",
"",
mobjCOMException,
mobjException
);
}
}

return Shape_PositionReturn;
}

Shape_PositionSizeExists

public static bool Shape_PositionSizeExists(
float sngLeft,
float sngTop,
float sngWidth,
float sngHeight,
float sngAccuracy)
{
try
{
if (clsError.ErrorFlag() == true)
return false;

Excel.Worksheet objwsh;
Excel.Shapes objshapes;
int ishapecount;
Excel.Shape objshape;

objwsh = (Excel.Worksheet)gApplicationExcel.ActiveSheet;
objshapes = (Excel.Shapes)objwsh.Shapes;

for (ishapecount = 1; ishapecount <= objshapes.Count; ishapecount++)
{
objshape = objshapes.Item(ishapecount);

if (Math.Abs(objshape.Left - sngLeft) < sngAccuracy &&
Math.Abs(objshape.Top - sngTop) < sngAccuracy &&
Math.Abs(objshape.Height - sngHeight) < sngAccuracy &&
Math.Abs(objshape.Width - sngWidth) < sngAccuracy)
{
Shape_PositionSizeExists = true;
return Shape_PositionSizeExists;
}
else
{
Shape_PositionSizeExists = false;
}
}
}
catch (System.Runtime.InteropServices.COMException objCOMException)
{
mobjCOMException = objCOMException;
}
catch (Exception objException)
{
mobjException = objException;
}
finally
{
if (gbDEBUG_EXCEL == true ||
(mobjCOMException != null || mobjException != null))
{
clsError.Handle(
"PositionSizeExists",
"clsShape",
"",
mobjCOMException,
mobjException
);
}
}

return Shape_PositionSizeExists;
}

Shape_PositionSizeGetRef

public static void Shape_PositionSizeGetRef(
Excel.Shape objShape,
ref float sngLeft,
ref float sngTop,
ref float sngWidth,
ref float sngHeight)
{
try
{
if (clsError.ErrorFlag() == true)
return;

sngLeft = objShape.Left;
sngTop = objShape.Top;
sngHeight = objShape.Height;
sngWidth = objShape.Width;
}
catch (System.Runtime.InteropServices.COMException objCOMException)
{
mobjCOMException = objCOMException;
}
catch (Exception objException)
{
mobjException = objException;
}
finally
{
if (gbDEBUG_EXCEL == true ||
(mobjCOMException != null || mobjException != null))
{
clsError.Handle(
"PositionSizeGetRef",
"clsShape",
"",
mobjCOMException,
mobjException
);
}
}
}

Shape_Return

public static Excel.Shape Shape_Return(
string sShapeName,
int iShapeNo = -1)
{
try
{
if (clsError.ErrorFlag() == true)
return null;

Excel.Worksheet objwsh;
Excel.Shapes objshapes;

objwsh = (Excel.Worksheet)gApplicationExcel.ActiveSheet;
objshapes = (Excel.Shapes)objwsh.Shapes;

if (iShapeNo == -1)
{
Shape_Return = objshapes.Item(sShapeName);
}

if (sShapeName.Length == 0)
{
Shape_Return = objshapes.Item(iShapeNo);
}
}
catch (System.Runtime.InteropServices.COMException objCOMException)
{
mobjCOMException = objCOMException;
}
catch (Exception objException)
{
mobjException = objException;
}
finally
{
if (gbDEBUG_EXCEL == true ||
(mobjCOMException != null || mobjException != null))
{
clsError.Handle(
"ShapeReturn",
"clsShape",
"",
mobjCOMException,
mobjException
);
}
}

return Shape_Return;
}

Shape_Selected

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

string sTypeReturn;

sTypeReturn = Microsoft.VisualBasic.Information.TypeName(
gApplicationExcel.Selection
);

switch (sTypeReturn)
{
case "GroupObject":
Shape_Selected = true;
break;

case "Rectangle":
Shape_Selected = true;
break;

case "TextBox":
Shape_Selected = true;
break;

case "Line":
Shape_Selected = true;
break;

case "Oval":
Shape_Selected = true;
break;

case "Range":
Shape_Selected = false;
break;

default:
System.Windows.Forms.MessageBox.Show(
"Should this be added to the list of shapes : " + sTypeReturn
);
Shape_Selected = false;
break;
}
}
catch (System.Runtime.InteropServices.COMException objCOMException)
{
mobjCOMException = objCOMException;
}
catch (Exception objException)
{
mobjException = objException;
}
finally
{
if (gbDEBUG_EXCEL == true ||
(mobjCOMException != null || mobjException != null))
{
clsError.Handle(
"Selected",
"clsShape",
"",
mobjCOMException,
mobjException
);
}
}

return Shape_Selected;
}

Shape_SelectedNameGet

public static string Shape_SelectedNameGet()
{
try
{
if (clsError.ErrorFlag() == true)
return null;

if (clsShape.Selected() == true)
{
Shape_SelectedNameGet = clszLateBindingExcel.SelectionNameReturn();
}
else
{
Shape_SelectedNameGet = "Nothing";
}
}
catch (System.Runtime.InteropServices.COMException objCOMException)
{
mobjCOMException = objCOMException;
}
catch (Exception objException)
{
mobjException = objException;
}
finally
{
if (gbDEBUG_EXCEL == true ||
(mobjCOMException != null || mobjException != null))
{
clsError.Handle(
"SelectedNameGet",
"clsShape",
"return the name of the shape currently selected.",
mobjCOMException,
mobjException
);
}
}

return Shape_SelectedNameGet;
}

Shape_SelectedShapeReturn

public static Excel.Shape Shape_SelectedShapeReturn()
{
try
{
if (clsError.ErrorFlag() == true)
return null;

Excel.Worksheet objwsh;
Excel.Shapes objshapes;
Excel.Shape objshape;

objwsh = (Excel.Worksheet)gApplicationExcel.ActiveSheet;
objshapes = (Excel.Shapes)objwsh.Shapes;

Shape_SelectedShapeReturn = objshapes.Item(
clszLateBindingExcel.SelectionNameReturn()
);
}
catch (System.Runtime.InteropServices.COMException objCOMException)
{
mobjCOMException = objCOMException;
}
catch (Exception objException)
{
mobjException = objException;
}
finally
{
if (gbDEBUG_EXCEL == true ||
(mobjCOMException != null || mobjException != null))
{
clsError.Handle(
"SelectedShapeReturn",
"clsShape",
"return the selected shape object.",
mobjCOMException,
mobjException
);
}
}

return Shape_SelectedShapeReturn;
}

Shape_SelectShape

public static string Shape_SelectShape(
string sShapeName,
int iShapeNo = -1)
{
try
{
if (clsError.ErrorFlag() == true)
return null;

Excel.Worksheet objwsh;
Excel.Shapes objshapes;

objwsh = (Excel.Worksheet)gApplicationExcel.ActiveSheet;
objshapes = (Excel.Shapes)objwsh.Shapes;

if (iShapeNo == -1)
{
objshapes.Item(sShapeName).Select();
Shape_SelectShape = objshapes.Item(sShapeName).Name;
}

if (sShapeName.Length == 0)
{
objshapes.Item(iShapeNo).Select();
Shape_SelectShape = objshapes.Item(iShapeNo).Name;
}
}
catch (System.Runtime.InteropServices.COMException objCOMException)
{
mobjCOMException = objCOMException;
}
catch (Exception objException)
{
mobjException = objException;
}
finally
{
if (gbDEBUG_EXCEL == true ||
(mobjCOMException != null || mobjException != null))
{
clsError.Handle(
"SelectShape",
"clsShape",
"",
mobjCOMException,
mobjException
);
}
}

return Shape_SelectShape;
}

Shape_Shift

public static void Shape_Shift(
Excel.Shape objShape,
string sDirection,
float sngShiftAmount,
int iNumberTimes = 1)
{
try
{
if (clsError.ErrorFlag() == true)
return;

float sngshiftdistance;

switch (sDirection)
{
case "UP":
objShape.Top = objShape.Top - (iNumberTimes * sngShiftAmount);
break;

case "DOWN":
objShape.Top = objShape.Top + (iNumberTimes * sngShiftAmount);
break;

case "LEFT":
objShape.Left = objShape.Left - (iNumberTimes * sngShiftAmount);
break;

case "RIGHT":
objShape.Left = objShape.Left + (iNumberTimes * sngShiftAmount);
break;
}
}
catch (System.Runtime.InteropServices.COMException objCOMException)
{
mobjCOMException = objCOMException;
}
catch (Exception objException)
{
mobjException = objException;
}
finally
{
if (gbDEBUG_EXCEL == true ||
(mobjCOMException != null || mobjException != null))
{
clsError.Handle(
"Shift",
"clsShape",
"shift the shape '" + objShape.Name + "' in the '" + sDirection +
"' a distance of '" + sngShiftAmount + "'.",
mobjCOMException,
mobjException
);
}
}
}

Shape_TextDefine

public static void Shape_TextDefine(
Excel.Shape objShape,
string sText)
{
try
{
if (clsError.ErrorFlag() == true)
return;

objShape.TextFrame.Characters().Text = sText;
objShape.TextFrame.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;
objShape.TextFrame.VerticalAlignment = Excel.XlVAlign.xlVAlignCenter;
}
catch (System.Runtime.InteropServices.COMException objCOMException)
{
mobjCOMException = objCOMException;
}
catch (Exception objException)
{
mobjException = objException;
}
finally
{
if (gbDEBUG_EXCEL == true ||
(mobjCOMException != null || mobjException != null))
{
clsError.Handle(
"TextDefine",
"clsShape",
"",
mobjCOMException,
mobjException
);
}
}
}

Shape_TextGet

public static string Shape_TextGet(
Excel.Shape objShape)
{
try
{
if (clsError.ErrorFlag() == true)
return null;

Shape_TextGet = objShape.TextFrame.Characters().Text;
}
catch (System.Runtime.InteropServices.COMException objCOMException)
{
mobjCOMException = objCOMException;
}
catch (Exception objException)
{
mobjException = objException;
}
finally
{
if (gbDEBUG_EXCEL == true ||
(mobjCOMException != null || mobjException != null))
{
clsError.Handle(
"TextGet",
"clsShape",
"",
mobjCOMException,
mobjException
);
}
}

return Shape_TextGet;
}

Shape_TextSet

public static void Shape_TextSet(
Excel.Shape objShape,
string sText)
{
try
{
if (clsError.ErrorFlag() == true)
return;

objShape.TextFrame.Characters().Text = sText;
}
catch (System.Runtime.InteropServices.COMException objCOMException)
{
mobjCOMException = objCOMException;
}
catch (Exception objException)
{
mobjException = objException;
}
finally
{
if (gbDEBUG_EXCEL == true ||
(mobjCOMException != null || mobjException != null))
{
clsError.Handle(
"TextSet",
"clsShape",
"",
mobjCOMException,
mobjException
);
}
}
}

Shape_Type2Return

public static string Shape_Type2Return(
Excel.Shape objShape)
{
try
{
if (clsError.ErrorFlag() == true)
return null;

string stemp = null;

switch (objShape.Type)
{
case Office.MsoShapeType.msoAutoShape:
stemp = "AutoShape";
break;

case Office.MsoShapeType.msoCallout:
stemp = "CallOut";
break;

case Office.MsoShapeType.msoCanvas:
stemp = "Canvas";
break;

case Office.MsoShapeType.msoChart:
stemp = "Chart";
break;

case Office.MsoShapeType.msoComment:
stemp = "Comment";
break;

case Office.MsoShapeType.msoDiagram:
stemp = "Diagram";
break;

case Office.MsoShapeType.msoEmbeddedOLEObject:
stemp = "EmbeddedOLE";
break;

case Office.MsoShapeType.msoFormControl:
stemp = "FormControl";
break;

case Office.MsoShapeType.msoFreeform:
stemp = "FreeForm";
break;

case Office.MsoShapeType.msoGroup:
stemp = "Group";
break;

case Office.MsoShapeType.msoLine:
stemp = "Line";
break;

case Office.MsoShapeType.msoLinkedOLEObject:
stemp = "LinkedOLE";
break;

case Office.MsoShapeType.msoLinkedPicture:
stemp = "LinkedPicture";
break;

case Office.MsoShapeType.msoMedia:
stemp = "Media";
break;

case Office.MsoShapeType.msoOLEControlObject:
stemp = "OLEControlObject";
break;

case Office.MsoShapeType.msoPicture:
stemp = "Picture";
break;

case Office.MsoShapeType.msoPlaceholder:
stemp = "Placeholder";
break;

case Office.MsoShapeType.msoScriptAnchor:
stemp = "ScriptAnchor";
break;

case Office.MsoShapeType.msoShapeTypeMixed:
stemp = "ShapeTypeMixed";
break;

case Office.MsoShapeType.msoTable:
stemp = "Table";
break;

case Office.MsoShapeType.msoTextBox:
stemp = "TextBox";
break;

case Office.MsoShapeType.msoTextEffect:
stemp = "TextEffect";
break;
}

Shape_Type2Return = stemp;
}
catch (System.Runtime.InteropServices.COMException objCOMException)
{
mobjCOMException = objCOMException;
}
catch (Exception objException)
{
mobjException = objException;
}
finally
{
if (gbDEBUG_EXCEL == true ||
(mobjCOMException != null || mobjException != null))
{
clsError.Handle(
"Type2Return",
"clsShape",
"returns the type of the shape '" + objShape.Name + "'.",
mobjCOMException,
mobjException
);
}

Shape_UnGroup

public static string[] Shape_UnGroup(
Excel.Shape objShape)
{
try
{
if (clsError.ErrorFlag() == true)
return null;

Excel.ShapeRange objshaperange;
int ishapecount;
string[] asShapeNames;

objshaperange = objShape.Ungroup();

asShapeNames = new string[objshaperange.Count];

for (ishapecount = 1; ishapecount <= objshaperange.Count; ishapecount++)
{
objShape = objshaperange.Item(ishapecount);
asShapeNames[ishapecount - 1] = objShape.Name;
}

Shape_UnGroup = asShapeNames;
}
catch (System.Runtime.InteropServices.COMException objCOMException)
{
mobjCOMException = objCOMException;
}
catch (Exception objException)
{
mobjException = objException;
}
finally
{
if (gbDEBUG_EXCEL == true ||
(mobjCOMException != null || mobjException != null))
{
clsError.Handle(
"UnGroup",
"clsShape",
"",
mobjCOMException,
mobjException
);
}
}

return Shape_UnGroup;
}

Shapes_Clear

public static void clearSheetContent(Excel.Worksheet worksheet)
{
try
{
worksheet.Rows.Clear();
worksheet.Columns.Clear();
foreach (Excel.Shape shape in worksheet.Shapes)
{
if (shape != null)
{
shape.Delete();
}
}
worksheet.Cells.MergeCells = false;
}
catch (System.Exception ex)
{
ExceptionHandling.MessageShow(System.Reflection.MethodBase.GetCurrentMethod(), ex);
}
}


Shapes_Group

public static Excel.Shape Shapes_Group(
params string[] asShapeArray)
{
try
{
if (clsError.ErrorFlag() == true)
return null;

Excel.Worksheet objwsh;
Excel.Shapes objshapes;
string sshapename;
int ishapecount;

Excel.ShapeRange objshaperange;

objwsh = (Excel.Worksheet)gApplicationExcel.ActiveSheet;
objshapes = (Excel.Shapes)objwsh.Shapes;

ishapecount = 0;

foreach (string name in asShapeArray)
{
if (ishapecount == 0)
objshapes.Item(name).Select();
else
objshapes.Item(name).Select(false);

ishapecount++;
}

if (clsShapes.Selected == true)
{
objshaperange = clszLateBindingExcel.SelectionShapeRange;

return objshaperange.Group();
}
}
catch (System.Runtime.InteropServices.COMException objCOMException)
{
mobjCOMException = objCOMException;
}
catch (Exception objException)
{
mobjException = objException;
}
finally
{
if (gbDEBUG_EXCEL == true ||
(mobjCOMException != null || mobjException != null))
{
clsError.Handle(
"Group",
"clsShapes",
"group the array of shapes.",
mobjCOMException,
mobjException
);
}
}

return null;
}

Shapes_SelectAll

public static void Shapes_SelectAll(
string sWbkName = "")
{
try
{
if (clsError.ErrorFlag() == true)
return;

Excel.Worksheet objwsh;
Excel.Shapes objshapes;
int ishapecount;

objwsh = (Excel.Worksheet)gApplicationExcel.ActiveSheet;
objshapes = (Excel.Shapes)objwsh.Shapes;

// for each shape does not work !!
for (ishapecount = 1; ishapecount <= objshapes.Count; ishapecount++)
{
objshapes.Item(ishapecount).Select(false);
}
}
catch (System.Runtime.InteropServices.COMException objCOMException)
{
mobjCOMException = objCOMException;
}
catch (Exception objException)
{
mobjException = objException;
}
finally
{
if (gbDEBUG_EXCEL == true ||
(mobjCOMException != null || mobjException != null))
{
clsError.Handle(
"SelectAll",
"clsShapes",
"select all the shapes on the worksheet.",
mobjCOMException,
mobjException
);
}
}
}

Shapes_Selected

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

string sTypeReturn;

sTypeReturn = Microsoft.VisualBasic.Information.TypeName(
gApplicationExcel.Selection
);

switch (sTypeReturn)
{
case "DrawingObjects":
return true;

default:
return false;
}
}
catch (System.Runtime.InteropServices.COMException objCOMException)
{
mobjCOMException = objCOMException;
}
catch (Exception objException)
{
mobjException = objException;
}
finally
{
if (gbDEBUG_EXCEL == true ||
(mobjCOMException != null || mobjException != null))
{
clsError.Handle(
"Selected",
"clsShapes",
"",
mobjCOMException,
mobjException
);
}
}

return false;
}

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