Why dictionary count returns 0 in th word addin?

Category: visual studio officetools

Question

DilanS on Wed, 11 Jul 2018 10:08:52


Hi,

I am passing some values from outside the word Addin and save it to the dictionary for later use when the user wants to save the word document.

Below is the code

[ComVisible(true)]
    [Guid("229CCF26-CECF-4708-8F44-561FB1655641")]
    [ClassInterface(ClassInterfaceType.None)]
    public class AddinUtilities : StandardOleMarshalObject , IAddinUtilities
    {

         
        private int clipCount = 0;
        private int maxClipCount = 0;
 
       
        public void SetClipData(Temp dataManager)
        {
            addClipElements(dataManager);
            
        }

        private void addClipElements(Temp dataManager)
        {
            Temp.Clip.Add(clipCount++,dataManager);
        }
 
        public void ClearClipData()
        {
            Temp.Clip.Clear();
        }

        public void SetClipCount(int count)
        {
            maxClipCount = count;
        }
    }

below is the temp class

  [Serializable]
    public class Temp
    {
        private string copiedText;

        [DisplayName("CopiedText")]
        public string CopiedText
        {
            get
            {
                return copiedText;
            }
            set
            {
                copiedText = value;
            }
        }

        private string time;

        [DisplayName("Time")]
        public string Time
        {
            get
            {
                return time;
            }
            set
            {
                time = value;
            }
        }

        public Temp( string _copiedText, string _copiedTime)
        {
            this.copiedText = _copiedText;
            this.time = DateTime.Now.ToString("T");
        }

        private static Dictionary<int, Temp> clip = new Dictionary<int, Temp>();

        public static Dictionary<int, Temp> Clip
        {
            get
            {
                return clip;
            }
            set
            {
                clip = value;
            }
        }

        public Dictionary<int, Temp> ReturnData()
        {
            return Clip;
        }
    }

This is how I pass data from outside the word Addin

word = new Microsoft.Office.Interop.Word.Application();
object addinName = "WordAddInClip";
COMAddIns comAddins = word.COMAddIns;

COMAddIn comAddin = comAddins.Item(addinName);
WordAddInClip.IAddinUtilities comObj = (WordAddInClip.IAddinUtilities)comAddin.Object;


while (comObj == null)
    {
       comObj = (WordAddInClip.IAddinUtilities)comAddin.Object;
       System.Threading.Thread.Sleep(1000);
    }


AddinDataMgr = new WordAddInClip.Temp(dataMgr.CopiedText, dataMgr.Time);
comObj.SetClipData(AddinDataMgr);

It adds data correctly because it increments the counter ow the Dictionary.

But when I call the Dictionary inside ThisAddin.cs class like below it shows the count as 0

void Application_DocumentBeforeSave(Word.Document Doc, ref bool SaveAsUI, ref bool Cancel)
{

MessageBox.Show("clip count " + Temp.Clip.Count.ToString());

}

What could be the issue?

Replies

Eugene Astafiev on Wed, 11 Jul 2018 23:36:50


Hello Dilan,

Try to pass a simple variable like integer instead of custom classes. Does it work correctly in that case?

Terry Xu - MSFT on Thu, 12 Jul 2018 07:47:06


Hello DilanS,

In my test, I did return the clip count. You shared code is not enough for reproducing your issue.

I would suggest you create a simple project and share it for us to reproduce your issue. Or you could try to check if my test project could work for you.

My Project is here. https://1drv.ms/u/s!ArC0gnwxLv5qhyeoky_lkDyh8Njr

Best Regards,

Terry

DilanS on Thu, 12 Jul 2018 11:30:10


Hi,

I tried it with a string value. But when I call it inside the Application_DocumentBeforeSave event it says the value is null. But the string value is passing correctly. So as explained in a different thread "COMaddins are implemented as DLLs loaded by each host process. No classes are ever shared between two running processes. You need to come up with a different storage mechanism (registry? file system?) to share common data." I have to find a different solution to save my data inside the <g class="gr_ gr_30 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" data-gr-id="30" id="30">wordaddin</g>. For VSTO word <g class="gr_ gr_128 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling" data-gr-id="128" id="128">addins</g>, is there any other way that I can save my data without using a database file?

Terry Xu - MSFT on Fri, 13 Jul 2018 01:53:30


Hello Dilans,

Have you tried to my test project? Could it work for you? Could you simple your project and share it to us for testing?

Best Regards, 

Terry

DilanS on Thu, 19 Jul 2018 09:33:36


The problem got solved by adding below lines

COMAddIns comAddins = excel.COMAddIns;
COMAddIn comAddin = comAddins.Item(addinName);

Terry Xu - MSFT on Thu, 19 Jul 2018 09:43:40


Hello DilanS,

To be honesty, I fail to understand why the code could resolve your issue. You previous code are all related to Word add-ins but the code seems be related to Excel? Maybe there is other design we do not know.

Anyway, glad to hear that your issue has been resolved. I would suggest you mark your solution which is the correct way to close the thread.

If you have any other issue, please feel free to post threads to let us know.

Best Regards,

Terry