Identificativo articolo: 303718 - Ultima modifica: venerdì 29 giugno 2007 - Revisione: 5.2 How to Utilizzare l'automazione per creare e visualizzare una presentazione di PowerPoint con Visual C# .NETQuesto articolo è stato precedentemente pubblicato con il codice di riferimento I303718
Per la versione di questo articolo relativa a Microsoft Visual Basic .NET, vedere il seguente articolo della Microsoft Knowledge Base (gli articoli con prefisso "Q" contengono informazioni in inglese): 303717
(http://support.microsoft.com/kb/303717/
)
.
Per la versione di questo articolo relativa a Microsoft Visual C++ .NET, vedere il seguente articolo della Microsoft Knowledge Base (gli articoli con prefisso "Q" contengono informazioni in inglese): 308336
(http://support.microsoft.com/kb/308336/
)
.
In questo articolo viene spiegato come automatizzare PowerPoint con Visual C# .NET per creare e visualizzare una presentazione di PowerPoint.
Creare un client di automazione per PowerPoint
-
Avviare Microsoft Visual Studio .NET. Scegliere
Nuovo
dal menu
File, quindi scegliere
Progetto. Selezionare
Applicazione Windows
dai tipi di progetto Visual C#. In base all'impostazione predefinita viene creato il progetto Form1.
-
Aggiungere un riferimento alla
Libreria oggetti di Microsoft PowerPoint
e alla
Libreria oggetti di Microsoft Graph. Per eseguire questa operazione, attenersi alla seguente procedura:
-
Scegliere
Aggiungi riferimento
dal menu
Progetto.
-
Nella scheda
COM
individuare
Microsoft PowerPoint Object Library
e scegliere
Seleziona. Individuare inoltre
Microsoft Graph Object Library
e scegliere
Seleziona.
NOTA: le Librerie oggetti di Microsoft PowerPoint e Microsoft Graph contengono un numero di versione. Il numero di versione per PowerPoint 2000 e Graph 2000 è 9.0, mentre quello per PowerPoint 2002 e Graph 2002 è 10.0.
-
Scegliere
OK
nella finestra di dialogo
Aggiungi riferimento
per confermare le selezioni. Se viene richiesto di generare wrapper per le librerie selezionate, scegliere
Sì.
-
Scegliere
Casella degli strumenti
dal menu
Visualizza
per visualizzare la Casella degli strumenti e aggiungere un pulsante a Form1.
-
Fare doppio clic su
Button1. Verrà visualizzata la finestra del codice del form.
-
Nella finestra del codice sostituire il codice seguente
private void button1_Click(object sender, System.EventArgs e)
{
}
con:
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:\\WINNT\\Soap Bubbles.bmp";
bool bAssistantOn;
PowerPoint.Application objApp;
PowerPoint.Presentations 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.Presentations;
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.ppLayoutTitleOnly);
objTextRng = objSlide.Shapes.Item(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.ppLayoutTitleOnly);
objTextRng = objSlide.Shapes.Item(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", "", MsoTriState.msoFalse, "", 0, "",
MsoTriState.msoFalse).OLEFormat.Object;
objChart.ChartType = Graph.XlChartType.xl3DPie;
objChart.Legend.Position=Graph.XlLegendPosition.xlLegendPositionBottom;
objChart.HasTitle = 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.msoTextEffect27,
"The End", "Impact", 96, MsoTriState.msoFalse, MsoTriState.msoFalse, 230, 200);
//Modify the slide show transition settings for all 3 slides in
//the presentation.
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:
bAssistantOn = objApp.Assistant.On;
objApp.Assistant.On = false;
//Run the Slide show from slides 1 thru 3.
objSSS = objPres.SlideShowSettings;
objSSS.StartingSlide = 1;
objSSS.EndingSlide = 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();
}
NOTA: in questo codice le costanti
sTemplate
e
sPic
rappresentano rispettivamente il percorso e il nome completo di un modello di PowerPoint e di un'immagine. Modificare questi percorsi come appropriato per utilizzare un modello o un'immagine installati nel computer.
-
con:
Scorrere verso l'alto la pagina del codice. Aggiungere la riga seguente alla fine dell'elenco di direttive
using.
Per PowerPoint 2002:
using Microsoft.Office.Core; //Office XP
using System.Runtime.InteropServices;
Per PowerPoint 2000:
using Office; //Office 2000
using System.Runtime.InteropServices; -
con:
Premere F5 per compilare ed eseguire il progetto.
-
con:
Scegliere
Button1
nel form per creare e visualizzare una presentazione di PowerPoint.
(c) Microsoft Corporation 2001, Tutti i diritti riservati. Con il contributo di Lori B. Turner, Microsoft Corporation.
Per ulteriori informazioni, visitare il seguente sito Web Microsoft Developer Network (MSDN) (informazioni in lingua inglese):
Per ulteriori informazioni sull'automazione di PowerPoint, fare clic sui numeri degli articoli della Microsoft Knowledge Base riportati di seguito (gli articoli con prefisso "Q" contengono informazioni in inglese):
180616
(http://support.microsoft.com/kb/180616/
)
How to Use MFC to Create and Show a PowerPoint Presentation
222929
(http://support.microsoft.com/kb/222929/
)
How to Automate PowerPoint Using Visual Basic
Le informazioni in questo articolo si applicano a- Microsoft Visual C# .NET 2002 Standard Edition
- Microsoft PowerPoint 2000 Standard Edition
- Microsoft PowerPoint 2002 Standard Edition
| kbhowto kbautomation kbgrpdso KB303718 |
LE INFORMAZIONI CONTENUTE NELLA MICROSOFT KNOWLEDGE BASE SONO FORNITE SENZA GARANZIA DI ALCUN TIPO, IMPLICITA OD ESPLICITA, COMPRESA QUELLA RIGUARDO ALLA COMMERCIALIZZAZIONE E/O COMPATIBILITA' IN IMPIEGHI PARTICOLARI. L'UTENTE SI ASSUME L'INTERA RESPONSABILITA' PER L'UTILIZZO DI QUESTE INFORMAZIONI. IN NESSUN CASO MICROSOFT CORPORATION E I SUOI FORNITORI SI RENDONO RESPONSABILI PER DANNI DIRETTI, INDIRETTI O ACCIDENTALI CHE POSSANO PROVOCARE PERDITA DI DENARO O DI DATI, ANCHE SE MICROSOFT O I SUOI FORNITORI FOSSERO STATI AVVISATI. IL DOCUMENTO PUO' ESSERE COPIATO E DISTRIBUITO ALLE SEGUENTI CONDIZIONI: 1) IL TESTO DEVE ESSERE COPIATO INTEGRALMENTE E TUTTE LE PAGINE DEVONO ESSERE INCLUSE. 2) I PROGRAMMI SE PRESENTI, DEVONO ESSERE COPIATI SENZA MODIFICHE, 3) IL DOCUMENTO DEVE ESSERE DISTRIBUITO INTERAMENTE IN OGNI SUA PARTE. 4) IL DOCUMENTO NON PUO' ESSERE DISTRIBUITO A SCOPO DI LUCRO. | |