Powerpoint Automation

M

Mike Weston

Hi

I have written a couple of small programs which update text values on some
slides, for example a time... this value is update evrey second.
My first test application was written in Delphi and my second application
was in C# both seem to exhibit the same problems in the sense that
Powerpoint (2000) leaks some memory and also after a while powerpoint hangs!

Does anyone know if it is possible to do this reliably?

There is some example C# code below.

Regards

Mike


variables
private PowerPoint.ApplicationClass ppApp = new
PowerPoint.ApplicationClass();

private PowerPoint.Presentation ppPres;


initial connect code!
ppApp.Presentations.Open("c:\\pptest\\softboard.ppt",0,0,0);

ppPres = ppApp.Presentations.Item(1);

ppPres.SlideShowSettings.Run();



tidy up at end!

ppApp.Quit();



Round my loop!

PowerPoint.Slide S;

PowerPoint.Shape UpdateShape;

DateTime DT;

for (int i = 1; i <= ppPres.Slides.Count; i++)

{

S = ppPres.Slides.Item(i);

for (int j = 1; j <= S.Shapes.Count; j++)

{

UpdateShape = S.Shapes.Item(j);

if (UpdateShape.Name.IndexOf("SETIME") > -1)

{

DT = DateTime.Now;

UpdateShape.TextFrame.TextRange.Text = DT.ToString("T");

}
 
P

Peter Huang [MSFT]

Hi Mike,

When you exit the PowerPoint SlideShow, your loop to setting the time is
running while the instance of Slide S has gone away, which may cause the
program to crash. To work around the problem, you may handle the
SlideShowEnd event to stop the loop.
Here is link about handle PowerPoint Events in C# .NET
KB HOWTO: Handle PowerPoint Events With Visual C# .NET
http://support.microsoft.com/?id=308825

Here is a sample code, you may have a try and let me if it does the job for
you.

using System.Runtime.InteropServices;

private static PowerPoint.ApplicationClass ppApp;
private UCOMIConnectionPoint m_oConnectionPoint;
private int m_Cookie;
public static ManualResetEvent allDone = new ManualResetEvent(false);
private static bool flag; // to indicate whether to continue the
time-setting loop
private void Form1_Load(object sender, System.EventArgs e)
{
ppApp = new PowerPoint.ApplicationClass();
// QI for IConnectionPointContainer.
UCOMIConnectionPointContainer oConnPointContainer =
(UCOMIConnectionPointContainer) ppApp;
// Get the GUID of the EApplication interface.
Guid guid=typeof(PowerPoint.EApplication).GUID;

// Find the connection point.
oConnPointContainer.FindConnectionPoint(ref guid,out m_oConnectionPoint);
// Call Advise to sink up the connection.
m_oConnectionPoint.Advise(this,out m_Cookie);
flag = true;
}
[DispId(2014)]
public void SlideShowEnd(PowerPoint.Presentation Pres)
{
flag = false;
allDone.WaitOne();
m_oConnectionPoint.Unadvise(m_Cookie);
ppApp.Presentations.Item(1).Save();
ppApp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(ppApp);
GC.Collect();
GC.WaitForPendingFinalizers();
}

private static void RunThread()
{
PowerPoint.Presentation ppPres;
ppApp.Presentations.Open("c:\\pptest\\softboard.ppt",0,0,0);
ppPres = ppApp.Presentations.Item(1);
ppPres.SlideShowSettings.Run();
PowerPoint.Slide S;
PowerPoint.Shape UpdateShape;
DateTime DT;
while(flag)
{
for (int i = 1; i <= ppPres.Slides.Count; i++)
{
S = ppPres.Slides.Item(i);
for (int j = 1; j <= S.Shapes.Count; j++)
{
UpdateShape = S.Shapes.Item(j);
if (UpdateShape.Name.IndexOf("Rectangle 3") > -1)
{
DT = DateTime.Now;
UpdateShape.TextFrame.TextRange.Text = DT.ToString("T");
}
}
}
}
allDone.Set();
}
private void button1_Click(object sender, System.EventArgs e)
{
Thread wt = new Thread(new ThreadStart(RunThread));
wt.Start();
wt.Priority = ThreadPriority.BelowNormal;
}


Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
 
M

Mike Weston

I have upgraded to SP3 to see if this solves my problems, the problem does
not occur on shutdown.

Regards

Mike

Peter Huang said:
Hi Mike,

When you exit the PowerPoint SlideShow, your loop to setting the time is
running while the instance of Slide S has gone away, which may cause the
program to crash. To work around the problem, you may handle the
SlideShowEnd event to stop the loop.
Here is link about handle PowerPoint Events in C# .NET
KB HOWTO: Handle PowerPoint Events With Visual C# .NET
http://support.microsoft.com/?id=308825

Here is a sample code, you may have a try and let me if it does the job for
you.

using System.Runtime.InteropServices;

private static PowerPoint.ApplicationClass ppApp;
private UCOMIConnectionPoint m_oConnectionPoint;
private int m_Cookie;
public static ManualResetEvent allDone = new ManualResetEvent(false);
private static bool flag; // to indicate whether to continue the
time-setting loop
private void Form1_Load(object sender, System.EventArgs e)
{
ppApp = new PowerPoint.ApplicationClass();
// QI for IConnectionPointContainer.
UCOMIConnectionPointContainer oConnPointContainer =
(UCOMIConnectionPointContainer) ppApp;
// Get the GUID of the EApplication interface.
Guid guid=typeof(PowerPoint.EApplication).GUID;

// Find the connection point.
oConnPointContainer.FindConnectionPoint(ref guid,out m_oConnectionPoint);
// Call Advise to sink up the connection.
m_oConnectionPoint.Advise(this,out m_Cookie);
flag = true;
}
[DispId(2014)]
public void SlideShowEnd(PowerPoint.Presentation Pres)
{
flag = false;
allDone.WaitOne();
m_oConnectionPoint.Unadvise(m_Cookie);
ppApp.Presentations.Item(1).Save();
ppApp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(ppApp);
GC.Collect();
GC.WaitForPendingFinalizers();
}

private static void RunThread()
{
PowerPoint.Presentation ppPres;
ppApp.Presentations.Open("c:\\pptest\\softboard.ppt",0,0,0);
ppPres = ppApp.Presentations.Item(1);
ppPres.SlideShowSettings.Run();
PowerPoint.Slide S;
PowerPoint.Shape UpdateShape;
DateTime DT;
while(flag)
{
for (int i = 1; i <= ppPres.Slides.Count; i++)
{
S = ppPres.Slides.Item(i);
for (int j = 1; j <= S.Shapes.Count; j++)
{
UpdateShape = S.Shapes.Item(j);
if (UpdateShape.Name.IndexOf("Rectangle 3") > -1)
{
DT = DateTime.Now;
UpdateShape.TextFrame.TextRange.Text = DT.ToString("T");
}
}
}
}
allDone.Set();
}
private void button1_Click(object sender, System.EventArgs e)
{
Thread wt = new Thread(new ThreadStart(RunThread));
wt.Start();
wt.Priority = ThreadPriority.BelowNormal;
}


Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
From: "Mike Weston" <[email protected]>
Subject: Powerpoint Automation
Date: Fri, 15 Aug 2003 11:09:56 +0100
Lines: 72
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
Message-ID: <[email protected]>
Newsgroups: microsoft.public.office.developer.automation
NNTP-Posting-Host: 212.42.175.93
Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP12.phx.gbl
Xref: cpmsftngxa06.phx.gbl microsoft.public.office.developer.automation:7170
X-Tomcat-NG: microsoft.public.office.developer.automation

Hi

I have written a couple of small programs which update text values on some
slides, for example a time... this value is update evrey second.
My first test application was written in Delphi and my second application
was in C# both seem to exhibit the same problems in the sense that
Powerpoint (2000) leaks some memory and also after a while powerpoint hangs!

Does anyone know if it is possible to do this reliably?

There is some example C# code below.

Regards

Mike


variables
private PowerPoint.ApplicationClass ppApp = new
PowerPoint.ApplicationClass();

private PowerPoint.Presentation ppPres;


initial connect code!
ppApp.Presentations.Open("c:\\pptest\\softboard.ppt",0,0,0);

ppPres = ppApp.Presentations.Item(1);

ppPres.SlideShowSettings.Run();



tidy up at end!

ppApp.Quit();



Round my loop!

PowerPoint.Slide S;

PowerPoint.Shape UpdateShape;

DateTime DT;

for (int i = 1; i <= ppPres.Slides.Count; i++)

{

S = ppPres.Slides.Item(i);

for (int j = 1; j <= S.Shapes.Count; j++)

{

UpdateShape = S.Shapes.Item(j);

if (UpdateShape.Name.IndexOf("SETIME") > -1)

{

DT = DateTime.Now;

UpdateShape.TextFrame.TextRange.Text = DT.ToString("T");

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top