C# Snippets


Add

public Word.AddIn Templates_GlobalAdd(string sFileName)
{
string smessage;

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

if (modGeneral.File_Exists(sFileName, true) == true)
return gApplicationWord.AddIns.Add(sFileName, true);
else
return null/* TODO Change to default(_) if this is not a reference type */;
}
catch (Exception ex)
{
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;

modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod(), null/* TODO Change to default(_) if this is not a reference type */, ex, smessage, true, null/* Conversion error: Set to default value for this argument */, null/* Conversion error: Set to default value for this argument */, null/* Conversion error: Set to default value for this argument */, System.Windows.Forms.MessageBoxIcon.Warning);

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

Exists

public bool Templates_GlobalExists(string sFileName, bool bInformUser = true)
{
System.Int32 itemplatecount;
bool bfound = false;
string AddinToCheck = "";

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

for (itemplatecount = 1; itemplatecount <= gApplicationWord.AddIns.Count; itemplatecount++)
{
try
{
AddinToCheck = "";
AddinToCheck = gApplicationWord.AddIns.Item(itemplatecount).Path + @"\" + gApplicationWord.AddIns.Item(itemplatecount).Name;
}
catch (Exception ex)
{
modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod(), null/* TODO Change to default(_) if this is not a reference type */, ex, "Error while attempting to check existence of template (" + sFileName + ")", bInformUser, false);
}

modGeneral.Tracer_Add2("WORD", "Checked add-in " + itemplatecount.ToString() + " (" + AddinToCheck + ")", false);

if (AddinToCheck == sFileName)
bfound = true;
}

if ((bfound == true))
{
if (bInformUser == true)
modMessages.Exception_Warning(System.Reflection.MethodBase.GetCurrentMethod(), sFileName + " has already been installed");
}
}
catch (Exception ex)
{
modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod(), null/* TODO Change to default(_) if this is not a reference type */, ex, "Error while attempting to check existence of template (" + sFileName + ")", null/* Conversion error: Set to default value for this argument */, false);
}
return bfound;
}
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 bool Templates_NormalExists(bool bInformUser = true)
{
Word.Template objtemplate;
System.Int32 itemplatecount;
bool bfound = false;

try
{
if (My.Settings.ERROR_OCCURRED == true)
return;

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

for (itemplatecount = 1; itemplatecount <= gApplicationWord.Templates.Count; itemplatecount++)
{
if (gApplicationWord.Templates(itemplatecount).Name.StartsWith("Normal") == true)
{
objtemplate = gApplicationWord.Templates(itemplatecount);
bfound = true;
break;
}
}

if ((bfound == false))
{
if (bInformUser == true)
modMessages.Templates_NormalDoesNotExistInformation();
}
}
catch (Exception ex)
{
modMessages.ErrorMessage(System.Reflection.MethodBase.GetCurrentMethod(), ex.Message, "determine if the global template, Normal.dot exists.");
}
}

public static bool NormalTemplateExists()
{
try
{
string sfolderpath;
sfolderpath = gApplicationWord.Templates("Normal.dot").Path;

NormalTemplateExists = true;
}
catch (Runtime.InteropServices.COMException objCOMException)
{
NormalTemplateExists = false;
}
catch (Exception objException)
{
NormalTemplateExists = false;
}
}
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

Installed

public bool Templates_GlobalInstalled(string sFileName)
{
int itemplatecount;

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

for (itemplatecount = 1; itemplatecount <= gApplicationWord.AddIns.Count; itemplatecount++)
{
if ((gApplicationWord.AddIns.Item(itemplatecount).Name == sFileName))
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);
}
}
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

Message_InValidDocumentInformation

public static void InvalidDocumentInformation()
{
if (clsError.ErrorFlag() == true)
return;

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);
}
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

Message_InvalidTemplateInformation

public static void InvalidTemplateInformation(string sTemplateName, bool bFlagError = true)
{
if (clsError.ErrorFlag() == true)
return;

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);
}
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

Message_InvalidTemplateTypeInformation

public static void InvalidTemplateTypeInformation(string sTemplateName, bool bFlagError = true)
{
if (clsError.ErrorFlag() == true)
return;

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);
}
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

Message_NormalTemplateDoesNotExistInformation

public static void NormalTemplateDoesNotExistInformation(bool bFlagError = false)
{
if (clsError.ErrorFlag() == true)
return;

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);
}
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

