2

(I'm week in English language, so at first excuse me for bad explaining :D )

I open an excel file through my application.

I have an Addd-In in Excel and a button in ribbon for save (exactly such a save action that Save button do) code of Click event of button is here:

Globals.ThisAddIn.Application.ActiveWorkbook.Save();

In my application I assign a method (called WorkbookBeforeSave) to "BeforeSave" event handler of workbook that save workbook manually in my custom directory.

private void WorkbookBeforeSave(bool saveasui, ref bool cancel)
{
    _excelApp.EnableEvents = false;//_excelApp is my Excel Application

    if (!_excelWorkbook.Saved)//_excelWorkbook is Active Excel Workbook
    {
        _excelWorkbook.SaveCopyAs(_savedFilePath);//_savedFilePath is my custom directory

        _excelWorkbook.Saved = true;
    }

    cancel = true;

    _excelApp.EnableEvents = true;
}

problem is when I click Original Excel Save Button "SaveCopyAs" method works correctly but when click on my custom Save Button "SaveCopyAs" method does not work. (no exception has thrown and all of codes compiled and debugged)

animuson
  • 53,861
  • 28
  • 137
  • 147
Omid.Hanjani
  • 1,444
  • 2
  • 20
  • 29
  • Don't use excel interop. it's very poor. – gdoron Feb 12 '12 at 08:03
  • 1
    `_excelApp.EnableEvents = false;` looks suspicious. – gdoron Feb 12 '12 at 08:04
  • I'm agree with you, excel interop is very poor. But I have to use. What are other available choices? – Omid.Hanjani Feb 12 '12 at 09:25
  • 1
    "_excelApp.EnableEvents = false;" is for prevent infinitive loop at save action and BeforeSave event – Omid.Hanjani Feb 12 '12 at 09:28
  • The most common reason to not get an exception is that the function actually did what you asked it to do. You just can't find the file back. Which happens when you don't specify the full path name of the file. – Hans Passant Feb 12 '12 at 09:40
  • thanks. I toggle a break point and check _savedFilePath before calling SaveCopyAs method. _savedFilePath is correct and it's the same thing when I press my save button or excel save button. – Omid.Hanjani Feb 12 '12 at 10:06
  • I think problem is related to calling Save manually from Add-In and raise Excel event in my application during to working with Add-In. – Omid.Hanjani Feb 12 '12 at 10:07
  • I don't really know much about C#... but from your problem description, `if (!_excelWorkbook.Saved)` is not doing what you think it should. Maybe try removing it and see if an error is thrown in that case. – Pynner Feb 13 '12 at 01:26

1 Answers1

0

Try to debug with try-catch, it should be helpful for you.

// using interop, excel tool, interop-excel, core

public void MyBeforeSave(Microsoft.Office.Interop.Excel.Workbook Wb, bool SaveAsUI, ref bool Cancel)
{
  //Globals.ThisAddIn.Application.ActiveWorkbook.SaveCopyAs(filename2); // if u use an external class to save (for threading or something else )
  this.Application.ActiveWorkbook.SaveCopyAs(filename); 

}   

Without threading/tasking the office 2007 excel - sometime - lagging or slower than normal at loading and at the save method.

The simple saveAs() method have lot of parameters (searh for it, you see it on msdn & here too) and its needs some hoak at the path, because the latest save-path change to the C# used save path.

I prefer the SaveCopyAs solution, because just one parameter and fast & furious. :D

Steve Czetty
  • 6,147
  • 9
  • 39
  • 48
huncyrus
  • 648
  • 8
  • 18