question

ApsaraAbeysiriwardana-3413 avatar image
0 Votes"
ApsaraAbeysiriwardana-3413 asked ApsaraAbeysiriwardana-3413 edited

Add .dotm files to Word.AddIns programatically

I am working on a Word custom ribbon bar add-in and currently developing a function to add .dotm files to Word.AddIns programmatically, a user friendly sub version of "Developer > Document Template > Template tab" functionality in Word.

What I am trying to achieve is as follows :

Implementing a dialog which enables users to add new .dotm files & attach/detach existing .dotm files. The added .dotm files are stored in a specific folder location and all added files are displaying in a checked list view. When user select to attach a template, if it is not existing in the Word Startup folder, add it and attach it to Word.AddIns.

I am stuck in attaching the .dotm file to Word.AddIns.

Below is my code for attach/detach templates.

 private void UpdateCustomAddIns()
 {
     try
     {
         string startupFolderPath = this.wordApp.StartupPath;
         Word.AddIn addIn = null;
         addInPath = "";
         addInName = "";
         addInFilePath = "";
    
         for (int i = 0; i < _view.AddInsListView.Items.Count; i++)
         {
             bool status = _view.AddInsListView.Items[i].Checked;
             addInName = _view.AddInsListView.Items[i].Name;
    
             if (status)
             {                       
                 addInFilePath = constants.UserDocumentsFolderName + @"\Custom add-ins\" + addInName;
                 addInPath = startupFolderPath + @"\" + addInName;
    
                 try
                 {
                     //throw error when template is already existing in the startup folder.                          
                     File.Copy(addInFilePath, addInPath, true);
                     addIn = GetWordAddInByTemplateName(addInName);
                     if (addIn != null)
                     {
                         addIn.Installed = true;
                     }
                     else
                     {   
                         //I have the issue in this step.
                         this.wordApp.AddIns.Add(addInName, true);                                                           
                     }
                 }
                 catch
                 {
                     // Select the correct add-in and detach it from word, then replace the template file in Word startup. 
                     addIn = GetWordAddInByTemplateName(addInName);
                     if (addIn != null)
                     {
                         addIn.Installed = false;
                         File.Copy(addInFilePath, addInPath, true);
                         addIn.Installed = true;
                     }
                 }
             }
             else
             {
                 addIn = GetWordAddInByTemplateName(addInName);
                 if (addIn != null)
                 {
                     if (addIn.Installed) 
                     {
                         addIn.Installed = false;
                         addInPath = startupFolderPath + @"\" + addInName;
                         File.Delete(addInPath);
                     }                           
                 }
             }
         }
     }
     catch(Exception ex)
     {
         MessageBox.Show(ex.Message + Environment.NewLine + "DotmManagerPresenter.UpdateCustomAddIns", "My app",
                 MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
 }

 private Word.AddIn GetWordAddInByTemplateName(string templateName)
 {
     int count = this.wordApp.AddIns.Count;
     for (int index = 1; index < count + 1; index++)
     {
         Word.AddIn addIn = this.wordApp.AddIns[index];
         if (addIn.Name == templateName)
         {
             return addIn;
         }
     }
     return null;
 }

I am having the issue in this line this.wordApp.AddIns.Add(addInName, true);

This step meant to attach the selected .dotm file to Word.AddIns. But after I run this line and when check the 'Developer > Document Template' dialog in Word , two instances of the just added template are visible in there with the same path.

One occurrence is checked, but it seems not the correct instances, the 'Remove' button is only enabled for the checked instance. This just added template is not identified as an 'attached' template by the code.
If I close Word and reopen Word, then that second instance is gone.
131963-jqkiz.png

My problem is ,
How should I fix my code to avoid these two instances of the same .dotm file? I tried to find what is this second occurrence, it is not a hidden file, no idea.
Without closing Word and re-open it, I want to update my add-ins listview with real-time details of the attached templates.

I implemented this functionality and when test it again after few days only I noticed this problem. I am not sure, but as I remember it worked before.

My Word version : Word 365 64 bit, Vesion 2102(Build 13801.20864)

Thank you in advance !








office-addins-devoffice-vsto-com-dev
jqkiz.png (41.2 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

0 Answers