"Save As" for Protected View document throwing e

Add-in Express™ Support Service
That's what is more important than anything else

"Save As" for Protected View document throwing e
System.Runtime.InteropServices.COMException : Save As method for Protected View workbooks failed 
Jacob John




Posts: 20
Joined: 2013-08-29
I'm using Excel 2013 and Add-in express .NET 7.2.4055.0 .When trying to export a document from website,it is opening in Protected View. After Clicking "Enable Editing",we call,

MSObject.GetType().InvokeMember("SaveAs", BindingFlags.InvokeMethod, null, MSObject, new object[] { fileName });

but it fails with following error.

System.Reflection.TargetInvocationException was caught
HResult=-2146232828
Message=Exception has been thrown by the target of an invocation.

StackTrace:
at System.RuntimeType.InvokeDispMethod(String name, BindingFlags invokeAttr, Object target, Object[] args, Boolean[] byrefModifiers, Int32 culture, String[] namedParameters)
at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams)
at System.Type.InvokeMember(String name, BindingFlags invokeAttr, Binder binder, Object target, Object[] args)
at CustomAddIn.SaveAs(String fileName)

InnerException: System.Runtime.InteropServices.COMException
HelpLink=xlmain11.chm
HResult=-2146827284
Message=SaveAs method of Workbook class failed
Source=Microsoft Excel
ErrorCode=-2146827284

Any clue about this error? what's the possible cause of this error?
Any help will be much appreciated.
Posted 17 Jul, 2014 10:22:41 Top
Andrei Smolin


Add-in Express team


Posts: 14137
Joined: 2006-05-11
Hello Jacob,

-2146827284 stands for 0x800a03ec. This error can be caused by a lot of reasons:
- you have no permissions
- the target file already exists
- the target path/file name is incorrect
- you save the workbook in an incorrect format

You can try to save the file manually. Does this produce the same problem? Does the issue occur if you save to e.g. your profile?

Try to specify the FileFormat parameter, see http://msdn.microsoft.com/en-us/library/office/ff841185%28v=office.15%29.aspx. Make sure that the workbook you save fits the row/column restriction on the excel book file format that you use. See also http://www.rondebruin.nl/win/s5/win001.htm.

Also, you can try ExcelApp.DisplayAlerts=false before you call SaveAs; restore it after the call is made.

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 18 Jul, 2014 03:19:00 Top
Jacob John




Posts: 20
Joined: 2013-08-29
Hello Andrei,
I've tried saving the file manually,it is working fine.The problem is coming only with the Protected View Documents where as for regular files,Save As method is working asusual.
Please help me on this.
Posted 18 Jul, 2014 05:53:40 Top
Andrei Smolin


Add-in Express team


Posts: 14137
Joined: 2006-05-11
Hello Jacob,

Do you specify the file type when calling SaveAs? What Excel version are you using? Do you save XLS or XLSX?

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 18 Jul, 2014 08:08:27 Top
Jacob John




Posts: 20
Joined: 2013-08-29
Hello Andrei,
I'm using Excel 2013 and saving xlsx type.I've tried specifying fie type but it doesn't seem to be working.
Posted 22 Jul, 2014 10:23:18 Top
Andrei Smolin


Add-in Express team


Posts: 14137
Joined: 2006-05-11
Jacob,

I've created a test add-in which produces exception "This command is not available when Excel is sandboxed" exception when run in a protected view window. I don't think there's a way to bypass this programmatically. The user should enable editing the workbook first.

Here's a citation from http://msdn.microsoft.com/en-us/library/office/ff822367%28v=office.15%29.aspx:

Because a Protected View window is designed to protect the user from potentially malicious code, the operations you can perform by using a Workbook object returned by a ProtectedViewWindow object will be limited. Operations that are not allowed will return an error.

private void adxRibbonButton1_OnClick(object sender, IRibbonControl control, bool pressed) {
    object context = control.Context;
    Excel.Workbook workbook = null;
    try {
        if (context is Excel.ProtectedViewWindow) {
            Excel.ProtectedViewWindow protectedViewWindow = ExcelApp.ActiveProtectedViewWindow;
            workbook = protectedViewWindow.Workbook;
            workbook.SaveAs(@"d:	est.xlsx", Excel.XlFileFormat.xlWorkbookDefault);
        } else if (context is Excel.Window) {
            Excel.Window window = context as Excel.Window;
            workbook = window.Parent as Excel.Workbook;
            workbook.SaveAs(@"d:	est.xlsx", Excel.XlFileFormat.xlWorkbookDefault);
        }
    } catch (Exception ex) {
        MessageBox.Show("Exception: " + ex.Message + "

StackTrace: " + ex.StackTrace);
    } finally {
        if (workbook != null) Marshal.ReleaseComObject(workbook); workbook = null;
        if (context != null) Marshal.ReleaseComObject(context); context = null;
    }
}


Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 23 Jul, 2014 03:54:14 Top