Message_NormalTemplateSaveChangesBeforeQuestion

public static bool NormalTemplateSaveChangesBeforeQuestion()
{
bool breturn;
Windows.Forms.DialogResult objreturn;

if (clsError.ErrorFlag() == true)
return;

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)
breturn = true;
if (objreturn == Windows.Forms.DialogResult.No)
breturn = false;

NormalTemplateSaveChangesBeforeQuestion = breturn;
}
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 void Templates_GlobalNotInstalled(string sFileName)
{
int itemplatecount;
try
{
Tracer_Add2("SUBROUTINE", System.Reflection.MethodBase.GetCurrentMethod().Name + " start");
if (My.Settings.ERROR_OCCURRED == true)
return;

for (itemplatecount = 1; itemplatecount <= gApplicationWord.AddIns.Count; itemplatecount++)
{
if ((gApplicationWord.AddIns.Item(itemplatecount).Name == sFileName))
gApplicationWord.AddIns.Item(itemplatecount).Installed = false;
}
}
catch (Exception ex)
{
modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod(), null/* TODO Change to default(_) if this is not a reference type */, ex);
}
}
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

public void Templates_GlobalRemove(string sFileName, bool bInformUser)
{
Word.AddIn objAddin;
int itemplatecount;

try
{
if (My.Settings.ERROR_OCCURRED == true)
return;

for (itemplatecount = gApplicationWord.AddIns.Count; itemplatecount >= 1; itemplatecount += -1)
{
if ((gApplicationWord.AddIns.Item(itemplatecount).Name == sFileName))
{
objAddin = gApplicationWord.AddIns.Item(itemplatecount);
objAddin.Delete();
}
}
}
catch (Exception ex)
{
if ((bInformUser == true))
modMessages.ErrorMessage(System.Reflection.MethodBase.GetCurrentMethod(), ex.Message, "", false);
else
Tracer_Add("Templates_GlobalRemove", "TEMPLATES", ex.Message);
}
}
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

Save

public void Templates_NormalSave()
{
Word.Template objtemplate;
System.Int32 itemplatecount;

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

for (itemplatecount = 1; itemplatecount <= gApplicationWord.Templates.Count; itemplatecount++)
{
if (gApplicationWord.Templates(itemplatecount).Name.StartsWith("Normal") == true)
{
objtemplate = gApplicationWord.Templates(itemplatecount);
objtemplate.Save();
return;
}
}

modMessages.Templates_NormalDoesNotExistInformation();
}
catch (Exception ex)
{
modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod(), null/* TODO Change to default(_) if this is not a reference type */, ex);
}
}
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 static bool NormalSavedGet()
{
try
{
if (clsError.ErrorFlag() == true)
return;

Word.Template objtemplate;
System.Int32 itemplatecount;

for (itemplatecount = 1; itemplatecount <= gApplicationWord.Templates.Count; itemplatecount++)
{
if (gApplicationWord.Templates(itemplatecount).Name.StartsWith("Normal") == true)
{
objtemplate = gApplicationWord.Templates(itemplatecount);

NormalSavedGet = objtemplate.Saved;
return;
}
}

clszMessagesWord.NormalTemplateDoesNotExistInformation();
}
catch (Runtime.InteropServices.COMException objCOMException)
{
mobjCOMException = objCOMException;
}
catch (Exception objException)
{
mobjException = objException;
}

finally
{
if (gbDEBUG_WORD == true | ((!mobjCOMException == null) | (!mobjException == null)))
clsError.Handle("NormalSavedGet", msCLASSNAME, "determine if the global template Normal.dot needs to be saved.", mobjCOMException, mobjException);
}
}
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 static void NormalSavedSet(bool bTrueOrFalse)
{
try
{
if (clsError.ErrorFlag() == true)
return;

Word.Template objtemplate;
System.Int32 itemplatecount;

for (itemplatecount = 1; itemplatecount <= gApplicationWord.Templates.Count; itemplatecount++)
{
if (gApplicationWord.Templates(itemplatecount).Name.StartsWith("Normal") == true)
{
objtemplate = gApplicationWord.Templates(itemplatecount);

objtemplate.Saved = bTrueOrFalse;
return;
}
}

clszMessagesWord.NormalTemplateDoesNotExistInformation();
}
catch (Runtime.InteropServices.COMException objCOMException)
{
mobjCOMException = objCOMException;
}
catch (Exception objException)
{
mobjException = objException;
}

finally
{
if (gbDEBUG_WORD == true | ((!mobjCOMException == null) | (!mobjException == null)))
clsError.Handle("NormalSavedSet", msCLASSNAME, "change the saved property of the global template, Normal.dot.", mobjCOMException, mobjException);
}
}
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

