Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why would the Excel Interop remove an image after processing a file?

Excel Interop is removing images from processed files.

I’m using the Excel Interop, no third-party components are present (that I'm aware of). The workflow is -- create a copy (target) of a file (template), populate cells, change radio-button state

  1. Create copy (target) of a pre-existing .xslm file (template)
  2. Open target via Excel Interop
  3. Populate target cells, change radio-button state
  4. The worksheet with an image is not modified
  5. Close target

On my dev machine, the target file looks great -- everything is populated, the image is present. NB: on my dev machine, I'm running the code from the VS2010 IDE.

On the production machine -- everything is populated, but the image is not present. Instead, the following error appears in its place: NB: on the production machine, it's running as a service, with the Local Service account.

"The image part with relationship ID rId1 was not found in the file" the image part with relationship ID rId1 was not found in the file

The entire workbook is opened via the following code:

var workbook = workbooks.Open(targetPath
     0, false, 5, Type.Missing, Type.Missing, false, XlPlatform.xlWindows, "",
     true, false, 0, true, false, false);

Please note that the worksheet with the image is not manipulated in the code.

The worksbooks (and individual worksheets) are protected. However, the protected template is processed correctly in dev, but not in production. I don't think the protection has anything to do with it (but who knows, right? This is Interop. ugh).

The file was created by another party, and all components (ie, the image) reside within the .xslm structure, not as links to another server.

I have verified that the image is visible on the production machine in the template file, but not in a processed file.

To confirm that this was not an issue in opening the file within the production, I emailed myself a copy, and the image continued to be not present.

I have also confirmed that, on my development machine, processed files do have a visible image.

I unprotected the worksheet, and unzipped the file structure. The .jpg file is indeed not present in the processed target from the production machine.

One more note -- Office 2010 is installed on my development machine, but Office 2007 on the production machine. As a result, I'm using the Office 12 Interop. No runtime errors are generated in either environment.

I am using the Interop (instead of an OpenXml library) because there are ActiveX controls present that must be populated. Note however, that none of the ActiveX controls have any issues -- they are working fine. It's just image-files that are vanishing from processed files (they render fine in the template file).

UPDATE NOTE: There are four other image-files, all .emf on a different worksheet; they are all stripped as well.

like image 442
Michael Paulukonis Avatar asked Feb 21 '23 20:02

Michael Paulukonis


2 Answers

As explained in comments (and eventually in an edit to the question), the code was running in production as a service, with the Local Service account.

I am now unsure why I picked this account -- something I found in passing during my research on getting the Interop to run correctly as a service?

However, once I switched from the Local Service account to the Local System account (and checked "Allow service to interact with desktop") it worked. Automagically.

  1. services.msc
  2. select the service
  3. right-click, select "Properties"
  4. select the "Log On" tab
  5. select "Local System account" and check "Allow service to interact with desktop"

"Allow service to interact with desktop" might not be required; Other notes on automating the Interop suggest that other desktop settings are required, however I did an install where those pre-requisites were set but THIS value unchecked; app still worked...

http://i.imgur.com/k6Qwy.png

like image 111
Michael Paulukonis Avatar answered Apr 06 '23 19:04

Michael Paulukonis


Interop is not supported in sever-scenarios by MS.

There are many options to read/edit/create Excel files without Interop:

MS provides the free OpenXML SDK V 2.0 - see http://msdn.microsoft.com/en-us/library/bb448854%28office.14%29.aspx (XLSX only)

This can read+write MS Office files (including Excel).

Another free option see http://www.codeproject.com/KB/office/OpenXML.aspx (XLSX only)

IF you need more like handling older Excel versions (like XLS, not only XLSX), rendering, creating PDFs, formulas etc. then there are different free and commercial libraries like ClosedXML (free, XLSX only), EPPlus (free, XLSX only), Aspose.Cells, SpreadsheetGear, LibXL and Flexcel etc.

It is hard to say whether your specific case (ActiveX controls) is fully supported by any of the above... that is something you need to test...

Even if the ActiveX controls are supported by any of the libraries there is a chance that the ActiveX controls themselves don't work within a Windows Service (permissions etc.).

EDIT - as per comment:

I understand the ActiveX problem and I addressed it from 2 points:

  • Have you really tested all above mentioned libraries ?
  • Have you checked with the implementor(s) of the ActiveX controls whether the ActiveX controls could even theoretically work in a Windows Service scenario ?

EDIT 3 - after the UPDATE from the OP:

.emf is a vector file format... IIRC GDI+ is used to render it on current Windows versions... .emf has evolved a bit over time so older OS and/or Office versions can sometimes have problems rendering newer .emf files... which in turn means the problem is the "missing desktop" in the Windows Service AND/OR that your .emf files are "too new" for the production machine.

like image 32
Yahia Avatar answered Apr 06 '23 20:04

Yahia