Unstable Office(Powerpoint) Automation [duplicate]

南笙酒味 提交于 2019-12-11 06:58:44

问题


I am working on an app that will allow the user to upload a presentation, edit it, and then download the final output as another PowerPoint presentation.

I have very unstable behavior for different presentations that I upload:

  1. Sometimes the changed images are blurred (Not sure why?)

  2. Sometimes incorrect shape ids are returned, and therefore I can not merge the changed work with the existing PowerPoint shape.

    var shape = (PowerPoint.Shape)item;
    var shapeid=shape.ID; //this is different from what interop has returned on first presentation read.
    
  3. Animations are not getting copied properly(sometimes they do sometimes they do not).

          newshape.AnimationSettings.EntryEffect = oldshape.AnimationSettings.EntryEffect;
          newshape.AnimationSettings.AdvanceMode=oldshape.AnimationSettings.AdvanceMode;        
          newshape.AnimationSettings.AdvanceTime=oldshape.AnimationSettings.AdvanceTime;
          newshape.AnimationSettings.AfterEffect=oldshape.AnimationSettings.AfterEffect;
          newshape.AnimationSettings.Animate=oldshape.AnimationSettings.Animate;
          newshape.AnimationSettings.AnimateBackground = oldshape.AnimationSettings.AnimateBackground;
          newshape.AnimationSettings.TextLevelEffect = PowerPoint.PpTextLevelEffect.ppAnimateByAllLevels;
          newshape.AnimationSettings.AnimateTextInReverse=oldshape.AnimationSettings.AnimateTextInReverse;
    

I am aware of the fact that server side automation may have unstable behavior or deadlock. However nothing documents exactly what is unstable about the behavior.
Are these behaviors (above two) in same category or am I missing something here? How can I fix these issues?


回答1:


If you still need to use Interop then try to release the COM objects and ocasionally kill the PowerPoint instances as mentioned below:

public static class PowerPointInterOp
{
    static PowerPoint.Application powerPointApp = null;
    static Object oMissing = System.Reflection.Missing.Value;
    static Object oTrue = true;
    static Object oFalse = false;
    static Object oCopies = 1;

    public static void InitializeInstance()
    {
        if (powerPointApp == null)
        {
            powerPointApp = new PowerPoint.ApplicationClass();

        }           
    }

    public static void KillInstances()
    {
        try
        {
            Process[] processes = Process.GetProcessesByName("POWERPNT");
            foreach(Process process in processes)
            {
                process.Kill();
            }
        }
        catch(Exception)
        {

        }
    }

    public static void CloseInstance()
    {
        if (powerPointApp != null)
        {
            powerPointApp.Quit();
            System.Runtime.InteropServices.Marshal.ReleaseComObject(powerPointApp);
            powerPointApp = null;
        }
    }

    public static PowerPoint.Presentation OpenDocument(string documentPath)
    {
        InitializeInstance();

        PowerPoint.Presentation powerPointDoc = powerPointApp.Presentations.Open(documentPath, MsoTriState.msoTrue, MsoTriState.msoTrue, MsoTriState.msoFalse);

        return powerPointDoc;
    }

    public static void CloseDocument(PowerPoint.Presentation powerPointDoc)
    {
        if (powerPointDoc != null)
        {
            powerPointDoc.Close();
            System.Runtime.InteropServices.Marshal.ReleaseComObject(powerPointDoc);
            powerPointDoc = null;
        }           
    }


}


来源:https://stackoverflow.com/questions/22508145/unstable-officepowerpoint-automation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!