Template_AttachCorrectTemplate

public bool AttachCorrectActiveDocTemplate(ref Word.Document TargetDocument)
{
TemplateType TemplateType = GetTemplateType(TargetDocument);

string strTemplatePath;

switch (TemplateType)
{
case object _ when ClientApp.TemplateType.1:
{
strTemplatePath = getTemplateLocation("Template_1.dot");
break;
}

case object _ when ClientApp.TemplateType.2:
{
strTemplatePath = getTemplateLocation("Template_2.dot");
break;
}

case object _ when ClientApp.TemplateType.3:
{
strTemplatePath = getTemplateLocation("Template_3.dot");
break;
}

case object _ when ClientApp.TemplateType.4:
{
strTemplatePath = getTemplateLocation("Template_4.dot");
break;
}

default:
{
strTemplatePath = getTemplateLocation("Template_5.dot");
break;
}
}

try
{
Word.Template attachedTemplate = (Word.Template)TargetDocument.AttachedTemplate;

if (attachedTemplate.FullName.ToUpperInvariant == strTemplatePath.ToUpperInvariant())
return true;
else
try
{
TargetDocument.AttachedTemplate = strTemplatePath;

return true;
}
catch (Exception ex)
{
return false;
}
}
catch (Exception ex)
{
modMessages.ErrorMessage(System.Reflection.MethodBase.GetCurrentMethod(), null/* TODO Change to default(_) if this is not a reference type */, ex, "check / change the attached template", false, false);
return false;
}
}
Public Function AttachCorrectActiveDocTemplate(ByRef TargetDocument As Word.Document) As Boolean

Dim TemplateType As TemplateType = GetTemplateType(TargetDocument)

Dim strTemplatePath As String

Select Case TemplateType

Case ClientApp.TemplateType.1

strTemplatePath = getTemplateLocation("Template_1.dot")

Case ClientApp.TemplateType.2

strTemplatePath = getTemplateLocation("Template_2.dot")

Case ClientApp.TemplateType.3

strTemplatePath = getTemplateLocation("Template_3.dot")

Case ClientApp.TemplateType.4

strTemplatePath = getTemplateLocation("Template_4.dot")

Case Else

strTemplatePath = getTemplateLocation("Template_5.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

Template_AttachedTemplateGet

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

AttachedTemplateGet = (Word.Template)gApplicationWord.ActiveDocument.AttachedTemplate.Name;
}
catch (Runtime.InteropServices.COMException objCOMException)
{
mobjCOMException = objCOMException;
}
catch (Exception objException)
{
mobjException = objException;
}

finally
{
if (gbDEBUG_WORD == true | ((!mobjCOMException == null) | (!mobjException == null)))
clsError.Handle("AttachedTemplateGet", msCLASSNAME, "return the name of the template attached to the active document.", mobjCOMException, mobjException);
}
}
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

Template_Available

public bool Template_Available(Word.Template objTemplate, bool bInformUser)
{
bool bavailable;
string stemplatename;

try
{
Tracer_Add2("SUBROUTINE", System.Reflection.MethodBase.GetCurrentMethod().Name + " start");
bavailable = objTemplate.Saved;
return true;
}
catch (Exception ex)
{
if ((bInformUser == true))
stemplatename = gSettingsFile.UserProfileFolder + My.Settings.APP_FILENAME_USERPROFILE_CUSTOMISATION;
return false;
}
}
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

Template_Exists

public bool AttachedTemplate_Exists(ref Word.Document objDocument, bool bInformUser = false)
{
Word.Template objTemplate = null/* TODO Change to default(_) if this is not a reference type */;

try
{
if (My.Settings.ERROR_OCCURRED == true)
return;

objTemplate = (Word.Template)objDocument.AttachedTemplate;
return true;
}
catch (Exception ex)
{
if ((bInformUser == true))
modMessages.Template_AttachedInvalid();
return false;
}
}
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

Template_Remove

source code

Template_ReturnPath


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