不稳定的Office(Powerpoint)自动化 [英] Unstable Office(Powerpoint) Automation

查看:169
本文介绍了不稳定的Office(Powerpoint)自动化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序,该应用程序将允许用户上载演示文稿,对其进行编辑,然后将最终输出下载为另一个PowerPoint演示文稿.

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. 有时更改的图像模糊(不确定为什么?)

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

有时返回不正确的形状ID,因此我无法将更改的工作与现有的PowerPoint形状合并.

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.

  • 动画没有被正确复制(有时它们有时没有被复制).

  • 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?

    推荐答案

    如果您仍然需要使用Interop,请尝试释放COM对象并按如下所述逐个杀死PowerPoint实例:

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

    这篇关于不稳定的Office(Powerpoint)自动化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