Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Friday, February 8, 2013 7:32 PM
private bool Convert(string sourcePath, string targetPath, XlFixedFormatType targetType)
{
bool result;
object missing = Type.Missing;
ApplicationClass application = null;
Workbook workBook = null;
try
{
application = new ApplicationClass();
object target = targetPath;
object type = targetType;
workBook = application.Workbooks.Open(sourcePath, missing, missing, missing, missing, missing,
missing, missing, missing, missing, missing, missing, missing, missing, missing);
workBook.ExportAsFixedFormat(targetType, target, XlFixedFormatQuality.xlQualityStandard, true, false, missing, missing, missing, missing);
result = true;
}
catch
{
result = false;
}
finally
{
if (workBook != null)
{
workBook.Close(true, missing, missing);
workBook = null;
}
if (application != null)
{
application.Quit();
application = null;
}
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
}
return result;
}
Argh...I closed the link that I found this code from, but it was here on the microsoft site...if I find it again, I'll link to it. However, I get a compile error of:Interop type 'Microsoft.Office.Interop.Excel.ApplicationClass' cannot be embedded. use the applicable interface instead.
All replies (4)
Friday, February 8, 2013 8:19 PM âś…Answered
The issue is that you're using the PIA version of the Office assemblies but they cannot be embedded. The simpler solution would be to right click the interop assembly and set the Embed option to false.
Michael Taylor - 2/8/2013
http://msmvps.com/blogs/p3net
Friday, February 8, 2013 8:56 PM
Do you mean I should right click this statement:
using Microsoft.Office.Interop.Excel;
When I do, I don't see the option to set Embed to false.
...If it matters this is a Windows Form running VS2010 Ultimate.
EDIT
I figured it out, I just had to click the drop down arrow in Solution Explorer, and then right click the offending assembly choose properties, then set Embed to False.
Friday, February 8, 2013 9:52 PM
One Additional question...the above code doesn't actually convert it to pdf tho...what did I code inaccurately?
Friday, February 8, 2013 10:20 PM
I'm not an expert on Excel so your best bet is to post in the Excel forums. But based upon this documentation (http://msdn.microsoft.com/en-us/library/microsoft.office.tools.excel.workbook.exportasfixedformat.aspx) you have to install an addin before it'll work.
Michael Taylor - 2/8/2013
http://msmvps.com/blogs/p3net