Microsoft Office Interop Excel for Office 2007

0 votes

I'm interested in testing out the Windows form application that turns office files (Excel, Word, and Powerpoint) into PDF files. The 2007 edition of Office and Visual Studio won't install on my client's computer. To convert to PDF format, my application needs Microsoft.Office.Iterop.Excel.dll. The PC of my client cannot locate this dll file, and the following error has occurred.

System.AugumentException: Value does not fall within the expected range.
at Microsoft.Office.Interop.Excel._Workbook.ExportAsFixedFromat(.......)

How can I solve this problem?

My code is following

public bool ExportWorkbookToPdf(string workbookPath, string outputPath)
        {
            // If either required string is null or empty, stop and bail out
            if (string.IsNullOrEmpty(workbookPath) || string.IsNullOrEmpty(outputPath))
            {
                return false;
            }

            // Create COM Objects
            Microsoft.Office.Interop.Excel.Application excelApplication;
            Microsoft.Office.Interop.Excel.Workbook excelWorkbook;

            // Create new instance of Excel
            excelApplication = new Microsoft.Office.Interop.Excel.Application();

            // Make the process invisible to the user
            excelApplication.ScreenUpdating = false;

            // Make the process silent
            excelApplication.DisplayAlerts = false;

            // Open the workbook that you wish to export to PDF
            excelWorkbook = excelApplication.Workbooks.Open(workbookPath);

            MessageBox.Show(workbookPath);
            // If the workbook failed to open, stop, clean up, and bail out
            if (excelWorkbook == null)
            {
                excelApplication.Quit();

                excelApplication = null;
                excelWorkbook = null;
                MessageBox.Show("in null");
                return false;
            }

            var exportSuccessful = true;
            try
            {
                // Call Excel's native export function (valid in Office 2007 and Office 2010, AFAIK)
                excelWorkbook.ExportAsFixedFormat(Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF, outputPath);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
                // Mark the export as failed for the return value...
                exportSuccessful = false;

                // Do something with any exceptions here, if you wish...
                // MessageBox.Show...        
            }
            finally
            {
                // Close the workbook, quit the Excel, and clean up regardless of the results...
                excelWorkbook.Close();
                excelApplication.Quit();

                excelApplication = null;
                excelWorkbook = null;
            }

            // You can use the following method to automatically open the PDF after export if you wish
            // Make sure that the file actually exists first...
            if (System.IO.File.Exists(outputPath))
            {
                MessageBox.Show(outputPath);
                System.Diagnostics.Process.Start(outputPath);
            }

            return exportSuccessful;
        
Nov 5, 2022 in Others by Kithuzzz
• 38,020 points
1,448 views

1 answer to this question.

0 votes

Each client must install the  2007 Microsoft Office Add-in: Microsoft Save as PDF, as has already been mentioned in the comments. It makes no difference if you are using Windows 8.1 or if you have any installed PDF readers. For Office 2007 to write PDFs, you need the Add-in. (Office 2010 and 2013 don't require any Add-Ins.)

answered Nov 5, 2022 by narikkadan
• 63,720 points

Related Questions In Others

0 votes
1 answer

c#, Microsoft Interop Excel , change font style for s selected range

Change your code to this: worksheet.get_Range("B3", "B4").Cells.Font.Name = ...READ MORE

answered Oct 31, 2022 in Others by narikkadan
• 63,720 points
1,855 views
0 votes
1 answer

How do I create an Excel (.XLS and .XLSX) file in C# without installing Microsoft Office?

You can use OLEDB to create and ...READ MORE

answered Jan 3, 2023 in Others by narikkadan
• 63,720 points
1,559 views
0 votes
1 answer

Where can I use the best online practice test software for the Microsoft AZ-2014 exam?

Hi, @John, I would suggest you visit here https://learning.shine.com/practice-tests/microsoft/sub I ...READ MORE

answered Oct 14, 2020 in Others by Gitika
• 65,890 points
812 views
0 votes
1 answer

Latest Microsoft AZ-304 Dumps for Exams Revision Guaranteed

Hi, @pamega, Thanks for the info. READ MORE

answered Nov 30, 2020 in Others by Gitika
• 65,890 points
697 views
0 votes
1 answer

How to Freeze Top Row and Apply Filter in Excel Automation with C#

Try this: // Fix first row workSheet.Activate(); workSheet.Application.ActiveWindow.SplitRow = 1; workSheet.Application.ActiveWindow.FreezePanes ...READ MORE

answered Oct 22, 2022 in Others by narikkadan
• 63,720 points
2,564 views
0 votes
0 answers

Print Excel Worksheet/Workbook

I'm attempting to make a C# Winform ...READ MORE

Oct 31, 2022 in Others by Kithuzzz
• 38,020 points
629 views
0 votes
1 answer

Obtaining Excel worksheet reference by worksheet name via C#

Instead of using Excel.Workbook.Sheets collection, it's easier to access Excel.Workbook.Worksheet collection, ...READ MORE

answered Nov 10, 2022 in Others by narikkadan
• 63,720 points
550 views
0 votes
1 answer

Using c# to select a worksheet in excel

Try this: Excel.Worksheet xlWorkSheetFocus = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(2); xlWorkSheetFocus.Activate(); I hope this ...READ MORE

answered Nov 14, 2022 in Others by narikkadan
• 63,720 points
580 views
0 votes
1 answer

'Microsoft.Office.Interop.Excel.Range' does not contain a definition for 'get_Default'

You are using C# version 4, the ...READ MORE

answered Oct 16, 2022 in Others by narikkadan
• 63,720 points
2,133 views
0 votes
1 answer

How to reference Microsoft.Office.Interop.Excel dll?

Use NuGet (VS 2013+): The NuGet package manager ...READ MORE

answered Oct 9, 2022 in Others by narikkadan
• 63,720 points
2,776 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP