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);
}
Public Shared Function FromColor(ByVal objColor As System.Drawing.Color) As Word.WdColor

FromColor = _
CType(Microsoft.VisualBasic.RGB(objColor.R, objColor.G, objColor.B), Word.WdColor)

End Function

Colours_FromRGB

public static Word.WdColor FromRGB(int iRGBRed, int iRGBGreen, int iRGBBlue)
{
FromRGB = (Word.WdColor)Microsoft.VisualBasic.RGB(iRGBRed, iRGBGreen, iRGBBlue);
}
Public Shared Function FromRGB(ByVal iRGBRed As Integer, _
ByVal iRGBGreen As Integer, _
ByVal iRGBBlue As Integer) As Word.WdColor

FromRGB = _
CType(Microsoft.VisualBasic.RGB(iRGBRed, iRGBGreen, iRGBBlue), Word.WdColor)

End Function

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;
}
}
Public Function Frame_CorrectPosition(ByRef objDocument As Word.Document, _
ByVal objFrame As Word.Frame, _
ByVal sngHorizontalPosition As Single, _
ByVal sngHorizontalDistanceFromText As Single, _
ByVal sngVerticalPosition As Single, _
ByVal sngVerticalDistanceFromText As Single) _
As Boolean

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

If ((objFrame.HorizontalPosition > sngHorizontalPosition - 2) And _
(objFrame.HorizontalPosition < sngHorizontalPosition + 2)) And _
((objFrame.HorizontalDistanceFromText > sngHorizontalDistanceFromText - 2) And _
(objFrame.HorizontalDistanceFromText < sngHorizontalDistanceFromText + 2)) And _
((objFrame.VerticalPosition > sngVerticalPosition - 2) And _
(objFrame.VerticalPosition < sngVerticalPosition + 2)) And _
((objFrame.VerticalDistanceFromText > sngVerticalDistanceFromText - 2) And _
(objFrame.VerticalDistanceFromText < sngVerticalDistanceFromText + 2)) Then

Return True
Else
Return False
End If

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

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;
}
}
Public Function Frame_Exists(ByRef objDocument As Word.Document, _
ByVal objRange As Word.Range, _
ByVal sngWidthMin As Single, _
ByVal sngWidthMax As Single, _
ByVal sContainsText As String) _
As Integer

'this returns the frame number

Dim iframeno As Integer
Dim objframe As Word.Frame
Dim stext As String

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

If objRange.Frames.Count > 0 Then
For iframeno = 1 To objRange.Frames.Count
objframe = objRange.Frames(iframeno)
stext = objframe.Range.Text

If Frame_IsNotTableObject(objframe) Then
If (objframe.Width > sngWidthMin) And _
(objframe.Width < sngWidthMax) And _
(stext.IndexOf(sContainsText) > -1) Then

Return iframeno
End If
End If
Next iframeno
End If

Return 0

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

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;
}
}
Public Function Frame_IsNotTableObject(ByVal objFrame As Word.Frame) As Boolean
Dim sngwidth As Single
Try
sngwidth = objFrame.Width
Return True

Catch ex As Exception
Return False
End Try
End Function

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 */;
}
}
Public Function Frame_Return(ByRef objDocument As Word.Document, _
ByVal objRange As Word.Range, _
ByVal sngWidthMin As Single, _
ByVal sngWidthMax As Single) As Word.Frame

Dim iframeno As Integer
Dim objframe As Word.Frame

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

If (objRange.Frames.Count = 0) Then
'display message
Return Nothing
End If

For iframeno = 1 To objRange.Frames.Count
objframe = objRange.Frames(iframeno)

If (objframe.Width > sngWidthMin And objframe.Width < sngWidthMax) Then
Return objframe
Exit Function
End If

Next iframeno

Return Nothing

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

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);
}
}
Public Sub Frame_Update(ByRef objDocument As Word.Document, _
ByVal objFrame As Word.Frame, _
ByVal sText As String)

Dim objRange As Word.Range

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

objRange = objFrame.Range
objRange.Text = sText

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

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);
}
}
Public Function Picture_Insert(ByVal objShapesCollection As Word.Shapes, _
ByVal sFolderPath As String, _
ByVal sFileName As String, _
ByVal sngLeft As Single, _
ByVal sngTop As Single) As Boolean

Try

objShapesCollection.AddPicture(FileName:=sFolderPath & sFileName, _
LinkToFile:=False, _
Left:=CType(sngLeft, Object), _
Top:=CType(sngTop, Object))

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

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;
}
}
Public Function Shape_AddPicture(ByVal objShapesCollection As Word.Shapes, _
ByVal sFolderPath As String, _
ByVal sFileName As String, _
ByVal sngWidth As Single, _
ByVal sngHeight As Single, _
ByVal sngHorizontalLeft As Single, _
ByVal sngVerticalTop As Single, _
ByVal rgeAnchor As Word.Range) As Word.Shape

Dim objshape As Word.Shape

Try
gApplicationWord.ScreenUpdating = False

If (rgeAnchor Is Nothing) Then
'no anchor
objshape = objShapesCollection.AddPicture(FileName:=sFolderPath & sFileName, _
LinkToFile:=False, _
Left:=CType(sngHorizontalLeft, Object), _
Top:=CType(sngVerticalTop, Object), _
Width:=CType(sngWidth, Object), _
Height:=CType(sngHeight, Object))
Else
'with anchor
objshape = objShapesCollection.AddPicture(FileName:=sFolderPath & sFileName, _
LinkToFile:=False, _
Left:=CType(sngHorizontalLeft, Object), _
Top:=CType(sngVerticalTop, Object), _
Width:=CType(sngWidth, Object), _
Height:=CType(sngHeight, Object), _
Anchor:=CType(rgeAnchor, Object))
End If

'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 ex As System.Exception
Call modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod, Nothing, ex)
Return Nothing
Finally
gApplicationWord.ScreenUpdating = True
End Try
End Function

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);
}
}
Public Sub Shape_Delete(ByVal objShapes As Word.Shapes, _
ByVal sShapeName As String)

Try
If (Shape_Exists(objShapes, sShapeName) = True) Then
objShapes.Item(sShapeName).Delete()
End If

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

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;
}
}
Public Function Shape_Exists(ByVal objShapes As Word.Shapes, _
ByVal sShapeName As String) _
As Boolean

Dim lcount As Long

Try
If (objShapes Is Nothing) Then Exit Function

For lcount = 1 To objShapes.Count
If (objShapes(lcount).Name = sShapeName) Then
Return True
End If
Next lcount

Return False

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

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);
}
}
Public Sub Shape_Format(ByVal objShape As Word.Shape)

Try
With objShape
.LockAnchor = 1 '
.LockAspectRatio = Microsoft.Office.Core.MsoTriState.msoTrue
.WrapFormat.AllowOverlap = 1
'.WrapFormat.DistanceBottom
'.WrapFormat.DistanceLeft
'.WrapFormat.DistanceRight
'.WrapFormat.DistanceTop
End With

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

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);
}
}
Public Sub Shape_Position(ByVal objShape As Word.Shape, _
ByVal sngTop As Single, _
ByVal wdRelativeVertical As Word.WdRelativeVerticalPosition, _
ByVal sngLeft As Single, _
ByVal wdRelativeHorizontal As Word.WdRelativeHorizontalPosition)

Try
With objShape
.Top = sngTop
.RelativeVerticalPosition = wdRelativeVertical

.Left = sngLeft
.RelativeHorizontalPosition = wdRelativeHorizontal
End With

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

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);
}
}
Public Sub Shape_Size(ByVal objshape As Word.Shape, _
ByVal sngHeight As Single, _
ByVal sngWidth As Single)

Try
If (objshape Is Nothing) Then Exit Sub

With objshape
.Height = sngHeight
.Width = sngWidth
End With

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

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);
}
}
Public Sub Shapes_Delete(ByVal objShapes As Word.Shapes, _
Optional ByVal sOnlyThisPrefix As String = "", _
Optional ByVal alArrayList As System.Collections.ArrayList = Nothing)

Dim sshapename As String = ""

