سؤال

ولدي النصي الذي يفتح باور بوينت من طلبي وتصدير كافة الشرائح. بعد ذلك، أنا في حاجة إلى التطبيق لتكون مغلقة.

ولقد حاولت من دون أي حظ. فأرجو المساعدة؟

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Powerpoint = Microsoft.Office.Interop.PowerPoint;
using Microsoft.Office.Core;
using Microsoft.Office.Interop.PowerPoint;
using System.Runtime.InteropServices;

namespace PresentrBuilder
{


class PowerpointConverter
{

    public static void Convert(String file, String safeFile)
    {
        Powerpoint.Application PP;
        Powerpoint.Presentation Presentation;

        PP = new Powerpoint.ApplicationClass();
        PP.Visible = MsoTriState.msoTrue;
        PP.WindowState = Microsoft.Office.Interop.PowerPoint.PpWindowState.ppWindowMinimized;
        Presentation = PP.Presentations.Open(file, MsoTriState.msoFalse, MsoTriState.msoTrue, MsoTriState.msoTrue);

            // Voor elke slide, exporteren
            String exportSlidesPath = Path.Combine(Properties.Settings.Default.CacheDir, @"presentatienaam1\slides");

            // Kijk of de directory bestaat
            if (!Directory.Exists(exportSlidesPath))
            {
                Directory.CreateDirectory(exportSlidesPath);
            }

                // Kijk of er al bestanden in de directory staan
                // Zo ja: verwijderen
                String[] files = Directory.GetFiles(exportSlidesPath, "*.png");
                if (files.Length > 0)
                {
                    foreach (string fileName in files)
                    {
                        File.Delete(Path.Combine(exportSlidesPath, fileName));
                    }
                }

            // Elke slide exporteren
            foreach (Slide slide in Presentation.Slides)
            {
                slide.Export(Path.Combine(exportSlidesPath, "slide_" + slide.SlideIndex + ".png"), "PNG", 1024, 768);
                Marshal.ReleaseComObject(slide);
            }


        GC.Collect();
        GC.WaitForPendingFinalizers();

        Marshal.ReleaseComObject(PP.Presentations);
        Marshal.ReleaseComObject(Presentation.Slides);

        Presentation.Close();
        Marshal.FinalReleaseComObject(Presentation);

        PP.Quit();
        Marshal.FinalReleaseComObject(PP);

    }

}
}
هل كانت مفيدة؟

المحلول

وانظر النقاش حول نفس الموضوع هنا: ج # والتفوق الأتمتة - إنهاء مثيل قيد التشغيل

ويغطي إكسل، ولكن المبادئ هي نفسها تماما.

موجز: انت بحاجة الى "الافراج" الكائنات Presentations، Slides و(متعددة) Slide. راجع للشغل، وأود أن لا يكلف نفسه عناء وضع المتغيرات لاغية. هذا ليس ضروريا أو مفيدا.

نصائح أخرى

وإذا كان أي شخص آخر يعاني من هذا (عدم القدرة على إغلاق PPT بعد بالتكرار عبر الشرائح)، حتى بعد القيام بجميع جمع القمامة والإفراج عن الموارد، وقضيت أفضل جزء من اليوم خدش رأسي مع هذا واحد. وكان بلدي الحل، بدلا من استخدام foreach تكرار خلال الشرائح، لم أكن كما يلي:

        Microsoft.Office.Interop.PowerPoint.Application powerpoint;
        Microsoft.Office.Interop.PowerPoint.Presentation presentation;
        Microsoft.Office.Interop.PowerPoint.Presentations presentations;

        powerpoint = new Microsoft.Office.Interop.PowerPoint.ApplicationClass();
        powerpoint.Visible = Microsoft.Office.Core.MsoTriState.msoTrue;
        presentations = powerpoint.Presentations;

        presentation = presentations.Open(localPath, MsoTriState.msoFalse, MsoTriState.msoTrue, MsoTriState.msoTrue);


        //String presentationTitle = objPres.Name;
        Microsoft.Office.Interop.PowerPoint.Slides slides = presentation.Slides;
        **for (int i = 1; i <= slides.Count; i++)
        {
            Microsoft.Office.Interop.PowerPoint.Slide slide = slides[i];
            String slideName = slide.Name;
            releaseCOM(slide);
        }**

وهذا هو بلدي طريقة releaseCOM:

    private static void releaseCOM(object o)
    {
        try
        {
            System.Runtime.InteropServices.Marshal.FinalReleaseComObject(o);
        }
        catch { }
        finally
        {
            o = null;
        }
    }

