How to: Use Automation to create and display the PowerPoint presentation in Visual C # .NET (from MSDN)

xiaoxiao2021-03-06  59

For Microsoft Visual Basic .NET versions of this article, see

303717.

For Microsoft Visual C .NET versions of this article, see

308336.

SUMMARY This article gradually describes how to automatically run PowerPoint to create and display the PowerPoint presentation via Visual C # .NET to create and display the PowerPoint presentation.

More information

Create an automation client for PowerPoint

Start Microsoft Visual Studio .NET. On the File menu, click New, and then click Project. Select a Windows application from the Visual C # project type. FORM1 is created by default. Add a reference to the Microsoft PowerPoint object library and the Microsoft Graph object library. To do this, follow these steps:

On the project menu, click Add Reference. On the COM tab, find the Microsoft PowerPoint Object Library, and then click Select. You should also find the Microsoft Graph object library, and then click Select. Note: Microsoft Office XP does not contain PIA, but you can download PIA. For additional information about Office XP PIA, click the article number below to view the article in the Microsoft Knowledge Base: 328912 Info: Microsoft Office XP PIA is available for download in the Add Reference dialog box Click OK to accept your choice . On the View menu, select the Toolbox to display the toolbox, then add a button to Form1. Double click on Button1. The code window of the form will appear. In the code window, put the following code private void button1_click (Object Sender, System.EventArgs E)

{

}

Replace with: Private Void Button1_Click (Object Sender, System.EventArgs E)

{

Showpresentation ();

Gc.collect ();

}

Private void showpresentation ()

{

String strtemplate, strpic;

StRTEMPLATE =

"C: // program files // microsoft office // templates // presentation designs // Blends.pot";

StrPic = "c: // windows // blue lace 16.bmp";

Bool Bassistanton;

PowerPoint.Application Objapp;

PowerPoint.Presentation Objpresset

PowerPoint._Presentation Objpres;

PowerPoint.slides objslides;

PowerPoint._slide objslide;

PowerPoint.TextRange ObjTextrng;

PowerPoint.shapes objshapes;

PowerPoint.Shape Objshape;

PowerPoint.slideshowWindows objssws;

PowerPoint.slideshowtransition objsst;

PowerPoint.slideshowSettings objsss;

PowerPoint.SlideRange objsldrng;

Graph.Chart Objchart;

// Create a new presentation based on a template.objapp = new powerpoint.application ();

Objapp.visible = msotristate.msotrue;

Objpresset = objapp.presentation;

Objpres = Objpresset.open (Strtemplate,

Msotristate.msofalse, msotristate.msotrue, msotristate.msotrue;

Objslides = objpres.slides;

// Build Slide # 1:

// add text to the slide, change the font and insert / position a

// Picture on The First Slide.

Objslide = Objslides.add (1, PowerPoint.ppslideLayout.ppLayOuttitleOn ";

Objtextrng = objslide.shapes [1] .TextFrame.TextRange

Objtextrng.text = "My Sample Presentation";

Objtextrng.font.name = "COMIC SANS MS";

ObjTextrng.font.size = 48;

Objslide.shapes.addpicture (strpic, msotristate.msofalse, msotristate.msotrue,

150, 150, 500, 350);

// Build Slide # 2:

// add text to the slide title, format the text. Also add a chart to the

// Slide and change the chart type to a 3d Pie Chart.

Objslide = Objslides.add (2, PowerPoint.ppslideLayout.ppLayOuttitleOn ";

Objtextrng = objslide.shapes [1] .TextFrame.TextRange

ObjTextrng.Text = "My Chart";

Objtextrng.font.name = "COMIC SANS MS";

ObjTextrng.font.size = 48;

Objchart = (graph.chart) objslide.shapes.addoleObject (150, 150, 480, 320,

"Msgraph.Chart.8", "", Msotristate.msofalse, "" ", 0," ",

Msotristtate.msofalse) .oleFormat.Object;

Objchart.charttype = graph.xlcharttype.xl3dpie;

ObjChart.Legend.position = graph.xllegendPosition.xllegendPositionBottom

Objchart.haastitle = true;

Objchart.charttitle.text = "Here it is ...";

// Build Slide # 3:

// Change The Background Color of this Slide Only. Add A Text Effect To The Slide

// and Apply Various Color Schemes and Shadows To The Text Effect.Objslide = Objslides.add (3, PowerPoint.ppslideLayout.ppLayoutBlank);

Objslide.followmasterbackground = msotristate.msofalse;

Objshapes = objslide.shapes;

Objshape = objshapes.addtexteffect (msopResetTexTeffect.msTexTefect27,

"THE END", "Impact", 96, Msotristate.Msofalse, Msotristate.msofalse, 230, 200);

// MODIFY THE SLIDE SHOW Transition Settings for All 3 Slides in

////////////////////////////////////////>

Int [] slideidx = new int [3];

For (int i = 0; i <3; i ) slideidx [i] = i 1;

Objsldrng = objslides.range (slideidx);

Objsst = objsldrng.slideshowtransition;

Objsst.advanceontime = msotristate.msotrue;

Objsst.advancetime = 3;

Objsst.enTryeffect = PowerPoint.ppenTryeffect.ppeffectBoxout;

// Prevent Office Assistant from Displaying Alert Messages:

Bassiston = Objapp.assistant.on;

Objapp.assistant.on = false;

// Run The Slide Show from Slides 1 thru 3.

Objsss = Objpres.slideshowSetting;

Objss.startingslide = 1;

Objsss.ndingslide = 3;

Objsss.run ();

// Wait for the slide show to end.

Objssws = objapp.slideshowwindows;

While (objssws.count> = 1) system.threading.thread.sleep (100);

// Reenable Office Assisant, IF IT WAS ON:

IF (Bassistanton)

{

Objapp.assistant.on = true;

Objapp.assistant.visible = false;

}

// Close The Presentation without Saving Changes and Quit PowerPoint.

Objpres.close ();

Objapp.quit ();

}

Note: In the above code, STEMPLATE and SPIC constants represent the full path and file name of the PowerPoint template and the image, respectively. Follow these paths to modify these paths to use templates or images installed in your system. Scroll to the top of the code window. Add the following code line to the end of the Using command list: use Microsoft.Office.core;

Using powerpoint = microsoft.Office.Interop.powerpoint;

Using graph = microsoft.office.interop.graph; use system.Runtime.InteropServices;

Press F5 to build and run the program. Create and display the PowerPoint presentation in the form.

Refer to more information, please visit the Microsoft Web site below:

Microsoft Office Development with Visual Studio (using Visual Studio) http://msdn.microsoft.com/library/en-us/dnoffdev/html/vsofficeDev.asp About PowerPoint Automation, click below Article number to see the article in the Microsoft Knowledge Base:

180616 HOWTO: USE MFC TO CREATE AND SHOW A PowerPoint Presentation

222929 HOWTO: Automate PowerPoint Using Visual Basic

The information in this article applies to:

Microsoft Visual C # .NET (2002) Microsoft PowerPoint 2002

Recent Updated: 2004-2-13 (6.0) Keywords: Kbautomation Kbhowto KB303718 KBAUDDEVELOPER

转载请注明原文地址:https://www.9cbs.com/read-109055.html

New Post(0)