Try
For icount As Integer = objShapes.Count To 1 Step -1
sshapename = objShapes.Item(icount).Name

'if the shape has been grouped then individual shapes cannot be deleted
If (alArrayList.Contains(sshapename) = True) Then
Tracer_Add2("SHAPES", sshapename & " NOT deleted", True)

Else
If (sOnlyThisPrefix.Length = 0) Then
objShapes.Item(icount).Delete()
Tracer_Add2("SHAPES", sshapename & " deleted", True)
Else
If (sshapename.Length > sOnlyThisPrefix.Length) Then
If (sshapename.Substring(0, sOnlyThisPrefix.Length) = sOnlyThisPrefix) Then
objShapes.Item(icount).Delete()
Tracer_Add2("SHAPES", sshapename & " deleted", True)
End If
End If
End If
End If

Next icount

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

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;
}
}
Public Function Shapes_RangeContains(ByVal objRange As Word.Range) As Boolean

Dim iNoOfShapes As Integer

Try
iNoOfShapes = objRange.ShapeRange.Count
If (iNoOfShapes = 0) Then Return False
If (iNoOfShapes > 0) Then Return True

Catch ex As Exception
Return False
End Try
End Function

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;
}
}
Public Function TextBox_Exists(ByRef objDocument As Word.Document, _
ByVal objRange As Word.Range, _
ByVal sngHeightMin As Single, _
ByVal sContainsText As String) As Boolean

Dim objshape As Word.Shape = Nothing
Dim stext As String

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

If modWordObjectModel.Shapes_RangeContains(objRange) Then
objshape = objRange.ShapeRange(1)

If (objshape.Type = Microsoft.Office.Core.MsoShapeType.msoTextBox) Then

objRange = objshape.TextFrame.TextRange
stext = objRange.Text

If (objshape.Height > sngHeightMin) And _
(stext.IndexOf(sContainsText) > -1) Then

Return True
End If
Else
Return False
End If
Else
Return False
End If

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

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);
}
}
Public Sub TextBox_Insert(ByRef objDocument As Word.Document)

Dim objrange As Word.Range
Dim objwordtemplate As Word.Template
Dim objtextboxshape As Word.Shape
Dim sauthorframetext As String

Try
objrange = objDocument.Sections(1).Range.Paragraphs(objDocument.Sections(1).Range.Paragraphs.Count - 1).Range
objrange.Collapse(Word.WdCollapseDirection.wdCollapseEnd)
objwordtemplate = CType(objDocument.AttachedTemplate, Word.Template)
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 ex As System.Exception
Call modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod, Nothing, ex)
End Try
End Sub

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 */;
}
}
Public Function TextBox_Return(ByRef objDocument As Word.Document, _
ByVal objRange As Word.Range, _
ByVal sngLeftMin As Single, _
ByVal sngLeftMax As Single, _
ByVal sngHeightMin As Single) As Word.Shape

Dim ishapeno As Integer
Dim objtextboxshape As Word.Shape

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

If modWordObjectModel.Range_ContainsShapes(objRange) = False Then
'display a message
Return Nothing
End If

For ishapeno = 1 To objRange.ShapeRange.Count
objtextboxshape = objRange.ShapeRange(ishapeno)

If objtextboxshape.Left > sngLeftMin And objtextboxshape.Left < sngLeftMax Then
If objtextboxshape.Height > sngHeightMin Then
Return objtextboxshape
End If
End If

Next ishapeno

Return Nothing

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

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);
}
}
Public Sub TextBox_Update(ByRef objDocument As Word.Document, _
ByVal objTextBoxShape As Word.Shape)

Dim objRange As Word.Range

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

objRange = objTextBoxShape.TextFrame.TextRange
objRange.Text = "this text"

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

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;
}
}
Public Function TextBox_CorrectPosition(ByRef objDocument As Word.Document, _
ByVal objTextBoxShape As Word.Shape, _
ByVal sngLeftMin As Single, _
ByVal sngLeftMax As Single) _
As Boolean

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

If (objTextBoxShape.Left > sngLeftMin And _
objTextBoxShape.Left < sngLeftMax) Then

Return True
End If

Return False

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

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