How to zoom a powerpoint file with c#? - c#

I'm using Microsoft.Office.Interop.PowerPoint library to open and view a PowerPoint file. I'm opening the file as an SlideShow without problems. The code to open the file is the following:
Microsoft.Office.Interop.PowerPoint.Application _pptApp;
Microsoft.Office.Interop.PowerPoint.Presentation _ppt;
Microsoft.Office.Interop.PowerPoint.Presentations _pptPresentations;
Microsoft.Office.Interop.PowerPoint.SlideShowWindow _pptSlideShowWindow;
private void Open(string fileName)
{
_pptApp = new Microsoft.Office.Interop.PowerPoint.Application();
_pptPresentations = _pptApp.Presentations;
//fileName represents the full path of the file
_ppt = _pptPresentations.Open(fileName, Microsoft.Office.Core.MsoTriState.msoTrue,
Microsoft.Office.Core.MsoTriState.msoTrue,Microsoft.Office.Core.MsoTriState.msoFalse);
_ppt.SlideShowSettings.ShowPresenterView = Microsoft.Office.Core.MsoTriState.msoFalse;
_ppt.SlideShowSettings.ShowType = Microsoft.Office.Interop.PowerPoint.PpSlideShowType.ppShowTypeSpeaker;
_pptSlideShowWindow = _ppt.SlideShowSettings.Run();
}
The code above works fine. With the _pptSlideShowWindow I can do some operations, like go to next slide _pptSlideShowWindow.View.Next(); or go to previous slide _pptSlideShowWindow.View.Previous();.
I would like to Zoom in and zoom out the presentations while it is on SlideShow. I've been looking around and I haven't seen any solution that works for me. The SlideShowView has a Zoom property but it's read only:
http://msdn.microsoft.com/en-us/library/microsoft.office.interop.powerpoint.slideshowview.zoom.aspx
I've tried not to open as SlideShow but the result is not good, as I haven't managed to open the file in reading mode programatically.
I'm a bit lost with this. Any help with this will be really appreciated.
Regards,
Pau

Related

PieChart.SaveImage(path,imageFormate) show exception on saving image 2nd time

I am using Chart Control of .NET 4.0 framework in Windows Forms Application i have been saving Pie Chart image on location through PieChart.SaveImage(Path,ChartImageFormat.Png), when i create doc file with Microsoft.Office.Interop.Word i paste that image in that document. it proceed first time very well and .doc created successfully, but i try to save pie chart 2nd time during win forms Running it give an System.IO.Exception
"The process cannot access the file 'path' because it is being used by
another process."
When i terminate program and run it again it over wright previous image but when i want to save image 2nd time during program running it gives same Exception
This is how i am saving image
private Void SavePieChart()
{
string PieChartPath= Application.StartupPath + #"\Chart.png";
PieChart.SaveImage(PieChartPath, ChartImageFormat.Png);
}
I searched, but did not find any efficient solution which solve my problem,
If anything doing wrong Kindly Point out my mistake, or any helping link to solve this. .
EDIT 1
This is where I am pasting that image in Doc file
System.Drawing.Image PieChart =System.Drawing.Image.FromFile(PieChartPath);
oHeader1 = oDoc.Content.Paragraphs.Add(ref oMissing);
Logothread = new Thread(() => Clipboard.SetImage(PieChart));
Logothread.SetApartmentState(ApartmentState.STA); //Set the thread to STA
Logothread.Start();
Logothread.Join();
oHeader1.Range.Paste();
oHeader1.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
oHeader1.Range.InsertParagraphAfter();
Thanks in advance
Problem is when i wast pasting image in doc file i was take image like
System.Drawing.Image PieChart =System.Drawing.Image.FromFile(PieChartPath);
as Reza and Taw described FromFile() keeps the file in use so that's why when i try to save image 2nd time it shows exception the file is already in process,
The i use FromStram() add this into my code
byte[] DataBytes= System.IO.File.ReadAllBytes(PieChartPath);
System.IO.MemoryStream ms = new System.IO.MemoryStream(DataBytes);
System.Drawing.Image PieChart = System.Drawing.Image.FromStream(ms);

Is it possible to convert a PPT to a video file with Interop Services without having PowerPoint installed?

Everything I've read on the matter says that PowerPoint has to be installed and ready to launch when using the Interop Services in a C# script to convert a PPT to a video file. Obviously I'd much rather PowerPoint not to be required for this script to successfully effect its conversion. Below is my code. Any advice is greatly appreciated:
Microsoft.Office.Interop.PowerPoint.Application ppApp = new Microsoft.Office.Interop.PowerPoint.Application();
ppApp.Visible = MsoTriState.msoTrue;
ppApp.WindowState = Microsoft.Office.Interop.PowerPoint.PpWindowState.ppWindowMinimized;
Microsoft.Office.Interop.PowerPoint.Presentations oPresSet = ppApp.Presentations;
Microsoft.Office.Interop.PowerPoint._Presentation oPres = oPresSet.Open(fileName,
MsoTriState.msoFalse, MsoTriState.msoFalse,
MsoTriState.msoFalse);
try
{
oPres.CreateVideo(exportPath);
}

Break Links PowerPoint 2010

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

C# MS Office PPT to Image conversion

I am a beginner with C# and windows programming. I have written a service which converts a powerpoint file to individual slide images using the slide.export method that the microsoft.office.interop.powerpoint libraries provide. I am able to get all the slides but some of them seem to be broken and i see a "Image cannot be displayed. Memory exceeded" or the likes of it. I thought it was insufficient memory and then just tried it with a ppt having one slide(whose image was broken) and to my dismay I found the single image to be broken as well.
Am I using export wrongly or should I pass different arguments than I am passing already? I will paste the code below.
Microsoft.Office.Interop.PowerPoint.Application appPpt
= new Microsoft.Office.Interop.PowerPoint.Application();
Microsoft.Office.Interop.PowerPoint.Presentation objActivePresentation
= appPpt.Presentations.Open(strFilePath,
Microsoft.Office.Core.MsoTriState.msoCTrue,
Microsoft.Office.Core.MsoTriState.msoTriStateMixed,
Microsoft.Office.Core.MsoTriState.msoFalse);
foreach (Microsoft.Office.Interop.PowerPoint.Slide objSlide
in objActivePresentation.Slides)
{
//Names are generated based on timestamp.
objSlide.Export(slideName, "PNG", 960, 720);
objSlide.Export(slideNameMedium, "JPG", 307, 231);
objSlide.Export(slideNametn, "JPG", 150, 113);
}
I'm in need of help here. Thanks in advance.
try SaveAs.
Hope it helps.....

C# - way to programmatically advance Powerpoint slide show?

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.

Categories

Resources