不稳定办公室(简报)自动化[复制](Unstable Office(Powerpoint) Auto

2019-10-19 15:04发布

这个问题已经在这里有一个答案:

  • 另类的Visio服务器端自动化 1回答

我工作的一个应用程序,允许用户上传演示文稿,编辑它,然后下载最终输出作为另一个PowerPoint演示文稿。

我有我上传不同的表现非常不稳定的行为:

  1. 有时改变图像模糊(不知道为什么?)

  2. 有时不正确形状IDS返回,因此,我不能合并变更后的工作与现有的PowerPoint形状。

     var shape = (PowerPoint.Shape)item; var shapeid=shape.ID; //this is different from what interop has returned on first presentation read. 
  3. 动画没有得到正确复印(有时他们这样做有时他们不这样做)。

      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; 

我知道的一个事实,即服务器端自动化可能有不稳定的行为或死锁。 但是没有文件究竟什么是关于行为的不稳定。
难道这些行为(上述二)在同一类别还是我失去了一些东西? 如何解决这些问题?

Answer 1:

如果您仍然需要使用互操作,然后尝试释放COM对象和ocasionally杀PowerPoint演示实例如下所述:

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;
        }           
    }


}


文章来源: Unstable Office(Powerpoint) Automation [duplicate]