0

問題に直面していますが、解決策を見つけることができません。C# を使用して 2 つのスライドを含む 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クラスには、Office アプリケーションの実行中のオブジェクトであるApplication1というフィールドが作成されます。Application次の行ではなく、実行中のアプリケーションと対話するためにこれを使用する必要があります。

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

つまり、この行を削除し、 の使用pptApplicationApplication.


1フィールドは.csファイルに表示されませんが、Intellisense で、またはソリューション エクスプローラーを展開することで表示できるはずです。

ここに画像の説明を入力

于 2015-05-12T10:18:35.250 に答える