C# Snippets


AutoText_DoesNotExist

Public Sub AutoText_DoesNotExist(ByVal sAutoTextName As String, _
ByVal sFullPath As String)

Dim smessage As String = ""

smessage = "The Autotext '" & sAutoTextName & "' does not exist in the following document or template." & _
System.Environment.NewLine & _
System.Environment.NewLine & _
"'" & sFullPath & "'"

System.Windows.Forms.MessageBox.Show(smessage, _
My.Settings.APP_WINFORMS_TITLE, _
System.Windows.Forms.MessageBoxButtons.OK, _
System.Windows.Forms.MessageBoxIcon.Information)

smessage = smessage & " (" & System.Reflection.MethodBase.GetCurrentMethod.Name & ")"

Call Tracer_Add2("MESSAGE", smessage.Replace(System.Environment.NewLine, " ").Replace(" ", " "))
End Sub

CanBeInserted

Public Function AutoText_CanBeInserted(ByRef objDocument As Word.Document, _
ByVal sAutoTextName As String) As Boolean

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

If sAutoTextName.ToUpperInvariant.Contains("COVER") = False Then
If CType(gApplicationWord.Selection.Information(Word.WdInformation.wdActiveEndPageNumber), Integer) = 1 Then
System.Windows.Forms.MessageBox.Show("You cannot place this object on the first page.")
Return True
End If
End If

'If Era.WordUtils.SharedWordFunctions.EraTableCanBeInserted = False Then
' System.Windows.Forms.MessageBox.Show("The requested object cannot be inserted into protected regions, tables, headers/footers or frames.")
' Return True
'End If

Return False

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

Exists

Public Function AutoText_Exists(ByRef objDocument As Word.Document, _
ByVal sAutoTextName As String, _
Optional ByVal objRange As Word.Range = Nothing, _
Optional ByVal bThisDocument As Boolean = False) _
As Boolean

Dim objTemplate As Word.Template = Nothing

Try
If My.Settings.ERROR_OCCURRED = True Then Exit Function

If (objRange Is Nothing) Then
objRange = gApplicationWord.Selection.Range
End If

If (AttachedTemplate_Exists(objDocument) = True) Then
If (bThisDocument = False) Then
objTemplate = CType(objDocument.AttachedTemplate, Word.Template)
Else
objDocument = modLateBinding.Return_ThisDocument()
objTemplate = CType(objDocument.AttachedTemplate, Word.Template)
End If

Return AutoText_ExistsForce(objDocument, objTemplate, sAutoTextName)
End If

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

ExistsForce

Public Function AutoText_ExistsForce(ByRef objDocument As Word.Document, _
ByVal objTemplate As Word.Template, _
ByVal sAutoTextName As String) As Boolean

Dim objAutoTextEntry As Word.AutoTextEntry

Try
If My.Settings.ERROR_OCCURRED = True Then Exit Function

objAutoTextEntry = objTemplate.AutoTextEntries(sAutoTextName)
Return True
Catch ex As Exception
Return False
End Try
End Function

Insert

Public Sub AutoText_Insert(ByRef objDocument As Word.Document, _
ByVal objTemplate As Word.Template, _
ByVal objRange As Word.Range, _
ByVal sAutoTextName As String, _
Optional ByVal bCheckItExists As Boolean = True)

Dim objAutoTextEntry As Word.AutoTextEntry

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

If (bCheckItExists = True) Then
If modWordObjectModel.AutoText_Exists(objDocument, sAutoTextName) = False Then
Call modMessages.AutoText_DoesNotExist(sAutoTextName, objTemplate.FullName)
Exit Sub
End If
End If

objAutoTextEntry = objTemplate.AutoTextEntries.Item(sAutoTextName)
objAutoTextEntry.Insert(objRange, True)

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

InsertPicture

Public Shared Function Insert(ByVal sAutoTextName As String, _
Optional ByVal objTemplate As Word.Template = Nothing, _
Optional ByVal bReturnShape As Boolean = False) As Word.Shape

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

If (objTemplate Is Nothing) Then
objTemplate = CType(gApplicationWord.ActiveDocument.AttachedTemplate, Word.Template)
End If

If bReturnShape = True Then
'if an object is a picture then this does not work !!
Insert = objTemplate.AutoTextEntries(sAutoTextName).Insert( _
Where:=gApplicationWord.Selection.Range, _
RichText:=True).ShapeRange(1)
Else
objTemplate.AutoTextEntries(sAutoTextName).Insert( _
Where:=gApplicationWord.Selection.Range, _
RichText:=True)
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("Insert", msCLASSNAME, _
"insert the autotext entry '" & sAutoTextName & "' " & _
"from the attached template.", _
mobjCOMException, mobjException)
End If
End Try
End Function

Placeholders_Insert

Public Sub Placeholders_Insert(ByRef objDocument As Word.Document, _
ByVal sPlaceholderType As String, _
ByVal sPlaceholderPosition As String, _
ByVal sPlaceholderWidth As String, _
Optional ByVal sPageSize As String = "A4")

Try
If My.Settings.ERROR_OCCURRED = True Then Exit Sub

Select Case sPlaceholderType
Case "Single" : Call PlaceholdersInsert_Single(objDocument, sPlaceholderPosition, sPlaceholderWidth, sPageSize)
Case "2SideBySide" : Call PlaceholdersInsert_2SideBySide(objDocument, sPlaceholderPosition, sPlaceholderWidth, sPageSize)
Case "3SideBySide" : Call PlaceholdersInsert_3SideBySide(objDocument, sPlaceholderPosition, sPlaceholderWidth, sPageSize)
Case "Multiple" : Call PlaceholdersInsert_Multiple(objDocument, sPlaceholderPosition, sPlaceholderWidth, sPageSize)
End Select

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

PlaceholdersInsert_2SideBySide

Public Sub PlaceholdersInsert_2SideBySide(ByRef oDocument As Word.Document, _
ByVal sPlaceholderPosition As String, _
ByVal sPlaceholderWidth As String, _
ByVal sPageSize As String)

Try
If My.Settings.ERROR_OCCURRED = True Then Exit Sub

Select Case sPlaceholderWidth
Case "Indented"
Select Case sPlaceholderPosition
Case "Current"
Case "Top of Page"
Case "Bottom of Page"
Case "Whole Page"
End Select

Case "Full Width"
Select Case sPlaceholderPosition
'n is missing from autotext entry
Case "Current" :
Call AutoText_Insert(oDocument, "BC-Side by Side: current position", False)
Case "Top of Page"
Case "Bottom of Page" :
Call AutoText_Insert(oDocument, "BC-Side by Side: bottom page", False)
Case "Whole Page"
End Select

Case "Landscape"
Select Case sPlaceholderPosition
Case "Current" :
Call AutoText_Insert(oDocument, "BC-LScp-Side by Side: CP", False)
Case "Top of Page"
Case "Bottom of Page" :
Call AutoText_Insert(oDocument, "BC-LScp-Side by Side: BP", False)
Case "Whole Page" :
Call AutoText_Insert(oDocument, "BC-LScp-Side by Side: FP", False)
End Select

Case "Slide"
Select Case sPlaceholderPosition
Case "Current" :
Call AutoText_Insert(oDocument, "BC-LScp-Side by Side: CP S", False)
Case "Top of Page"
Case "Bottom of Page" :
Call AutoText_Insert(oDocument, "BC-LScp-Side by Side: BP S", False)
Case "Whole Page" :
Call AutoText_Insert(oDocument, "BC-LScp-Side by Side: FP S", False)
End Select
End Select

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

PlaceholdersInsert_3SideBySide

Public Sub PlaceholdersInsert_3SideBySide(ByRef objDocument As Word.Document, _
ByVal sPlaceholderPosition As String, _
ByVal sPlaceholderWidth As String, _
ByVal sPageSize As String)

Try
If My.Settings.ERROR_OCCURRED = True Then Exit Sub

Select Case sPlaceholderWidth
Case "Indented"

Case "Full Width"

Case "Landscape"
Select Case sPlaceholderPosition
Case "Current" :
Call AutoText_Insert(objDocument, "BC-LScp-3 SS CP", False)
Case "Top of Page"
Case "Bottom of Page" :
Call AutoText_Insert(objDocument, "BC-LScp-3 SS BP", False)
Case "Full Page"
End Select

Case "Slide"
Select Case sPlaceholderPosition
Case "Current" :
Call AutoText_Insert(objDocument, "BC-LScp-3 SS CP S", False)
Case "Top of Page"
Case "Bottom of Page" :
Call AutoText_Insert(objDocument, "BC-LScp-3 SS BP S", False)
Case "Full Page"
End Select
End Select

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

PlaceholdersInsert_Multiple

Public Sub PlaceholdersInsert_Multiple(ByRef oDocument As Word.Document, _
ByVal sPlaceholderPosition As String, _
ByVal sPlaceholderWidth As String, _
ByVal sPageSize As String)

Try
If My.Settings.ERROR_OCCURRED = True Then Exit Sub

Select Case sPlaceholderWidth
Case "Indented"

