Solved

How to get the title of a presentation of a powerpoint app C#

Posted on 2016-09-23
2
30 Views
Last Modified: 2016-10-12
Hello experts,
My idea is that when I create a presentation a button will be created automatically and when I close it, the button must be removed so I need to get the title of the presentation do you have any idea ?
Here is what I did:
 
using Powerpoint = Microsoft.Office.Interop.PowerPoint;      
Powerpoint.Application wdApp = new Powerpoint.Application();
    string oldCaption = wdApp.Application.Caption ;
    string guid = Guid.NewGuid().ToString();
    //set caption to random value
    wdApp.Application.Caption = guid;
    //create a presentation
    PresentationpptPresentation=wdApp.Presentations.Add(Microsoft.Office.Core.MsoTriState.msoTrue);
    //make sure app is visible:
    wdApp.Visible = true;
    //find random value to get process id
    //here I need to get the title of the presentation
    int processId = GetProcessIdByWindowTitle(guid);

    //reset caption
    wdApp.Application.Caption = oldCaption;

    //create a dictionary
    Dictionary<int, Button> mapping = new Dictionary<int, button>();
    //add mapping
    mapping.Add(new KeyValuePair<int, Button>(processId, deleteButton));







    //found this online:
    /// <summary>
    /// Returns the name of that process given by that title
    /// </summary>
    /// <param name="AppId">Int32MaxValue re
    public static int GetProcessIdByWindowTitle(string AppId)
    {
       Process[] P_CESSES = Process.GetProcesses();
       for (int p_count = 0; p_count < P_CESSES.Length; p_count++)
       {
            if (P_CESSES[p_count].MainWindowTitle.Equals(AppId))
            {
                        return P_CESSES[p_count].Id;
            }
       }

        return Int32.MaxValue;
    }

Open in new window

0
Comment
Question by:SniperCode Sheva
  • 2
2 Comments
 
LVL 17

Accepted Solution

by:
xtermie earned 500 total points (awarded by participants)
ID: 41813458
You can try this code
using System;
using System.Collections.Generic;
using Microsoft.Office.Core;
using PowerPoint = Microsoft.Office.Interop.PowerPoint;

namespace Mintra.Publisher.DocumentConverter.Core.Utils
{
    class InteropUtility
    {


        public static IList<string> GetPresentationTitles(string pptPath)
        {

            IList<string> result = new List<string>();

            var presentationApp = new Microsoft.Office.Interop.PowerPoint.Application();

            try
            {
                presentationApp.Visible = Microsoft.Office.Core.MsoTriState.msoTrue;
                Microsoft.Office.Interop.PowerPoint.Presentations presentations = presentationApp.Presentations;

                var readOnly = Microsoft.Office.Core.MsoTriState.msoTrue;
                var untitled = Microsoft.Office.Core.MsoTriState.msoTrue;
                var withWindow = Microsoft.Office.Core.MsoTriState.msoFalse;

                Microsoft.Office.Interop.PowerPoint.Presentation presentation = presentations.Open(pptPath, readOnly, untitled, withWindow);
                int i = 0;
                foreach (PowerPoint.Slide slide in presentation.Slides)
                {
                    string defaultTitle = String.Format("Slide {0}", i);
                    String shapeTitle = ExtractSlideTitlefromShape(slide, defaultTitle);
                    result.Add(shapeTitle);
                }
            }
            finally
            {
                presentationApp.Quit();
            }


            return result;

        }

        private static string ExtractSlideTitlefromShape(PowerPoint.Slide slide, string defaultValue)
        {
            PowerPoint.HeadersFooters headersFooters = slide.HeadersFooters;
            PowerPoint.Shapes mastershapes = slide.Master.Shapes;

            for (int i = 1; i <= slide.Shapes.Count; i++)
            {
                PowerPoint.Shape shape = slide.Shapes[i];
                bool hasTextFrame = shape.HasTextFrame == MsoTriState.msoTrue;
                bool isTypePlaceholder = shape.Type.Equals(MsoShapeType.msoPlaceholder);
                bool hasTextInTextFrame = shape.TextFrame.HasText == MsoTriState.msoTrue;
                bool isTitleShape = shape.Name.ToLower().Contains("title");

                if (isTypePlaceholder && hasTextFrame && hasTextInTextFrame && isTitleShape)
                {
                    return shape.TextFrame.TextRange.Text;

                }
            }

            return defaultValue;
        }

    }
}

Open in new window

Or try saving as ppptx and extract titles from the xml that goes with it
0
 
LVL 17

Expert Comment

by:xtermie
ID: 41839770
Solution provided works
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Recently Microsoft released a brand new function called CONCAT. It's supposed to replace its predecessor CONCATENATE. But how does it work? And what's new? In this article, we take a closer look at all of this - we even included an exercise file for…
Technology opened people to different means of presenting information, but PowerPoint remains to be above competition. Know why PPT still works today.
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now