Search

Tuesday, November 2, 2010

Excel file convert in pdf using c#, Office Automation

public static void ConvertExcel(string SourceFile, string DestinatioFile)
{

//SourceFile:- c:\ab.xls or .xlsx
//DestinatioFile :- d:\\ab.pdf
object missing = System.Reflection.Missing.Value;
msExcel.Application excel = new msExcel.Application();


try
{
excel.Visible = false;
excel.ScreenUpdating = false;
excel.DisplayAlerts = false;

msExcel.Workbook wbk = excel.Workbooks.Open(SourceFile, missing,
missing, missing, missing, missing, missing,
missing, missing, missing, missing, missing,
missing, missing, missing);
wbk.Activate();


msExcel.XlFixedFormatType fileFormat = msExcel.XlFixedFormatType.xlTypePDF;

// Save document into PDF Format
wbk.ExportAsFixedFormat(fileFormat, DestinatioFile,
missing, missing, missing,
missing, missing, missing,
missing);

object saveChanges = msExcel.XlSaveAction.xlDoNotSaveChanges;
((msExcel._Workbook)wbk).Close(saveChanges, missing, missing);
wbk = null;
}
finally
{
((msExcel._Application)excel).Quit();
excel = null;
}

}

Convert Word file to pdf Using c# , Office Automation

public static void Convertword(string SourceFile, string DestinatioFile)
{
//Source file- file name ex. d:\ab.doc or ab.docx.
//DestinatioFile = d:\ab.pdf

ApplicationClass wordApplication = new ApplicationClass();



Document wordDocument = null;

object paramSourceDocPath = SourceFile;

object paramMissing = Type.Missing;

string paramExportFilePath = DestinatioFile;

WdExportFormat paramExportFormat = WdExportFormat.wdExportFormatPDF;

bool paramOpenAfterExport = false;

WdExportOptimizeFor paramExportOptimizeFor =

WdExportOptimizeFor.wdExportOptimizeForPrint;

WdExportRange paramExportRange = WdExportRange.wdExportAllDocument;

int paramStartPage = 0;

int paramEndPage = 0;

WdExportItem paramExportItem = WdExportItem.wdExportDocumentContent;

bool paramIncludeDocProps = true;

bool paramKeepIRM = true;

WdExportCreateBookmarks paramCreateBookmarks =

WdExportCreateBookmarks.wdExportCreateWordBookmarks;

bool paramDocStructureTags = true;

bool paramBitmapMissingFonts = true;

bool paramUseISO19005_1 = false;

try
{

// Open the source document.

wordDocument = wordApplication.Documents.Open(

ref paramSourceDocPath, ref paramMissing, ref paramMissing,

ref paramMissing, ref paramMissing, ref paramMissing,

ref paramMissing, ref paramMissing, ref paramMissing,

ref paramMissing, ref paramMissing, ref paramMissing,

ref paramMissing, ref paramMissing, ref paramMissing,

ref paramMissing);



// Export it in the specified format.

if (wordDocument != null)

wordDocument.ExportAsFixedFormat(paramExportFilePath,

paramExportFormat, paramOpenAfterExport,

paramExportOptimizeFor, paramExportRange, paramStartPage,

paramEndPage, paramExportItem, paramIncludeDocProps,

paramKeepIRM, paramCreateBookmarks, paramDocStructureTags,

paramBitmapMissingFonts, paramUseISO19005_1,

ref paramMissing);

}

catch (Exception ex)
{

// Respond to the error

}

finally
{

// Close and release the Document object.

if (wordDocument != null)
{

wordDocument.Close(ref paramMissing, ref paramMissing,

ref paramMissing);

wordDocument = null;

}



// Quit Word and release the ApplicationClass object.

if (wordApplication != null)
{

wordApplication.Quit(ref paramMissing, ref paramMissing,

ref paramMissing);

wordApplication = null;

}



GC.Collect();

GC.WaitForPendingFinalizers();

GC.Collect();

GC.WaitForPendingFinalizers();

}
}

Office Automation Configuraiton on Server.


WARNING: Office was not designed, and is not safe, for unattended execution
on a server. Developers who use Office in this manner do so at their own risk.