Case "Full Width"

Case "Landscape"
Select Case sPlaceholderPosition
Case "1 over 1" :
Call AutoText_Insert(oDocument, "BC-LScp-1 over 1", False)
Case "1 over 2" :
Call AutoText_Insert(oDocument, "BC-LScp-1 over 2", False)
Case "1 over 3" :
Call AutoText_Insert(oDocument, "BC-LScp-1 over 3", False)

Case "2 over 1" :
Call AutoText_Insert(oDocument, "BC-LScp-2 over 1", False)
Case "2 over 2" :
Call AutoText_Insert(oDocument, "BC-LScp-2 over 2", False)
Case "3 over 1" :
Call AutoText_Insert(oDocument, "BC-LScp-3 over 1", False)

Case "3 over 3" :
Call AutoText_Insert(oDocument, "BC-LScp-3 over 3 SS CP", False)
Case "3 over 3 with Bullets" :
Call AutoText_Insert(oDocument, "BC-LScp-3 over 3 SS CP Bull", False)

Case "1 Left 2 Right" :
Call AutoText_Insert(oDocument, "BC-LScp-1 Left and 2 Right", False)
Case "2 Left 1 Right" :
Call AutoText_Insert(oDocument, "BC-LScp-2 Left and 1 Right", False)
End Select

Case "Slide"
Select Case sPlaceholderPosition
Case "1 over 1" :
Call AutoText_Insert(oDocument, "BC-LScp-1 over 1 S", False)
Case "1 over 2" :
Call AutoText_Insert(oDocument, "BC-LScp-1 over 2 S", False)
Case "1 over 3" :
Call AutoText_Insert(oDocument, "BC-LScp-1 over 3 S", False)
Case "2 over 1" :
Call AutoText_Insert(oDocument, "BC-LScp-2 over 1 S", False)
Case "2 over 2" :
Call AutoText_Insert(oDocument, "BC-LScp-2 over 2 S", False)
Case "3 over 1" :
Call AutoText_Insert(oDocument, "BC-LScp-3 over 1 S", False)

Case "3 over 3" :
Call AutoText_Insert(oDocument, "BC-LScp-3 over 3 SS CP S", False)
Case "3 over 3 with Bullets" :
Call AutoText_Insert(oDocument, "BC-LScp-3 over 3 SS CP Bull S", False)

Case "1 Left 2 Right" :
Call AutoText_Insert(oDocument, "BC-LScp-1 Left and 2 Right S", False)
Case "2 Left 1 Right" :
Call AutoText_Insert(oDocument, "BC-LScp-2 Left and 1 Right S", False)
End Select

End Select

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

PlaceholdersInsert_Single

Public Sub PlaceholdersInsert_Single(ByRef oDocument As Word.Document, _
ByVal sPlaceholderPosition As String, _
ByVal sPlaceholderWidth As String, _
ByVal sPageSize As String)

Try
If My.Settings.ERROR_OCCURRED = True Then Exit Sub

Select Case sPlaceholderWidth
Case "Indented"
Select Case sPlaceholderPosition
Case "Current" :
Call AutoText_Insert(oDocument, "BC-Chart Indented", False)
Case "Top of Page"
Case "Bottom of Page" :
Call AutoText_Insert(oDocument, "BC-Chart Indented: bottom page", False)
Case "Whole Page"
End Select

Case "Full Width"
Select Case sPlaceholderPosition
Case "Current" :
Call AutoText_Insert(oDocument, "BC-Chart Wide", False)
Case "Top of Page"
Case "Bottom of Page" :
Call AutoText_Insert(oDocument, "BC-Chart Wide: bottom page", False)
Case "Whole Page"
End Select

Case "Landscape"
Select Case sPlaceholderPosition
Case "Current" :
Call AutoText_Insert(oDocument, "BC-LScp-Chart Wide CP", False)
Case "Top of Page"
Case "Bottom of Page" :
Call AutoText_Insert(oDocument, "BC-LScp-Chart Wide BP", False)
Case "Whole Page" :
Call AutoText_Insert(oDocument, "BC-LScp-Chart Wide FP", False)
End Select

Case "Slide"
Select Case sPlaceholderPosition
Case "Current" :
Call AutoText_Insert(oDocument, "BC-LScp-Chart Wide CP S", False)
Case "Top of Page"
Case "Bottom of Page" :
Call AutoText_Insert(oDocument, "BC-LScp-Chart Wide BP S", False)
Case "Whole Page" :
Call AutoText_Insert(oDocument, "BC-LScp-Chart Wide FP S", False)
End Select
End Select

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

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