C# Snippets


Add

Public Function Templates_GlobalAdd(ByVal sFileName As String) As Word.AddIn

Dim smessage As String

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

If modGeneral.File_Exists(sFileName, True) = True Then
Return gApplicationWord.AddIns.Add(sFileName, True)
Else
Return Nothing
End If

Catch ex As System.Exception

smessage = "load the following Word Template." & _
System.Environment.NewLine & _
"'" & sFileName & "'" & _
System.Environment.NewLine & _
System.Environment.NewLine & _
"Please try closing and reopening Word." & _
System.Environment.NewLine & _
System.Environment.NewLine & _
sProblemPersistsScreenShot

Call modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod, Nothing, ex, _
smessage, True, , , , System.Windows.Forms.MessageBoxIcon.Warning)

Return Nothing
End Try
End Function

AttachCorrectTemplate

source code
Public Function AttachCorrectActiveDocTemplate(ByRef TargetDocument As Word.Document) As Boolean

Dim TemplateType As TemplateType = GetTemplateType(TargetDocument)

Dim strTemplatePath As String

Select Case TemplateType

Case FICCClientApp.TemplateType.Feature

strTemplatePath = getTemplateLocation("ResearchTemplate_Feature.dot")

Case FICCClientApp.TemplateType.Landscape

strTemplatePath = getTemplateLocation("ResearchTemplate_Landscape.dot")

Case FICCClientApp.TemplateType.AbsaReport

strTemplatePath = getTemplateLocation("ResearchTemplate_ABSAReport.dot")

Case FICCClientApp.TemplateType.AbsaFeature

strTemplatePath = getTemplateLocation("ResearchTemplate_ABSAFeature.dot")

Case Else 'FICCClientApp.TemplateType.Report

strTemplatePath = getTemplateLocation("ResearchTemplate_Report.dot")

End Select

Try

Dim attachedTemplate As Word.Template = CType(TargetDocument.AttachedTemplate, Word.Template)

If attachedTemplate.FullName.ToUpperInvariant = strTemplatePath.ToUpperInvariant Then

Return True

Else

Try

TargetDocument.AttachedTemplate = strTemplatePath

Return True

Catch ex As Exception

Return False

End Try

End If

Catch ex As Exception
Call modMessages.ErrorMessage(System.Reflection.MethodBase.GetCurrentMethod, Nothing, ex, _
"check / change the attached template", False, False)
Return False

End Try


End Function

AttachedTemplateGet

Public Shared Function AttachedTemplateGet() As String
Try
If clsError.ErrorFlag() = True Then Exit Function

AttachedTemplateGet = _
CType(gApplicationWord.ActiveDocument.AttachedTemplate, Word.Template).Name

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("AttachedTemplateGet", msCLASSNAME, _
"return the name of the template attached to the active document.", _
mobjCOMException, mobjException)
End If
End Try
End Function
'********************************************************************************

Available

Public Function Template_Available(ByVal objTemplate As Word.Template, _
ByVal bInformUser As Boolean) As Boolean

Dim bavailable As Boolean
Dim stemplatename As String

Try
Call Tracer_Add2("SUBROUTINE", System.Reflection.MethodBase.GetCurrentMethod.Name & " start")
bavailable = objTemplate.Saved
Return True

Catch ex As System.Exception
If (bInformUser = True) Then

stemplatename = gSettingsFile.UserProfileFolder & My.Settings.APP_FILENAME_USERPROFILE_CUSTOMISATION

'Call modMessages.Template_Unavailable(stemplatename)
End If
Return False
End Try
End Function

Exists

Public Function Templates_GlobalExists(ByVal sFileName As String, _
Optional ByVal bInformUser As Boolean = True) As Boolean

Dim itemplatecount As System.Int32
Dim bfound As Boolean = False
Dim AddinToCheck As String = ""

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

For itemplatecount = 1 To gApplicationWord.AddIns.Count

Try
AddinToCheck = ""
AddinToCheck = gApplicationWord.AddIns.Item(itemplatecount).Path & "\" & gApplicationWord.AddIns.Item(itemplatecount).Name
Catch ex As System.Exception
Call modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod, Nothing, ex, _
"Error while attempting to check existence of template (" & sFileName & ")", bInformUser, False)
End Try

modGeneral.Tracer_Add2("WORD", "Checked add-in " & itemplatecount.ToString & " (" & AddinToCheck & ")", False)

If AddinToCheck = sFileName Then
bfound = True
End If

Next itemplatecount

If (bfound = True) Then
If bInformUser = True Then
Call modMessages.Exception_Warning(System.Reflection.MethodBase.GetCurrentMethod, _
sFileName & " has already been installed")
End If
End If

Catch ex As System.Exception
Call modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod, Nothing, ex, _
"Error while attempting to check existence of template (" & sFileName & ")", , False)
End Try
Return bfound

