Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在VISUAL STUDIO 2010中运行时,将打开两个PPT演示文稿,而不是一个_C#_Visual Studio 2010_Powerpoint 2010 - Fatal编程技术网

C# 在VISUAL STUDIO 2010中运行时,将打开两个PPT演示文稿,而不是一个

C# 在VISUAL STUDIO 2010中运行时,将打开两个PPT演示文稿,而不是一个,c#,visual-studio-2010,powerpoint-2010,C#,Visual Studio 2010,Powerpoint 2010,我面临一个问题,但我无法找到解决办法。我编写了一个程序,其中使用C#开发了一个包含两张幻灯片的PowerPoint演示文稿。当我运行程序时,一切正常,但也会创建一个额外的空白PowerPoint演示文稿(Presentation2.pptx)。我无法理解为什么会创建空白演示文稿。请帮我解决这个问题 下面是我的代码: private void ThisAddIn_Startup(object sender, System.EventArgs e) { DataSe

我面临一个问题,但我无法找到解决办法。我编写了一个程序,其中使用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
}
private void ThisAddIn\u启动(对象发送方,System.EventArgs e)
{
数据集ds=新数据集();
SqlConnection con=newsqlconnection(@“数据源=****;初始目录=****;用户ID=****;密码=*******”);
SqlCommand sqlComm=新的SqlCommand(“PowerPointTest”,con);
sqlComm.CommandType=CommandType.StoredProcess;
SqlDataAdapter da=新的SqlDataAdapter();
da.SelectCommand=sqlComm;
da.填充(ds);
PowerPoint.Application pptApplication=新的PowerPoint.Application();
Microsoft.Office.Interop.PowerPoint.Slides;
Microsoft.Office.Interop.PowerPoint.\u幻灯片;
Microsoft.Office.Interop.PowerPoint.\u幻灯片1;
Microsoft.Office.Interop.PowerPoint.TextRange objText;
//创建演示文稿文件
PowerPoint.Presentation-pptPresentation=pptApplication.Presentations.Add(Office.MsoTriState.msoTrue);
Microsoft.Office.Interop.PowerPoint.CustomLayout CustomLayout=pptPresentation.SlideMaster.CustomLayouts[Microsoft.Office.Interop.PowerPoint.PpSlideLayout.ppLayoutText];
//创建新幻灯片
幻灯片=PPT演示文稿。幻灯片;
幻灯片=幻灯片。添加幻灯片(1,自定义布局);
slide1=slides.AddSlide(2,自定义布局);
幻灯片.ApplyTheme(@“C:\Program Files\Microsoft Office\Document Themes 14\Austin.thmx”);
幻灯片1.ApplyTheme(@“C:\Program Files\Microsoft Office\Document Themes 14\Austin.thmx”);
//添加标题
objText=slide.Shapes[1].TextFrame.TextRange;
objText.Text=“测试”;
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;
字符串Username=Convert.ToString(ds.Tables[0].行[0][“Username”]);
字符串LoginID=Convert.ToString(ds.Tables[0].行[0][“LoginID”]);
字符串EmailID1=Convert.ToString(ds.Tables[0].行[0][“EmailID1]”);
objText.Text=“用户名:”+UserName+“\n LoginID:“+LoginID+”\n电子邮件ID:“+EmailID1+”;
objText=slide1.Shapes[2].TextFrame.TextRange;
objText.Text=“内容从此处开始\n您无法添加文本\n项3”;
objText.Font.Size=24;
slide.NotesPage.Shapes[2].TextFrame.TextRange.Text=“测试”;
pptPresentation.SaveAs(@“c:\temp\test.pptx”,Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsDefault,Office.MsoTriState.msoTrue);
}
私有void ThisAddIn_关闭(对象发送方,System.EventArgs e)
{
}
#区域VSTO生成的代码
/// 
///设计器支持所需的方法-不修改
///此方法的内容与代码编辑器一起使用。
/// 
私有void InternalStartup()
{
this.Startup+=new System.EventHandler(ThisAddIn\u启动);
this.Shutdown+=new System.EventHandler(ThisAddIn\u Shutdown);
}
#端区
}

为Office应用程序创建外接程序项目时,您的
ThisAddIn
类将有一个名为
Application
1的字段,它是Office应用程序正在运行的
Application
对象。您应该使用此命令与正在运行的应用程序交互,而不是使用以下命令行:

PowerPoint.Application pptApplication = new PowerPoint.Application();
也就是说,您应该删除此行,并将
pptApplication
的任何用法替换为
Application


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


我找不到你。你能详细说明一下吗?我对这行做了评论:PowerPoint.Application pptApplication=new PowerPoint.Application();并代替PowerPoint.Presentation-pptPresentation=pptApplication.Presentations.Add(Office.MsoTriState.msoTrue);我把它改为PowerPoint.Presentation-pptPresentation=Application.Presentations.Add(Office.MsoTriState.msoTrue);但在我运行程序后,仍然有两个ppt文件正在打开。