Employers - Post Free Jobs
Forum guidelines
Convert VBA to C#
Posted on: 15 Feb 2013
I need this line of code:
ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF FileName:="sales.pdf" Quality:=xlQualityStandard DisplayFileAfterPublish:=True

converted to C#

Can someone please oblige this request?

  • 0
  • 0
Re: Convert VBA to C#
Posted on: 16 Feb 2013   Accepted Answer
If you don't specify a full path, then the file will be saved to the 'current' directory, normally the same directory as the .exe starts from.

If you don't want to hard-code a path, then it's possible to save files to system folders using the Environment.SpecialFolder enumeration:


Re: Convert VBA to C#
Posted on: 15 Feb 2013  
Try this:

(Type: XlFixedFormatType.xlTypePDF, Filename: "sales.pdf", Quality: XlFixedFormatQuality.xlQualityStandard, OpenAfterPublish: true);

The ExportAsFixedFormat method doesn't have a parameter called DisplayFileAfterPublish (not in Excel 2007 anyway) so I've assumed that it must be OpenAfterPublish that's intended.

Re: Convert VBA to C#
Posted on: 15 Feb 2013  
I added a Assembly Extension to the Microsoft.Office.Tools.Excel.v4.0.Utilities 

On the very first argument 
(Type: XLFixedFormatType.xlTypePDF,

I get a compile error of The name 'XLFixedFormatType' does not exist in the current context.

However on top of that, I am now receiving multiple Errors like:
'Workbook' is an ambiguous reference between 'Microsoft.Office.Interop.Excel.WOrkbook' and 'Microsoft.Office.Tools.Excel.Workbook'

And the 2nd compile error of Cannot find the interop type that matches the embedded interop type 'Microsoft.Office.Tools.Excel.Worksheet'  Are you missing an assembly reference?

I am going to remove the Assembly Extension, as that will remove my compile errors.  Did I do that incorrectly?

Re: Convert VBA to C#
Posted on: 15 Feb 2013  
What I generally do with Excel is to add this 'using' alias at the top of the file:

using Excel = Microsoft.Office.Interop.Excel;

I then prefix everything with Excel. So the call to this method would become:

Excel.Application oXL = new Excel.Application();
// etc

(Type: Excel.XlFixedFormatType.xlTypePDF, Filename: "sales.pdf", Quality: Excel.XlFixedFormatQuality.xlQualityStandard, OpenAfterPublish: true);

If you need to use tools as well, you could define a using alias for that:

using XLTools = Microsoft.Office.Tools.Excel;

and then use that alias to distinguish between the two.

However, if you don't need to use tools then, yes, I'd remove the assembly reference as Office interop is complicated enough!

Re: Convert VBA to C#
Posted on: 15 Feb 2013  
Let me give that a twirl.  I have lots of variables so it may take me a bit to get them updated as such.
Re: Convert VBA to C#
Posted on: 15 Feb 2013  
That is definately generating a .pdf but my ? is where does it save it, or do I need to add a parameter in to save the pdf also?
Re: Convert VBA to C#
Posted on: 15 Feb 2013  
I had to list out where it was saving the .pdf.  DUH!  Such a simple fix.


Offshore Software and Database Development
MCN is your source for developing solutions involving websites, mobile apps, cloud-computing, databases, BI, back-end services and processes and client-server applications.