وإضافة إلى الإجابة غاري: من أجل إطلاق سراح مجموعات، لديك لتوزيعها على المتغيرات المؤقتة. (كنت الشرائح والعروض كمتغيرات درجة الحرارة في المثال أدناه).

  // removed using statements...

    namespace PresentrBuilder
    {


      class PowerpointConverter
      {

      public static void Convert(String file, String safeFile)
      {
        Powerpoint.Application PP;
        Powerpoint.Presentation Presentation;

        PP = new Powerpoint.ApplicationClass();
        // ...
        var presentations = PP.Presentations;
           try
            {
        Presentation = presentations.Open(file, MsoTriState.msoFalse, MsoTriState.msoFalse, MsoTriState.msoFalse);

            // Voor elke slide, exporteren
            // ...

            // Elke slide exporteren
            var slides = Presentation.Slides;

            foreach (Slide slide in slides)
            {
                slide.Export(Path.Combine(exportSlidesPath, "slide_" + slide.SlideIndex + ".png"), "PNG", 1024, 768);
                Marshal.ReleaseComObject(slide);
            }
  Marshal.ReleaseComObject(presentations);
        Marshal.ReleaseComObject(slides);

        Presentation.Close();
        Marshal.FinalReleaseComObject(Presentation);
         }
        catch (System.Exception err){}
        finally{

       // GC.WaitForPendingFinalizers();    

        PP.Quit();
        Marshal.FinalReleaseComObject(PP);
        GC.Collect();
      } 
 } } }

ولدي التعليمات البرمجية التي يفتح باور بوينت من تطبيق vb.net بلدي، ويدير عرض الشرائح، ثم يغلق عليه بعد ذلك. كان الألم الحقيقي في مؤخرته للعثور على جميع الكائنات كوم اضطررت لإسقاط بعد ذلك. هنا هو رمز:

Imports MSWord = Microsoft.Office.Interop.Word
Imports MSPowerPt = Microsoft.Office.Interop.PowerPoint
Imports MSExcel = Microsoft.Office.Interop.Excel

Dim MSPowerPtApp As MSPowerPt.Application
Dim MSPowerPtPresentation As MSPowerPt.Presentation
Dim MSPowerPtPresentations As MSPowerPt.Presentations
Dim MSPowerPtSettings As MSPowerPt.SlideShowSettings
Dim MSPowerPtSlideShowWindow As MSPowerPt.SlideShowWindow
Dim MSPowerPtSlideShowWindows As MSPowerPt.SlideShowWindows

Function Display_PowerPoint_Show(ByVal filename As String)
  Dim MSPowerPtSlides As MSPowerPt.Slides
  Display_PowerPoint_Show = True
  Try
    If (Not FileExists(filename)) Then
      Display_PowerPoint_Show = False
      MsgBox("Display_PowerPoint_Show: Text file: " & filename & " not found", MsgBoxStyle.Information, "File Not Found")
      GoTo Exit_Display_PowerPoint_Show
    End If

    MSPowerPtApp = New MSPowerPt.Application
    MSPowerPtApp.Visible = True
    MSPowerPtApp.WindowState = MSPowerPt.PpWindowState.ppWindowMinimized

    'Create a new presentation that is based on the specified template.
    MSPowerPtPresentations = MSPowerPtApp.Presentations
    MSPowerPtPresentation = MSPowerPtPresentations.Open(lbFiles.SelectedValue, True)
    MSPowerPtSlides = MSPowerPtPresentation.Slides

    If (MSPowerPtSlides.Count = 0) Then
      MsgBox("This Powerpoint file has no slides", MsgBoxStyle.Information, "No Slides in File")
      GoTo ClosePowerPointFile
    End If

    MSPowerPtSettings = MSPowerPtPresentation.SlideShowSettings
    MSPowerPtSettings.StartingSlide = 1
    MSPowerPtSettings.EndingSlide = 1

    ''Run the slide show and wait for the slide show to end.
    MSPowerPtSlideShowWindow = MSPowerPtSettings.Run()
    MSPowerPtSlideShowWindows = MSPowerPtApp.SlideShowWindows

    Do While MSPowerPtSlideShowWindows.Count >= 1
      System.Windows.Forms.Application.DoEvents()
    Loop

    'Close the presentation without saving changes and then quit MSPowerPt.
    MSPowerPtPresentation.Saved = True
    MSPowerPtPresentation.Close()

    RemoveComObjRef(MSPowerPtSlideShowWindow)
    RemoveComObjRef(MSPowerPtSlideShowWindows)
    RemoveComObjRef(MSPowerPtSettings)

ClosePowerPointFile:
    RemoveComObjRef(MSPowerPtSlides)
    RemoveComObjRef(MSPowerPtPresentation)
    RemoveComObjRef(MSPowerPtPresentations)

    'Quit MSPowerPt.
    MSPowerPtApp.Quit()
    RemoveComObjRef(MSPowerPtApp)
    GC.Collect()

  Catch ex As Exception
    Display_PowerPoint_Show = False
    MsgBox("Display_PowerPoint_Show - File: " & filename & ", Error: " & ex.Message & " reading file", _
           MsgBoxStyle.Information, "Error Reading File")
  End Try

Exit_Display_PowerPoint_Show:
End Function

'RemoveComObjRef function to remove reference.
Private Sub RemoveComObjRef(ByVal ComObject As Object)
  Try
    System.Runtime.InteropServices.Marshal.ReleaseComObject(ComObject)
  Catch
  Finally
    ComObject = Nothing
  End Try
End Sub

وآمل أن يساعد هذا شخص لتخطي مزيدا من الجهد كنت قد وضعت في.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top