Regardless, it may absolutely required to use Office in this manner. In these cases,
special configuration must be done to avoid errors on Office startup. The steps
in this article demonstrate how to configure Office to run as the interactive user
account when it is started for Automation.

Configuring Office as the launching user


To set up an Office Automation server as the launching user account, follow these
steps:




Log on to the computer as the Administrator and create a new user account that will
automate Office. In our example, this account is named OfficeAutomationUser.
Create a password for this user account, and select Never expire so that
the password does not have to be changed.


Add the OfficeAutomationUser account to the Administrators group.


Click Start, click Run, and then type DCOMCNFG.
Select the application that you want to automate. The application names are listed
below:

Microsoft Access 97 - Microsoft Access Database

Microsoft Access 2000/2002/2003/2007 - Microsoft Access Application

Microsoft Excel 97/2000/2002/2003/2007 - Microsoft Excel Application

Microsoft Word 97 - Microsoft Word Basic

Microsoft Word 2000/2002/2003/2007 - Microsoft Word Document

Click Properties to open the property dialog box for this application.



Click the Security tab. Verify that Use Default Access Permissions
and Use Default Launch Permissions are selected.


Click the Identity tab. Verify that The Launching User is selected.


Click OK to close the property dialog box and return to the main applications
list dialog box.


In the DCOM Configuration dialog box, click the Default Security tab.


Click Edit Defaults for access permissions. Verify that the following users
are listed in the access permissions, or add the users if they are not listed:

SYSTEM

INTERACTIVE

Everyone

Administrators

OfficeAutomationUser

IUSR_<machinename>*

IWAM_<machinename>*



* These accounts exist only if Internet Information Server (IIS) is installed
on the computer.



Make sure that each user is allowed access and click OK.


Click Edit Defaults for launch permissions. Verify that the following users
are listed in the launch permissions, or add the users if they are not listed:

SYSTEM

INTERACTIVE

Everyone

Administrators

OfficeAutomationUser

IUSR_<machinename>*

IWAM_<machinename>*



* These accounts exist only if IIS is installed on the computer.



Make sure that each user is allowed access, and then click OK.


Click OK to close DCOMCNFG.


Start REGEDIT and verify that the following keys and string values exist
for the Office application that you want to automate:

Microsoft Access 2000/2002/2003/2007:

Key: HKEY_CLASSES_ROOT\AppID\MSACCESS.EXE

AppID: {73A4C9C1-D68D-11D0-98BF-00A0C90DC8D9}



Microsoft Access 97:

Key: HKEY_CLASSES_ROOT\AppID\MSACCESS.EXE

AppID: {8CC49940-3146-11CF-97A1-00AA00424A9F}



Microsoft Excel 97/2000/2002/2003/2007:

Key: HKEY_CLASSES_ROOT\AppID\EXCEL.EXE

AppID: {00020812-0000-0000-C000-000000000046}



Microsoft Word 97/2000/2002/2003/2007:

Key: HKEY_CLASSES_ROOT\AppID\WINWORD.EXE

AppID: {00020906-0000-0000-C000-000000000046}




If these keys do not exist, you can create them by running the following .reg file
on your system:


REGEDIT4

[HKEY_CLASSES_ROOT\AppID\WINWORD.EXE]
"AppID"="{00020906-0000-0000-C000-000000000046}"

[HKEY_CLASSES_ROOT\AppID\EXCEL.EXE]
"AppID"="{00020812-0000-0000-C000-000000000046}"

[HKEY_CLASSES_ROOT\AppID\MSACCESS.EXE]
"AppID"="{73A4C9C1-D68D-11D0-98BF-00A0C90DC8D9}"
     

NOTE: The sample .reg file is for Access 2000 or Access 2002. If you
are using Access 97, change the AppID key to the following:

"AppID"="{8CC49940-3146-11CF-97A1-00AA00424A9F}"




Restart the system. This is required.




Cannot find Microsoft Word Application in DCOM


On 64 bit system with 32 bit Office try this:
  1. Start
  2. Run
  3. mmc -32
  4. File
  5. Add Remove Snap-in
  6. Component Services
  7. Add
  8. OK
  9. Console Root
  10. Component Services
  11. Computers
  12. My Computer
  13. DCOM Config
  14. Microsoft Excel Application

Blog Archive

Contributors