C# 不稳定办公(Powerpoint)自动化

C# 不稳定办公(Powerpoint)自动化,c#,asp.net,powerpoint,office-interop,powerpoint-automation,C#,Asp.net,Powerpoint,Office Interop,Powerpoint Automation,我正在开发一个应用程序,允许用户上传演示文稿,编辑它,然后将最终输出作为另一个PowerPoint演示文稿下载 我上传的不同演示文稿的行为非常不稳定: 有时更改后的图像会模糊(不确定原因?) 有时会返回不正确的形状ID,因此我无法将更改的工作与现有PowerPoint形状合并 var shape = (PowerPoint.Shape)item; var shapeid=shape.ID; //this is different from what interop has returned on

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

我上传的不同演示文稿的行为非常不稳定:

  • 有时更改后的图像会模糊(不确定原因?)

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

    var shape = (PowerPoint.Shape)item;
    var shapeid=shape.ID; //this is different from what interop has returned on first presentation read.
    
  • 动画复制不正确(有时复制,有时不复制)

  • 我知道服务器端自动化可能有不稳定的行为或死锁。但是,没有任何文件确切记录该行为的不稳定性。

    这些行为(以上两种)是属于同一类别还是我遗漏了什么?如何解决这些问题?

    如果仍然需要使用Interop,请尝试释放COM对象并局部删除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;
            }           
        }
    
    
    }
    

    “不稳定的办公自动化”是一个同义词:(我想我们需要更多地了解您的代码在做什么来指出潜在的修复。@JoelCoehoorn:谢谢您的编辑。正如我已经提到的我遇到的许多不稳定行为。我甚至会添加将动画从现有形状复制到新创建形状的部分。从ASP.NET或其他版本使用Office Interop是一个可怕的想法服务器技术。这些API是为在桌面应用程序中使用而编写的,用于自动化Office(一套桌面应用程序).Server应用程序在许多方面都不同,因此在其中使用Office Interop是一个非常非常糟糕的主意。Microsoft也不支持它,并且可能会违反您的Office许可证。请参阅,除非您要求应用程序成为不稳定的垃圾,否则我建议您停止使用Office Interop from服务器应用程序。微软已经告诉你“不”,你已经看到它“无处不在”,那么怎样才能让你意识到你犯了一个严重的错误呢?
    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;
            }           
        }
    
    
    }