I am currently trying to change some settings in a .pptx files via C# and Microsoft.Office.Interop.PowerPoint. I have some .wmv movies linked on several slides of the presentation. At the time the presentations were created, all movies play as soon as they are clicked. However, I want to change this to start automatically playing as soon as the slide is viewed. This this has to be done to a lot of presentations, so there is no way to do this manually.
I found the PlaySettings.PlayOnEntry property, but I can't figure out how to use it. I found several examples how to do this with a new movie to be embedded (and then, only for Visual Basic), but since the movies are already embedded, this is not what I want.
I also have no idea how I can actually access any objects on the current slide, maybe there is a way to check if a shape is a video-file and then change above setting, but the MSDN-Reference is not very helpful on Office-Topics. I'm using Powerpoint 2007 and Visual Studio 2010 if that matters.
#Lennart's solution is part of it, you then need a page trigger
var videoAnimation = slide.TimeLine.MainSequence.FindFirstAnimationFor(objShapes);
if (videoAnimation != null)
{
videoAnimation.Timing.TriggerType = PowerPoint.MsoAnimTriggerType.msoAnimTriggerWithPrevious;
}
Got it. Searching through all shapes of the Presentation and filtering out the movies works:
//While iterating through all slides i:
objShapes = objPres.Slides[i].Shapes;
foreach (Microsoft.Office.Interop.PowerPoint.Shape s in objShapes) {
if(s.Name.Contains(".wmv")){
s.AnimationSettings.PlaySettings.PlayOnEntry = MsoTriState.msoTrue;
}
}
Related
I am trying to copy slides from one PowerPoint presentation to another. I have used the procedure outlined in the following article, and it generally works fine.
https://learn.microsoft.com/en-us/previous-versions/office/developer/office-2007/ee361883(v=office.12)?redirectedfrom=MSDN
However, when the slide to be copied contains notes, the resulting presentation after copying is corrupted. I've noticed that the code generates a new notesMaster which is not added to the notesMasterIdLst in presentation.xml, and have a suspicion this might be the issue. However, I cannot add the new notes master to the presentation, as a presentation can only have one notesMaster.
According to the Microsoft Documentation, Open XML SDK is defined this way:
The Open XML SDK 2.5 simplifies the task of manipulating Open XML
packages and the underlying Open XML schema elements within a package.
The Open XML SDK 2.5 encapsulates many common tasks that developers
perform on Open XML packages, so that you can perform complex
operations with just a few lines of code.
It looks like it is not easy to solve your problem using the Open XML SDK. If you use Aspose.Slides for .NET you will copy a slide with its notes as shown below:
var sourceFileName = "example1.pptx";
var targetFileName = "example2.pptx";
var slideIndex = 0;
using (var sourcePresentation = new Presentation(sourceFileName))
using (var targetPresentation = new Presentation(targetFileName))
{
var slide = sourcePresentation.Slides[slideIndex];
targetPresentation.Slides.AddClone(slide);
targetPresentation.Save(targetFileName, SaveFormat.Pptx);
}
You can also evaluate Aspose.Slides Cloud for presentation manipulating. This REST-based API allows you to make 150 free API calls per month for API learning and presentation processing. The following code example shows you how to do the same using Aspose.Slides Cloud:
var slidesApi = new SlidesApi("my_client_id", "my_client_key");
var sourceFileName = "example1.pptx";
var targetFileName = "example2.pptx";
var slideIndex = 1;
using (var sourceStream = File.OpenRead(sourceFileName))
slidesApi.UploadFile(sourceFileName, sourceStream);
using (var targetStream = File.OpenRead(targetFileName))
slidesApi.UploadFile(targetFileName, targetStream);
slidesApi.CopySlide(targetFileName, slideIndex, null, sourceFileName);
using (var resultStream = slidesApi.DownloadFile(targetFileName))
using (var fileStream = File.OpenWrite(targetFileName))
resultStream.CopyTo(fileStream);
I work as a Support Developer at Aspose.
I believe I managed to solve this issue by doing the following steps:
Make the source presentations editable when opening them:
using (PresentationDocument mySourceDeck =
PresentationDocument.Open(
presentationFolder + sourcePresentation, true))
{
PresentationPart sourcePresPart =
mySourceDeck.PresentationPart;
Copy the notes slide CommonSlideData from the slide, then delete the notes slide part from the slide:
sp = (SlidePart)sourcePresPart.GetPartById(slideId.RelationshipId);
CommonSlideData notesSlideData = null;
if (sp.NotesSlidePart != null)
{
notesSlideData = (CommonSlideData)sp.NotesSlidePart.NotesSlide.CommonSlideData.CloneNode(true);
sp.DeletePart(sp.NotesSlidePart);
}
Readd any existing notes slide data by adding a new NotesSlidePart to the copied slide (now added to the target presentation and called destSp), adding relationship parts and a new NotesSlide object initialised with the copied notes slide data.
if (notesSlideData != null)
{
NotesSlidePart notesSlidePart1 = destSp.AddNewPart<NotesSlidePart>();
notesSlidePart1.AddPart(destSp);
notesSlidePart1.AddPart(destPresPart.NotesMasterPart);
NotesSlide notesSlide = new NotesSlide(notesSlideData);
notesSlidePart1.NotesSlide = notesSlide;
}
Warning: The notes slides will be deleted from the source presentation files, so you might want to make a copy of them first, or add them back to the presentation after it has been copied/merged.
This seems to retain at least some existing formatting of the notes slides, such as bold text. However, I have not yet tested this on a lot of different presentations so I suppose there could be some issues if the notes slides are based on very different notes slide masters, but I'm not sure.
Related to this, I ran into a similar issue after getting the notes slides to work, which seemed to be because of any custom xml parts that existed on the presentation to be copied. These presentations worked after adding some code to add relationships to the copied presentation's CustomXmlPart to the target presentation:
foreach (var customXmlPart in destSp.GetPartsOfType<CustomXmlPart>())
{
destPresPart.AddPart(customXmlPart);
}
I have been struggling with C# and PowerPoint, I posted earlier about how to update Links in ppt using C# if the links were set to manual, I didn't get any responses so I figured I would try to circumvent the issue by setting the links to automatic in the file, then when C# opens it it updates them, saves the file, and then breaks them and saves it as another file name, but that hasn't proven any easier.
All I need to know is how to break the links. I know some VBA and wrote a code to break them, but when I call a macro in C# with a RunMacro method it doesn't seem to be working with the method I am using (? - I'm new to C# so I don't fully understand why this is, but if you Google "run macro PowerPoint C#" you will find the way I am sure I tried to go about it.) Please help, I am at a complete loss.
My script looks something like this
Using PowerPoint = Microsoft.Office.Interop.PowerPoint;
public void Main()
{
PowerPoint.Application ppt = new PowerPoint.Application();
PowerPoint.Presentation PRS = ppt.Presentations.Open(#"Filename.pptm",
Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoTrue);
PRS.UpdateLinks();
PRS.Save
//here is where I need to break the links
PRS.SaveAs(#"filename with links broken.pptm",
Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsOpenXMLPresentationMacroEnabled, MsoTriState.msoTrue);
PRS.Close();
ppt.Quit();
}
I tried setting the linkformat to manual before opening the file, but that doesn't affect any shapes that are already created, only new ones created within the program afterwards.
I have done a similar project in Powerpoint which also involves breaking links. In my program I am reading from an Excel file and take this data and put it into a Powerpoint presentation. In my Powerpoint template file I have all of my links to my Excel file laid out and formatted the way that I want it to look. The program begins to run and this fills the Excel file. After the writing to Excel is finished, I open up Powerpoint and UpdateLinks() to my presentation. Once the links are updated I break the links using a for loop on the Shapes in the Powerpoint. After, I save and close the document. Below is my function that I use to create my Powerpoint file based off of each of my ppt file templates (for my example I iterate through multiple ppt files because each ppt file contains only one slide. They are all combined into one slideshow later in the process).
public void CreatePowerpoint()
{
string[] fileArray = Directory.GetFiles(#"C:\Users\Me\Desktop\test");
Microsoft.Office.Interop.PowerPoint.Application pptApp = new Microsoft.Office.Interop.PowerPoint.Application();
for (int i = 0; i < fileArray.Length; i++)
{
string file = fileArray[i];
Microsoft.Office.Interop.PowerPoint.Presentation powerpoint = pptApp.Presentations.Open(file);
powerpoint.UpdateLinks();
Microsoft.Office.Interop.PowerPoint.Slides slides = powerpoint.Slides;
Microsoft.Office.Interop.PowerPoint.Slide slide = slides[1];
Microsoft.Office.Interop.PowerPoint.Shapes pptShapes = (Microsoft.Office.Interop.PowerPoint.Shapes)slide.Shapes;
foreach (Microsoft.Office.Interop.PowerPoint.Shape y in pptShapes)
{
Microsoft.Office.Interop.PowerPoint.Shape j = (Microsoft.Office.Interop.PowerPoint.Shape)y;
try
{
//If auto link update is disabled for links
//j.LinkFormat.j.Update();
if (j.LinkFormat != null)
{
j.LinkFormat.BreakLink();
}
}
catch (Exception)
{
}
}
powerpoint.SaveAs(#"C:\Users\Me\Desktop\test" + i + ".pptx");
powerpoint.Close();
Thread.Sleep(3000);
}
pptApp.Quit();
}
Changing things before opening a file will have no effect on files that aren't open, of course. Iterate through the shapes collection on each slide and for each shape:
If .Type = msoLinkedOLEObject Then
.LinkFormat.BreakLink
End If
msoLinkedOLEObject = 10
I've spent three weeks googling it, and I -have- found snippets, but none of them have been useful. I'm trying to write a c# program that is able to view a powerpoint. I have no idea how. I've looked through SO many snippets and the MSDN pages in the Ppt interop and I'm completely at a loss. Has anybody done this, or knows how and would quickly demonstrate code showing me how to do this? As little code as needed to get it working would be preferrable. What I'm going for is this: A powerpoint slide is going to be displayed in a picturebox and every 40 seconds it's going to switch to the next slide. I am NOT allowed to export the powerpoints to pictures, and load the pictures (Which I couldn't figure out how to do, either). If anybodys wondering, yes this IS for a programming class, however it's not a graded assignment. I've spent three weeks on this and it's driving me insane. Any help at all would be appreciated. Thank you. If it helps, here's all the working code I've come up with on my own so far, and it's nothing like what I'm trying to accomplish.
PowerPoint.Application oPPT;
PowerPoint.Presentations objPresSet;
const string strPres = #"E:\C#\Ch 16\PP Stuff\TestTextBox\TextBoxTestWithArrays\TextBoxTestWithArrays\Ad Analysis.pptx";
const string myPath = #"E:\C#\Ch 16\PP Stuff\TestTextBox\TextBoxTestWithArrays\TextBoxTestWithArrays\";
{
oPPT = new PowerPoint.Application();
oPPT.Visible = Microsoft.Office.Core.MsoTriState.msoTrue;
objPresSet = oPPT.Presentations;
objPresSet.Open(strPres, MsoTriState.msoFalse, MsoTriState.msoTrue, MsoTriState.msoTrue);
/*
* This crashes, and I'm clueless as to why it wouldn't work. I wanted it here
* to check the # of msg boxes that opened against the # of slides in the ppt.
*/
foreach (PowerPoint.Slides mySlides in objPresSet)
MessageBox.Show("{0}", mySlides.ToString());
}
Have a look at http://skp.mvps.org/vb/pptvbwnd.htm
It explains how to display and control a presentation on form in VB5/6, but might give you some ideas.
Here is my situation,
I have an ESRI Map Silverlight application that needs to display ShapeFiles that were given to me the client.
The only third party library I've found that will allow you to do this is the ESRI Silverlight API Contrib. The example they give is to use an Open File Dialog to select the shape files and to load them into a FileInfo classes to show. (See example on site's frontpage).
However I run into the issue that since it is a Silverlight app, any attempt to instantiate an instance of a FileInfo object causes the app to crash.
So my question is, is there a way for me to load/display shape files that I have saved locally to the app in Silverlight?
Please let me know if you need me to give more info.
Thanks in advance!
Code:
FileInfo runwayShp = new FileInfo("Layers\\Runway.shp"); //This line breaks, says file access is denied.
FileInfo runwayDbf = new FileInfo("Layers\\Runway.dbf");
ShapeFile shapeFileReader = new ShapeFile();
if (runwayShp != null && runwayDbf != null)
{
shapeFileReader.Read(runwayShp, runwayDbf);
}
GraphicsLayer graphicsLayer = MyMap.Layers["ShapeLayer"] as GraphicsLayer;
foreach (ShapeFileRecord record in shapeFileReader.Records)
{
Graphic graphic = record.ToGraphic();
if (graphic != null)
graphicsLayer.Graphics.Add(graphic);
}
}
I have a silveright app that is doing pretty much the same thing, but what I am doing is uploading the shapefile as a blob to a SQL db on the back end, and then grabbing it from there.
for what you are trying to do, you should look at this codeplex project. I think it will help you out.
I'd like to be able to advance through a Powerpoint presentation by pressing buttons in a Windows form. Here's some code I've found from http://bytes.com/topic/c-sharp/answers/272940-open-powerpoint-presentation-c-window-form that opens a Powerpoint presentation slide show:
Microsoft.Office.Interop.PowerPoint.Application oPPT;
Microsoft.Office.Interop.PowerPoint.Presentations objPresSet;
Microsoft.Office.Interop.PowerPoint.Presentation objPres;
//the location of your powerpoint presentation
string strPres = #"filepath";
//Create an instance of PowerPoint.
oPPT = new Microsoft.Office.Interop.PowerPoint.ApplicationClass();
// Show PowerPoint to the user.
oPPT.Visible = Microsoft.Office.Core.MsoTriState.msoTrue;
objPresSet = oPPT.Presentations;
//open the presentation
objPres = objPresSet.Open(strPres, MsoTriState.msoFalse,
MsoTriState.msoTrue, MsoTriState.msoTrue);
objPres.SlideShowSettings.Run();
I haven't found any methods that can advance through the slides, however. Any ideas?
(Really what I'm trying to do is use the WiiRemote to advance the slides, for a student project).
The method to advance programatically is "SlideShowWindow.View.Next". You can also use "SlideShowWindow.View.Previous" to go backwards.
Looks like there is a method called GotoSlide that will work, just needed to dig a little more! For instance:
int i = 0;
while (true)
{
i++;
objPres.SlideShowWindow.View.GotoSlide(i, MsoTriState.msoFalse);
System.Threading.Thread.Sleep(5000);
}
I think you could do this to run them one at a time, for instance:
oSettings = objPres.SlideShowSettings
oSettings.StartingSlide = 3
oSettings.EndingSlide = 3
oSettings.Run()
Do While oApp.SlideShowWindows.Count >= 1
System.Windows.Forms.Application.DoEvents()
Loop
You might have get a answer for you question, However for the people who will face the same kind of problem I will send a link for a opensource C# code which handles this kind of situation smoothly.
https://code.msdn.microsoft.com/office/How-to-Automate-control-23cd2a8f
Download the zip file, inside there is a c# project which controls power point slides perfectly
Best wishes folks.