Home » Visual StudioRSS

Not able to save Excel Files Embedded inside MS Word Document

I have excel files embedded inside a MS Word document, these i have taken into OLEFormat variables. I need to extract them and save them into a folder using C#.

I am using the following code where "obj" is the Microsoft.Office.Interop.Word.OLEFormat Object which holds the embedded excel file

obj.Activate();

Microsoft.Office.Interop.Excel._Workbook objExcel = (Microsoft.Office.Interop.Excel._Workbook)obj.Object;

Now both objExcel.SaveAs and objExcel.SaveCopyAs dont work. Both give exceptions.

Exception when objExcel.SaveAs is used

"Exception from HRESULT: 0x800A03EC"

Exception when objExcel.SaveCopyAs is used

The file could not be accessed. Try one of the following:


• Make sure the specified folder exists.
• Make sure the folder that contains the file is not read-only.
• Make sure the file name does not contain any of the following characters:  <  >  ?  [  ]  :  | or  *
• Make sure the file/path name doesn't contain more than 218 characters.

Please help me in resolving this issue 

 

 

 

 

 

4 Answers Found

 

Answer 1

Hi Ankit

I think the Excel workbooks needs to be opened in an Excel window (rather than in-place) in order to save  (as) to a separate file. You should be able to force that using DoVerb on the OLEFormat object  (obj)...

 

Answer 2

Hi Cindy,

Its not working. I am still getting the same exceptions.

This is what i am using

 

object verbIndex = WdOLEVerb.wdOLEVerbShow;

obj.Activate();

obj.DoVerb(ref verbIndex);

Also please tell me what exactly does DoVerb do, i couldnt find much information on this online.

 

Answer 3

Please find my sample code, the input file is a MS Word Document which has a single table and the embedded files to be extracted are present in cell (2,2).  For convinience, i have written this code in form load of a windows application.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Interop.Word;
using Microsoft.Office.Interop.Excel;

namespace TestExtractObjects
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
      Microsoft.Office.Interop.Word.Application WordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
      object nullobj = System.Reflection.Missing.Value;
      object file = @"<InputFileLocation>";
      object verbIndex = WdOLEVerb.wdOLEVerbShow;

      Microsoft.Office.Interop.Word._Document Worddoc = WordApp .Documents.Open(
        ref file, ref nullobj, ref nullobj, ref nullobj,
        ref nullobj, ref nullobj, ref nullobj,
        ref nullobj, ref nullobj, ref nullobj,
        ref nullobj, ref nullobj, ref nullobj,
        ref nullobj, ref nullobj, ref nullobj);

      Microsoft.Office.Interop.Word.Table tbl = Worddoc .Tables[1];

      try
      {

        for (int i = 1; i <= tbl.Cell(2, 2).Range.Fields.Count; i++)
        {
          Microsoft.Office.Interop.Word.OLEFormat obj = tbl.Cell(2, 2).Range.Fields[i].OLEFormat;
          object strFileName = @"<OutputFolderWhereExtractedFilesAreToBeSaved" + @"\" + obj.IconLabel;


          switch (obj.ProgID)
          {

            case "Excel.Sheet.12":
            case "Excel.Sheet.8":
              obj.Activate();
              obj.DoVerb(ref verbIndex);
              Microsoft.Office.Interop.Excel._Workbook objExcel = (Microsoft.Office.Interop.Excel._Workbook)obj.Object;
              object strExcelFileName = @"E:\SCV Work Items\OutputFolder" + @"\" + obj.IconLabel;
              objExcel.SaveAs(strExcelFileName, nullobj, nullobj, nullobj, nullobj, nullobj, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlShared, nullobj, nullobj, nullobj, nullobj, nullobj);
              break;

            case "Word.Document.12":
            case "Word.Document.8":
              Document doc = (Document)obj.Object;
              object strDocFileName = @"<OutputFolderWhereExtractedFilesAreToBeSaved>" + @"\" + obj.IconLabel;
              doc .SaveAs(ref strDocFileName, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj,
                        ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj,
                            ref nullobj, ref nullobj, ref nullobj);
              break;


            default:
              break;

          }
        }
      }
      catch (Exception ex)
      {
        MessageBox.Show(ex.Message);
      }

      
    }
  }
}

 

 

 

 

Answer 4

Hi Ankit

You need wdOLEVerbOpen to force it to open in a separate window. Show does exactly what you don't want: opens it in-place.

This is all the information available (from Words VBA Help). Most of them aren't really any use.

Name Value Description
wdOLEVerbDiscardUndoState-6Forces the object to discard any undo state that it might be maintaining; note that the object remains active, however.
wdOLEVerbHide-3Removes the object's user interface from view.
wdOLEVerbInPlaceActivate-5Runs the object and installs its window, but doesn't install any user-interface tools.
wdOLEVerbOpen-2Opens the object in a separate window.
wdOLEVerbPrimary0Performs the verb that is invoked when the user double-clicks the object.
wdOLEVerbShow-1Shows the object to the user for editing or viewing. Use it to show a newly inserted object for initial editing.
wdOLEVerbUIActivate-4Activates the object in place and displays any user-interface tools that the object needs, such as menus or toolbars.

 
 
 

<< Previous      Next >>


Microsoft   |   Windows   |   Visual Studio   |   Follow us on Twitter