locked
Excel Add-in creating a lot of .tmp files RRS feed

  • Question

  • I am developing an Excel 2010 Add-in with MS Visual Studio 2015 in C#.  My app sorts through data in one workbook and divides it up into several worksheets in another workbook.  the application runs I get a lot of .tmp files showing up in the directory I am saving the files to.  I have looked around and haven't seen anyone complaining about this so I am wondering if I am doing something wrong.  The only thing I can find regarding the .tmp files is that they are typically created when when an office application is saving and the save doesn't execute correctly.  I replaced all my Workbook.Save() commands with

       try
                        {
                            Workbook.Save();
                        }
                        catch(Exception ex)
                        {
                            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK);
                        }

    But no exceptions are being thrown.   I am getting a lot of

    Managed Debugging Assistant 'DisconnectedContext' has detected a problem in 'C:\Program Files (x86)\Microsoft Office\Office14\excel.exe'.

    Additional information: Transition into COM context 0x7665b0 for this RuntimeCallableWrapper failed with the following error: System call failed. (Exception from HRESULT: 0x80010100 (RPC_E_SYS_CALL_FAILED)). This is typically because the COM context 0x7665b0 where this RuntimeCallableWrapper was created has been disconnected or it is busy doing something else. Releasing the interfaces from the current COM context (COM context 0x766720). This may cause corruption or data loss. To avoid this problem, please ensure that all COM contexts/apartments/threads stay alive and are available for context transition, until the application is completely done with the RuntimeCallableWrappers that represents COM components that live inside them.

    Errors which break my debugger, but when I look around I can't find any fixes for this issue either.  Not sure if this is linked to the .tmp files or not. Any other suggestions on what I can do to prevent this? Thanks in advance.

    Friday, June 24, 2016 6:05 PM

Answers

  • I have run the add-in on two other computers and the .tmp files are not being created. I'm not sure what is causing the issue, but for now I've added a clean up routine to the end of the application.  For now I am moving on, if it becomes a problem on other machines I will revisit the issue, thanks for your help.
    Tuesday, June 28, 2016 1:30 PM

All replies

  • >>>the application runs I get a lot of .tmp files showing up in the directory I am saving the files to.

    According to your description, when you save an existing file in Excel, Excel creates a temporary file in the destination folder that you specify in the Save As dialog box. The temporary file contains the whole contents of your workbook. If Excel successfully saves the temporary file, the temporary file is renamed with the file name you specify in the Save As dialog box. I think that this issue is related to your Excel Add-in, so I suggest that you could save as Workbook to check whether this issue can be resolved or debug your Excel Add-in.

    For more information, click here to refer about Description of the way that Excel saves files

    and here to refer about How to troubleshoot errors when you save Excel workbooks

    In addition could you provide more sample codes, that will help us reproduce and resolve this issue.

    Thanks for your understanding.

    Monday, June 27, 2016 8:33 AM
  • I have created a very simple project that causes the error to occur several times when I run it on my computer that doesn't have any proprietary information in it.  I created a new Microsoft Excel 2010 VSTO Add-in project, Added a ribbon (Visual designer) Item to it, dropped a button1 on the ribbon, and wrote this code.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    using Microsoft.Office.Tools.Ribbon;
    using Excel = Microsoft.Office.Interop.Excel;
    using Microsoft.Office.Tools.Excel;
    
    namespace TmpFileCreator
    {
        public partial class Ribbon1
        {
            Excel.Application ThisApp;
            private void Ribbon1_Load(object sender, RibbonUIEventArgs e)
            {
    
            }
    
            private void button1_Click(object sender, RibbonControlEventArgs e)
            {
                SaveFileDialog ThisSave = new SaveFileDialog();
                if(!Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\Temp"))
                {
                    Directory.CreateDirectory(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\Temp");
                }
                ThisSave.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\Temp";
                ThisSave.Filter = "Excel files 97-2003 (*.xls)|*.xls|Excel files (*.xlsx)|*.xlsx";
                ThisSave.FilterIndex = 2;
                ThisSave.FileName = "RawData.xlsx";
                ThisSave.ShowDialog();
                ThisApp = (Excel.Application)Marshal.GetActiveObject("Excel.Application");
                Excel.Workbook ThisBook = ThisApp.ActiveWorkbook;
                Excel.Worksheet ThisSheet = ThisApp.ActiveSheet;
                ThisBook.SaveAs(ThisSave.FileName);
                for (int x = 1; x < 10000; x++)
                {
                    ThisSheet.Range["A" + x.ToString()].Value = x.ToString();
                    if(x%200 == 0)
                    {
                        try
                        {
                            ThisBook.Save();
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK);
                        }
                    }
                }
                
            }
        }
    }
    When I execute this code I get typically 8 or more .tmp files in the folder.  The basic principle of this code matches what I am trying to do in my code, I am trying to periodically save my work as I am importing data so I don't lose all my work during very long imports.

    Monday, June 27, 2016 8:24 PM
  • >>>When I execute this code I get typically 8 or more .tmp files in the folder.

    According to your description, I have made a sample with your sample codes to try to reproduce this issue, unfortunately, I am not able. You could refer to below result:

    I suggest that you could run this project on an other machine to make sure this issue can be reproduced.

    In addition when you create an Office project, Visual Studio automatically generates a class named Globals in the project. You can use the Globals class to access several different project items at run time from any code in the project.

    Excel.Workbook ThisBook = Globals.ThisAddIn.Application.ActiveWorkbook;


    For more information, click here to refer about Global Access to Objects in Office Projects

    Tuesday, June 28, 2016 5:44 AM
  • I have run the add-in on two other computers and the .tmp files are not being created. I'm not sure what is causing the issue, but for now I've added a clean up routine to the end of the application.  For now I am moving on, if it becomes a problem on other machines I will revisit the issue, thanks for your help.
    Tuesday, June 28, 2016 1:30 PM
  • Hi D_T_M,

    I am glad to hear that you have some progress. If you have any question, please feel free post them on MSDN forum.

    In addition I suggest that you could check whether other Add-ins have installed or repair Excel.
    Wednesday, June 29, 2016 2:04 AM