End Function

Exists

Public Function Templates_NormalExists(Optional ByVal bInformUser As Boolean = True) _
As Boolean

Dim objtemplate As Word.Template
Dim itemplatecount As System.Int32
Dim bfound As Boolean = False

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

'check if the Normal template exists in the Templates collection
'This sometimes has the name "Normal" or the name "Normal.dot"

For itemplatecount = 1 To gApplicationWord.Templates.Count
If gApplicationWord.Templates(itemplatecount).Name.StartsWith("Normal") = True Then
objtemplate = gApplicationWord.Templates(itemplatecount)
bfound = True
Exit For
'even though the Normal.dot template exists in the collection the actual file may not actually exist.
'NormalExists = clsFile.Exists(objtemplate.Path, "Normal.dot", "")
End If
Next itemplatecount

If (bfound = False) Then
If bInformUser = True Then
Call modMessages.Templates_NormalDoesNotExistInformation()
End If
End If

Catch ex As System.Exception
Call modMessages.ErrorMessage(System.Reflection.MethodBase.GetCurrentMethod, ex.Message, _
"determine if the global template, Normal.dot exists.")
End Try
End Function
'************************************************************************************
Public Shared Function NormalTemplateExists() As Boolean
Try

Dim sfolderpath As String
sfolderpath = gApplicationWord.Templates("Normal.dot").Path

NormalTemplateExists = True

Catch objCOMException As System.Runtime.InteropServices.COMException
NormalTemplateExists = False
Catch objException As Exception
NormalTemplateExists = False
End Try

End Function
'********************************************************************************

Exists

source code
Public Function AttachedTemplate_Exists(ByRef objDocument As Word.Document, _
Optional ByVal bInformUser As Boolean = False) As Boolean

Dim objTemplate As Word.Template = Nothing

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

objTemplate = CType(objDocument.AttachedTemplate, Word.Template)
Return True

Catch ex As Exception
If (bInformUser = True) Then
modMessages.Template_AttachedInvalid()
End If
Return False
End Try
End Function
'************************************************************************************

Installed

Public Function Templates_GlobalInstalled(ByVal sFileName As String) As Boolean

Dim itemplatecount As Integer

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

For itemplatecount = 1 To gApplicationWord.AddIns.Count
If (gApplicationWord.AddIns.Item(itemplatecount).Name = sFileName) Then
Return True
End If
Next itemplatecount
Return False

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

InValidDocumentInformation

Public Shared Sub InvalidDocumentInformation()

If clsError.ErrorFlag() = True Then Exit Sub

Call System.Windows.Forms.MessageBox.Show( _
"This document has not been created with the correct template." & _
gsCRLF & _
"The " & gsSOLUTION_NAME_WORD & " will only work with the official template.", _
gsSOLUTION_NAME_WORD & " - Invalid Template", _
Windows.Forms.MessageBoxButtons.OK, _
Windows.Forms.MessageBoxIcon.Information)

clsError.ErrorFlagChange(True)
End Sub
'***********************************************************************************

InvalidTemplateInformation

Public Shared Sub InvalidTemplateInformation(ByVal sTemplateName As String, _
Optional ByVal bFlagError As Boolean = True)

If clsError.ErrorFlag() = True Then Exit Sub

Call System.Windows.Forms.MessageBox.Show( _
"The template '" & sTemplateName & "' is not an official template." & _
gsCRLF & _
"Please select a valid template.", _
gsSOLUTION_NAME_WORD, _
Windows.Forms.MessageBoxButtons.OK, _
Windows.Forms.MessageBoxIcon.Information)

clsError.ErrorFlagChange(bFlagError)
End Sub
'***********************************************************************************

InvalidTemplateTypeInformation

Public Shared Sub InvalidTemplateTypeInformation(ByVal sTemplateName As String, _
Optional ByVal bFlagError As Boolean = True)

If clsError.ErrorFlag() = True Then Exit Sub

Call System.Windows.Forms.MessageBox.Show( _
"The template type '" & sTemplateName & "' is not valid." & _
gsCRLF & _
"The " & gsSOLUTION_NAME_WORD & " will only work with an official template.", _
gsSOLUTION_NAME_WORD, _
Windows.Forms.MessageBoxButtons.OK, _
Windows.Forms.MessageBoxIcon.Information)

clsError.ErrorFlagChange(bFlagError)
End Sub
'***********************************************************************************

NormalTemplateDoesNotExistInformation

Public Shared Sub NormalTemplateDoesNotExistInformation(Optional ByVal bFlagError As Boolean = False)

If clsError.ErrorFlag() = True Then Exit Sub

