views:

57

answers:

1

Hi I'm trying to create and open a powerpoint by using binary data from a sql database by using linq.

A. First I'm reading it into a byte array and then creating the .ppt file.

public bool createPresentation(string fileName, byte[] powerPoint)
    {
        DirectoryInfo di = new DirectoryInfo(downloadPath);
        if (!di.Exists)
            di.Create();

        fileName = string.Concat(downloadPath, fileName,".PPT");
        //Define a new instance of FileStream
        FileStream powerpointStream = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite);
       powerpointStream.Write(powerPoint, 0, powerPoint.Count());
       powerpointStream.Close();

        return True; 
    }

B. Then I'm trying to open the .ppt file and save it as a .pptx file

public bool convertPPTtoPPTX(string path)
    {
        string source = path;
        string destination = path.Replace("PPT", "PPTX");

        DirectoryInfo di = new DirectoryInfo(downloadPathPPTX);
        if (!di.Exists)
            di.Create();

        PowerPoint.Application app = new PowerPoint.Application();//Line Y

        PowerPoint.Presentation pptx = app.Presentations.Open(source, MsoTriState.msoFalse, MsoTriState.msoTrue, MsoTriState.msoFalse);//Line Z
        pptx.SaveAs(destination, PowerPoint.PpSaveAsFileType.ppSaveAsDefault);
        pptx.Close();
        app.Quit();

       return true;
    }

C. Finally I'm trying to read the .pptx file into a byte array inorder to update the db through linq.

    public byte[] convertToBinary(string source)
    {
        byte[] binary = File.ReadAllBytes(source);
        return binary;
    }

E. This is how i obtain the binary data through linq-sql

public List<Template> getPPTFileBiniary(int ID)
    {
        var ppt = from p in db.paPresentationTemplates
                  where p.ID==ID
                  select new Template { pptFile = p.PPTFile.ToArray() };

        return ppt.ToList();
    }

F. Template class used in E

class Template
{
    public int ID { get; set; }
    public string  FileName { get; set; }
    public Byte[] pptFile { get; set; }

    public Template()
    { 

    }

}

I've got several issues regarding this.

  1. For the following byte stream, I get a error thrown, stating: "PowerPoint could not open the file." from part B Line Z. byte data: "0x00000000000000000000" Why is that?
  2. For some runtime instances the following exception is thrown again from Part B Line Y. "Creating an instance of the COM component with CLSID {91493441-5A91-11CF-8700-00AA0060263B} from the IClassFactory failed due to the following error: 80010108". But when I debug using F11 key, this exception is not thrown. Can someone please explain this?
  3. Also for some instances when calling part B, an exception is thrown which states "the powerpoint file is being used by another program/application." When powerpoint is not even running in my taskmanager processes.

Please help me to overcome these barriers. Thanks, Yasindu.

A: 

I found out the reason for the 1st part of my question as well as a solution for my 2nd question.

Q1:
This occurs because the saved bit stream of the ppt file represents a corrupted file. Therefore once created it cannot be opened.

Q2: The error happens when I’m always trying to create a new application instance inside the loop. Therefore, 1. I created the instance at the top of my class and disabled the app.Quit() method call. 2. After closing the power point object, I made sure that the object was destroyed by equaling it to Null.(pptx = null;)

Q3 is still a doubt for me and would be grateful for any expertise help.

Yasindu