Click here to Skip to main content
15,890,995 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am getting the exception on running below code. It is run as windows service in c#.
It is working fine in my machine. Using Excel 2013.
Below is the core code
C#
#region SmartPDFCaptureService
     /// <summary>
     /// SMART_PDf_CAPTURE_SERVICE_SYNCHRONIZER
     /// </summary>
     /// <param name="source"></param>
     /// <param name="e"></param>
     private void SmartPDFCaptureService(object source, ElapsedEventArgs e)
     {
         WriteServiceLog("" + SmartPDFLogCaption + "Entered " + MethodInfo.GetCurrentMethod().Name + " Method");
         try
         {
             //Main Function to capture data from PDFs set in FileStore Folder
             ExtractPDFData();
         }
         catch (Exception ex)
         {
             WriteErrorLog(MethodInfo.GetCurrentMethod().Name + " " + ex.Message, ex.StackTrace, true);
         }
         WriteServiceLog("" + SmartPDFLogCaption + "Finished " + MethodInfo.GetCurrentMethod().Name + " Method");
     }
     #endregion


What I have tried:

#region SmartPDFCaptureService
///
/// SMART_PDf_CAPTURE_SERVICE_SYNCHRONIZER
///

/// <param name="source" />
/// <param name="e" />
private void SmartPDFCaptureService(object source, ElapsedEventArgs e)
{
WriteServiceLog("" + SmartPDFLogCaption + "Entered " + MethodInfo.GetCurrentMethod().Name + " Method");
try
{
//Main Function to capture data from PDFs set in FileStore Folder
ExtractPDFData();
}
catch (Exception ex)
{
WriteErrorLog(MethodInfo.GetCurrentMethod().Name + " " + ex.Message, ex.StackTrace, true);
}
WriteServiceLog("" + SmartPDFLogCaption + "Finished " + MethodInfo.GetCurrentMethod().Name + " Method");
}
#endregion
Posted
Updated 24-Apr-16 22:56pm
Comments
ranio 25-Apr-16 3:11am    
Below is the excel part using function GetfillData
#region GetFillData
//Copy the Standard Excel Template to DATA folder fetching PDF Data/Move PDF from the File Store to DONE Folder afterwards at last
private void GetFillData(string SourcePDFFullPath)
{

WriteServiceLog("" + SmartPDFLogCaption + "Entered " + MethodInfo.GetCurrentMethod().Name + " Method");
try
{

//Delimiter with which data is fetched from SMART PDF (split with '|')
Char delimiter1 = '|';
//Delimiter with which data is fetched from SMART PDF (split with '~' after spliting with '|')
Char delimiter2 = '~';

//Pass the PDF File in the FileStore
PdfReader pdfReader = new PdfReader(SourcePDFFullPath);
AcroFields pdfFormFields = pdfReader.AcroFields;
string PDFFileName = Path.GetFileName(SourcePDFFullPath);
//Get Template Id from Hidden Field(Template ID set Statically) in SMART PDF
string TemplateID = pdfFormFields.GetField("hidTemplateId");
//Get PDF Data in control fields from Hidden Field(PDF) in SMART PDF
string CapturedPDFValues = pdfFormFields.GetField("hidPDFDataExport");
string[] PDFValues = CapturedPDFValues.Split(delimiter1);

//Fetch Unique No(Quotation No) from PDF
string UniqueNo = PDFValues[0].Substring(PDFValues[0].LastIndexOf(delimiter2) + 1);
//Set Current Date while rernaming Excel Template in DATA Folder (ddmmyyyy format)configured in APP.Config
string CurrentDate = DateTime.Now.ToString("" + FileRenameDateFormat + "");


//Validate If the Template Id is empty for the PDF Files in FileStore Folder
if (TemplateID == "")
{
WriteServiceLog("" + SmartPDFLogCaption + "" + TemplateIdValidationMsg1 + "" + SourcePDFFullPath + MethodInfo.GetCurrentMethod().Name + " Method");
return;

}
//Capture PDF Data If the Template Id Exists for the PDF File in FileStore Folder
else
{
//Validate Excel Template exists in the Template Folder
string ExcelTemplateFullPath = string.Empty;
string ExcelExtension = string.Empty;
//Save the Excel Template with data in the Destination Excel Folder(DATA Folder in FileStore) with UniqueNo+Currentdate(ddmmyyyy format)
string DestinationFullPathExcel = string.Empty;
if (File.Exists(ExcelTemplatePath + TemplateNamingStart + TemplateID + ".xls"))
{
ExcelExtension = ".xls";
ExcelTemplateFullPath = ExcelTemplatePath + TemplateNamingStart + TemplateID + ExcelExtension;
DestinationFullPathExcel = DestinationExcelPath + TemplateNamingStart + TemplateID + "_" + UniqueNo + CurrentDate + ExcelExtension;

}
else if (File.Exists(ExcelTemplatePath + TemplateNamingStart + TemplateID + ".xlsx"))
{
ExcelExtension = ".xlsx";
ExcelTemplateFullPath = ExcelTemplatePath + TemplateNamingStart + TemplateID + ExcelExtension;
DestinationFullPathExcel = DestinationExcelPath + TemplateNamingStart + TemplateID + "_" + UniqueNo + CurrentDate + ExcelExtension;

}
else
{

WriteServiceLog("" + SmartPDFLogCaption + "Template Id:" + TemplateNamingStart + TemplateID + " doesn't exists in the Template Folder" + MethodInfo.GetCurrentMethod().Name + " Method");
return;
}
//Capture PDF Data If Template I

Probably, it';s a permissions issue - a service doesn't run under your user ID, so if your ExtractPDFData method is accessing files (as is likely) then you probably need to check and make sure that the folder they are in has sufficient permissions for all users to read / write.

However, none of that code uses Excel at all - of if the method is trying to use Excel to read PDF file, then it's possible that Excel is not installed (or not installed for all users) on the target machine. Why are you using Excel to read PDF files anyway?

We can't check any of that for you - we don't have access to your computers - but if all of that checks out, then you need to be very much more specific about the error message, and the exact line it occurs on.
 
Share this answer
 
Microsoft discourages the use of Office programs by services or other automated tasks:
https://support.microsoft.com/en-us/kb/257757[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900