Call System.Windows.Forms.MessageBox.Show( _
"You must close Word and reopen it." & _
gsCRLF & _
"If prompted to save the changes, press Yes." & _
gsCRLF & _
gsCRLF & _
"The global template, Normal.dot does not exist." & _
gsCRLF & _
"A new one will be created for you automatically after you close Word.", _
gsMESSAGEBOX_TITLE_WORD, _
Windows.Forms.MessageBoxButtons.OK, _
Windows.Forms.MessageBoxIcon.Information)

clsError.ErrorFlagChange(bFlagError)
End Sub
'***********************************************************************************

NormalTemplateSaveChangesBeforeQuestion

Public Shared Function NormalTemplateSaveChangesBeforeQuestion() As Boolean

Dim breturn As Boolean
Dim objreturn As Windows.Forms.DialogResult

If clsError.ErrorFlag() = True Then Exit Function

objreturn = System.Windows.Forms.MessageBox.Show( _
"Some changes have been made to the global template, Normal.dot." & _
gsCRLF & _
"Do you want to save these changes ?", _
gsMESSAGEBOX_TITLE_WORD, _
Windows.Forms.MessageBoxButtons.YesNo, _
Windows.Forms.MessageBoxIcon.Question)

If objreturn = Windows.Forms.DialogResult.Yes Then breturn = True
If objreturn = Windows.Forms.DialogResult.No Then breturn = False

NormalTemplateSaveChangesBeforeQuestion = breturn
End Function
'***********************************************************************************

NotInstalled

Public Sub Templates_GlobalNotInstalled(ByVal sFileName As String)

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

For itemplatecount = 1 To gApplicationWord.AddIns.Count
If (gApplicationWord.AddIns.Item(itemplatecount).Name = sFileName) Then

gApplicationWord.AddIns.Item(itemplatecount).Installed = False
End If
Next itemplatecount

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

Remove

source code
Public Sub Templates_GlobalRemove(ByVal sFileName As String, _
ByVal bInformUser As Boolean)

Dim objAddin As Word.AddIn
Dim itemplatecount As Integer

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

For itemplatecount = gApplicationWord.AddIns.Count To 1 Step -1
If (gApplicationWord.AddIns.Item(itemplatecount).Name = sFileName) Then
objAddin = gApplicationWord.AddIns.Item(itemplatecount)
objAddin.Delete()
End If
Next itemplatecount

Catch ex As Exception
If (bInformUser = True) Then
Call modMessages.ErrorMessage(System.Reflection.MethodBase.GetCurrentMethod, ex.Message, _
"", False)
Else
Call Tracer_Add("Templates_GlobalRemove", "TEMPLATES", ex.Message)
End If
End Try
End Sub
'************************************************************************************

Remove

source code
source code

ReturnPath


Save

Public Sub Templates_NormalSave()
Dim objtemplate As Word.Template
Dim itemplatecount As System.Int32

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

For itemplatecount = 1 To gApplicationWord.Templates.Count
If gApplicationWord.Templates(itemplatecount).Name.StartsWith("Normal") = True Then
objtemplate = gApplicationWord.Templates(itemplatecount)
objtemplate.Save()
Exit Sub
End If
Next itemplatecount

Call modMessages.Templates_NormalDoesNotExistInformation()

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

SavedGet

Public Shared Function NormalSavedGet() As Boolean
Try
If clsError.ErrorFlag() = True Then Exit Function

Dim objtemplate As Word.Template
Dim itemplatecount As System.Int32

For itemplatecount = 1 To gApplicationWord.Templates.Count
If gApplicationWord.Templates(itemplatecount).Name.StartsWith("Normal") = True Then
objtemplate = gApplicationWord.Templates(itemplatecount)

NormalSavedGet = objtemplate.Saved
Exit Function
End If
Next itemplatecount

Call clszMessagesWord.NormalTemplateDoesNotExistInformation()

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("NormalSavedGet", msCLASSNAME, _
"determine if the global template Normal.dot needs to be saved.", _
mobjCOMException, mobjException)
End If
End Try
End Function
'********************************************************************************

SavedSet

Public Shared Sub NormalSavedSet(ByVal bTrueOrFalse As Boolean)

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

Dim objtemplate As Word.Template
Dim itemplatecount As System.Int32

For itemplatecount = 1 To gApplicationWord.Templates.Count
If gApplicationWord.Templates(itemplatecount).Name.StartsWith("Normal") = True Then
objtemplate = gApplicationWord.Templates(itemplatecount)

objtemplate.Saved = bTrueOrFalse
Exit Sub
End If
Next itemplatecount

Call clszMessagesWord.NormalTemplateDoesNotExistInformation()

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("NormalSavedSet", msCLASSNAME, _
"change the saved property of the global template, Normal.dot.", _
mobjCOMException, mobjException)
End If
End Try
End Sub
'********************************************************************************

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