0

我面临一个问题,但我无法找到解决方案。我编写了一个程序,其中使用 C# 开发了带有两张幻灯片的 PowerPoint 演示文稿。当我运行该程序时,一切正常,但还创建了一个额外的空白 PowerPoint 演示文稿 ( Presentation2.pptx)。我无法理解为什么会创建一个空白演示文稿。请帮我解决这个问题。

下面是我的代码:

    private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {

        DataSet ds = new DataSet();

        SqlConnection con = new SqlConnection(@"Data Source=*****;Initial Catalog=******;User ID=****;Password=******");

        SqlCommand sqlComm = new SqlCommand("PowerPointTest", con);

        sqlComm.CommandType = CommandType.StoredProcedure;

        SqlDataAdapter da = new SqlDataAdapter();

        da.SelectCommand = sqlComm;

        da.Fill(ds);
        PowerPoint.Application pptApplication = new PowerPoint.Application();

        Microsoft.Office.Interop.PowerPoint.Slides slides;
        Microsoft.Office.Interop.PowerPoint._Slide slide;
        Microsoft.Office.Interop.PowerPoint._Slide slide1;
        Microsoft.Office.Interop.PowerPoint.TextRange objText;

        // Create the Presentation File
        PowerPoint.Presentation pptPresentation = pptApplication.Presentations.Add(Office.MsoTriState.msoTrue);

        Microsoft.Office.Interop.PowerPoint.CustomLayout customLayout = pptPresentation.SlideMaster.CustomLayouts[Microsoft.Office.Interop.PowerPoint.PpSlideLayout.ppLayoutText];

        // Create new Slide
        slides = pptPresentation.Slides;
        slide = slides.AddSlide(1, customLayout);
        slide1 = slides.AddSlide(2, customLayout);
        slide.ApplyTheme(@"C:\Program Files\Microsoft Office\Document Themes 14\Austin.thmx");
        slide1.ApplyTheme(@"C:\Program Files\Microsoft Office\Document Themes 14\Austin.thmx");
        // Add title
        objText = slide.Shapes[1].TextFrame.TextRange;
        objText.Text = "test";
        objText.Font.Name = "Arial";
        objText.Font.Size = 32;

        objText = slide1.Shapes[1].TextFrame.TextRange;
        objText.Text = "TEST1";
        objText.Font.Name = "Arial";
        objText.Font.Size = 40;

        objText = slide.Shapes[2].TextFrame.TextRange;
        string Username = Convert.ToString(ds.Tables[0].Rows[0]["UserName"]);
        string LoginID = Convert.ToString(ds.Tables[0].Rows[0]["LoginID"]);
        string EmailID1 = Convert.ToString(ds.Tables[0].Rows[0]["EmailID1"]);
        objText.Text = "UserName:" + Username +"\n LoginID:"+ LoginID +"\n Email ID:"+EmailID1+".";



        objText = slide1.Shapes[2].TextFrame.TextRange;
        objText.Text = "Content goes from here\nYou cannot add text\nItem 3";
        objText.Font.Size = 24;


        slide.NotesPage.Shapes[2].TextFrame.TextRange.Text = "Test";

        pptPresentation.SaveAs(@"c:\temp\test.pptx", Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsDefault, Office.MsoTriState.msoTrue);

    }

    private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
    {
    }

    #region VSTO generated code

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InternalStartup()
    {
        this.Startup += new System.EventHandler(ThisAddIn_Startup);
        this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
    }

    #endregion
}
4

1 回答 1

0

当您为 Office 应用程序创建加载项项目时,您的ThisAddIn类将有一个名为Application1的字段,它是 Office 应用程序的运行Application对象。您应该使用它来与正在运行的应用程序进行交互,而不是使用这一行:

PowerPoint.Application pptApplication = new PowerPoint.Application();

也就是说,您应该删除此行并将任何使用的 替换为pptApplicationjust Application


1该字段在文件中不可见,.cs但您应该能够在 Intellisense 中或通过展开解决方案资源管理器看到它:

在此处输入图像描述

于 2015-05-12T10:18:35.250 回答