当前位置:编程学习 > C#/ASP.NET >>

在vc2005 winform中如何打开Powerpoint??急急急!!!

我已经引用
using Microsoft.Office.core;
using Powerpoint;
但是当运行程序:
Powerpoint.Application ppa = new Powerpoint.ApplicationClass();
PowerPoint.Presentation ppp=ppa.Presentations.Open(@"E:\lhs.ppt",
                Microsoft.Office.Core.MsoTriState.msoTrue,
                Microsoft.Office.Core.MsoTriState.msoFalse,
                Microsoft.Office.Core.MsoTriState.msoFalse);
会出现“在调用COM组件时出错 hresult E_fail”的问题?!各位大虾帮帮啊!!急啊
--------------------编程问答-------------------- Mark --------------------编程问答-------------------- 给分啊!!!
各位帮帮忙啊!!! --------------------编程问答-------------------- 对于com组件不懂,只能帮你顶一下了 --------------------编程问答-------------------- 查了很多的帖子,都是通过PowerPoint.Presentation ppp=ppa.Presentations.Open  (@"E:\lhs.ppt",
                Microsoft.Office.Core.MsoTriState.msoTrue,
                Microsoft.Office.Core.MsoTriState.msoFalse,
                Microsoft.Office.Core.MsoTriState.msoFalse);
打开啊,为什么我的就出错了呢?是不是版本问题啊?
可是
ppa.visible = MsoTriState.msoTrue;却可以打开一个新的Powrepoint啊!!
郁闷啊!!
大家帮忙啊!!

--------------------编程问答-------------------- 添加引用了么? --------------------编程问答-------------------- using Microsoft.Office.core;
using Powerpoint;
并且已经在“引用”中加了Microsoft.Office.core的dll文件了
还是出错啊? --------------------编程问答-------------------- 这个帖子不要发了啊!!各位!!

为 PowerPoint 创建自动化客户端
1. 启动 Microsoft Visual Studio .NET。在文件菜单上,单击新建,然后单击项目。从 Visual C# 项目类型中选择 Windows 应用程序。默认情况下会创建 Form1。 
2. 添加对 Microsoft PowerPoint 对象库和 Microsoft Graph 对象库的引用。为此,请按照下列步骤操作: a.  在项目菜单上,单击添加引用。 
b.  在 COM 选项卡上,找到 Microsoft PowerPoint 对象库,然后单击选择。还应找到 Microsoft Graph 对象库,然后单击选择。

注意:Microsoft Office XP 不包含 PIA,但您可以下载 PIA。 有关 Office XP PIA 的其他信息,请单击下面的文章编号,以查看 Microsoft 知识库中相应的文章: 
328912 (http://support.microsoft.com/kb/328912/EN-US/) INFO:Microsoft Office XP PIA 可供下载  
c.  在添加引用对话框中单击确定以接受您的选择。 
 
3. 在视图菜单上,选择工具箱以显示工具箱,然后向 Form1 添加一个按钮。 
4. 双击 Button1。将出现该窗体的代码窗口。 
5. 在代码窗口中,将以下代码private void button1_Click(object sender, System.EventArgs e)
{
}

替换为: 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.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[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[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, "", 
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();


注意:在上述代码中,sTemplate 和 sPic 常量分别表示 PowerPoint 模板和图片的完整路径及文件名。按照需要修改这些路径以使用安装在您系统中的模板或图片。 
6. 滚动到代码窗口的顶部。将下面的代码行添加到 using 指令列表的末尾:using Microsoft.Office.Core;
using PowerPoint = Microsoft.Office.Interop.PowerPoint;
using Graph = Microsoft.Office.Interop.Graph;
using System.Runtime.InteropServices;

 
7. 按 F5 键生成并运行该程序。 
8. 在窗体中单击 Button1 创建并显示 PowerPoint 演示文稿。 
--------------------编程问答-------------------- 这个帖子也不用发了啊!!!大虾们给点意见啊!!


用编程的方式根据对象模型很容易实现在word、excel文档中搜索文本,在powerpoint里面也同样如此,使用对象模型有助于我们了解office的文档结构。
  
    搜索的思路和方法基本是一样的,用powerpoint应用程序对象打开指定的文档,用文档对象获取文档,再使用合适的对象将文档分割成搜索范围适中的对象进行搜索。
  
    打开powerpoint的vba帮助文档vbapp10.chm,根据对象模型图,很容易找到我们需要的几个集合和对象:application、presentations、presentation、slides、slide、textframe、textrange。其中presentation代表一个 powerpoint 文档,slide表示powerpoint文档中的单张幻灯片,textframe是幻灯片上的文本框,textrange是文本框中的文本。
  
    打开powerpoint文档:
  
  string filename="";
  
  powerpoint.application pa=new powerpoint.applicationclass();
  powerpoint.presentation pp=pa.presentations.open(filename,
   microsoft.office.core.msotristate.msotrue,
   microsoft.office.core.msotristate.msofalse,
   microsoft.office.core.msotristate.msofalse);
    open()方法的第三个参数在帮助文档中的说明如下:
  
    untitled 可选。msotristate 类型。指定文件是否有标题。
  
    因为是untitled,所以按照上面的代码,打开文档之后才能引用powerpoint文档的标题,如果不想使用标题,就要把枚举msofalse改成msotrue。
  搜索文本:
  
  string[] strkeywordlist={}; //要搜索的文本
  powerpoint.textrange otext;
  foreach(powerpoint.slide slide in pp.slides)
  {
   foreach(powerpoint.shape shape in slide.shapes)
   {
   foreach(string strkeyword in strkeywordlist)
   {
   otext=null;
   otext=shape.textframe.textrange.find(strkeyword,0,microsoft.office.core.msotristate.msofalse,microsoft.office.core.msotristate.msotrue);
   if (otext!=null)
   {
   messagebox.show("文档中包含指定的关键字 "+strkeyword+" !","搜索结果",messageboxbuttons.ok);
   continue;
   }
   }
   }
  } 
--------------------编程问答-------------------- 拜托,帮你试了这么久,搞得又不要了,昏到家;
if (DialogResult.OK == openFileDialog2.ShowDialog())
{
                Microsoft.Office.Interop.PowerPoint.Application ppa = new Microsoft.Office.Interop.PowerPoint.ApplicationClass();
                ppa.Visible = MsoTriState.msoTrue;//少这个了啊
                Presentation ppp = ppa.Presentations.Open(openFileDialog2.FileName, MsoTriState.msoTrue, MsoTriState.msoTrue, MsoTriState.msoTrue);
}
//openFileDialog2是打开对话框 --------------------编程问答-------------------- 还有引用的是Microsoft.Office.Interop.PowerPoint
不是using Powerpoint啊.受到你的小误导,搞这么久,呵呵. --------------------编程问答-------------------- Microsoft.Office.Interop.PowerPoint
这个东西没办法引入啊!!
if (DialogResult.OK == openFileDialog2.ShowDialog())
{
                Microsoft.Office.Interop.PowerPoint.Application ppa = new Microsoft.Office.Interop.PowerPoint.ApplicationClass();
                ppa.Visible = MsoTriState.msoTrue;//少这个了啊
                Presentation ppp = ppa.Presentations.Open(openFileDialog2.FileName, MsoTriState.msoTrue, MsoTriState.msoTrue, MsoTriState.msoTrue);
}
我用了,可能是没有引用Microsoft.Office.Interop.PowerPoint
的问题,没有成功啊!!
我的怎么引用不进来啊? --------------------编程问答-------------------- 难道是因为我用的是vc2005??? --------------------编程问答-------------------- 老大别走啊?? --------------------编程问答-------------------- String strFileName;

            //Find the Office document.
            openFileDialog1.FileName = "";
            openFileDialog1.ShowDialog();
            strFileName = openFileDialog1.FileName;

            //If the user does not cancel, open the document.
            if (strFileName.Length != 0)
            {
                Object refmissing = System.Reflection.Missing.Value;
                oDocument = null;
                axWebBrowser1.Navigate(strFileName, ref refmissing, ref refmissing, ref refmissing, ref refmissing);
            }

word,excel,ppt都可以打开 --------------------编程问答-------------------- 在MSDN里有例子,但只对office2003及以前的有用,对office2007就没用了 --------------------编程问答-------------------- 学习 --------------------编程问答-------------------- myoldtrafford()
老大,这样是在webBrowser中打开啊!!
我们的要求是在Powerpoint中打开呀??!!
必须先new一个Powerpoint再用这个Powerpoint打开才行啊!!
大家帮帮忙啊!!

急急急!! --------------------编程问答-------------------- 各位大虾帮帮忙啊!!
难道就没有高手能完成winform中打开Powerpoint的问题吗?

急急急!!


有分拿啊!! --------------------编程问答-------------------- up --------------------编程问答-------------------- 各位大虾帮帮忙啊!!
难道就没有高手能完成winform中打开Powerpoint的问题吗?

急急急!!
--------------------编程问答-------------------- PowerPoint.Application ppa = new PowerPoint.ApplicationClass();
ppa.Visible = MsoTriState.msoTrue                
Presentation ppp = ppa.Presentations.Open(pptFileName, MsoTriState.msoTrue, MsoTriState.msoTrue, MsoTriState.msoTrue);

应该是可以实现的?!
是不是你用了vc2005的问题啊??

学习
--------------------编程问答-------------------- 各位大虾帮帮忙啊!!
难道就没有高手能完成winform中打开Powerpoint的问题吗?

急急急!!

--------------------编程问答-------------------- Mark --------------------编程问答-------------------- 各位大虾帮帮忙啊!!
难道就没有高手能完成winform中打开Powerpoint的问题吗?

急急急!!
--------------------编程问答-------------------- 各位大虾帮帮忙啊!!
难道就没有高手能完成winform中打开Powerpoint的问题吗?
--------------------编程问答-------------------- 学习 --------------------编程问答-------------------- 还是没有答案啊??!! --------------------编程问答-------------------- 高手都去哪了啊??
帮帮忙啊!! --------------------编程问答-------------------- lhsdechuntian() 的方法不是你要的吗?具体想干什么?MSDN有例子。
--------------------编程问答-------------------- MSDN的例子,总是报错啊
会出现“在调用COM组件时出错 hresult E_fail”的问题?! --------------------编程问答-------------------- 最近几天每什么人来了啊??
我的问题啊!!
可怜的人啊!!
--------------------编程问答-------------------- 各位大虾帮帮忙啊!!
难道就没有高手能完成winform中打开Powerpoint的问题吗?
--------------------编程问答-------------------- 学习 --------------------编程问答-------------------- 晕啊 --------------------编程问答-------------------- 无奈 --------------------编程问答-------------------- mark,学习 --------------------编程问答-------------------- 我最近也在做,能在Winform中打开PowerPoint,但是不显示文档。真是奇怪。也不报错。谁知我我这是什么原因? --------------------编程问答--------------------
using System.Reflection;
//using Access = Microsoft.Office.Interop.Access;
//using Excel = Microsoft.Office.Interop.Excel;
using PowerPoint = Microsoft.Office.Interop.PowerPoint;
//using Word = Microsoft.Office.Interop.Word; --------------------编程问答-------------------- 我也遇到同样的问题了。。。 --------------------编程问答-------------------- 我做的是在vs2005中打开Excel的例子,也是历经重重困难搞定的 --------------------编程问答-------------------- Process.Start("D:\1.ppt");
不知道楼主还在不在。还有别的方法我正在写~这都2012年了 --------------------编程问答-------------------- using System;

using System.Collections.Generic;
using System.Linq;
using System.Text;
using OFFICECORE = Microsoft.Office.Core;
using POWERPOINT = Microsoft.Office.Interop.PowerPoint;
using System.Windows;
using System.Collections;

namespace wt
{
    public class Pro
    {
        #region=========基本的参数信息=======
        POWERPOINT.Application objApp = null;
        POWERPOINT.Presentation objPresSet = null;
        POWERPOINT.SlideShowWindows objSSWs;
        POWERPOINT.SlideShowTransition objSST;
        POWERPOINT.SlideShowSettings objSSS;
        POWERPOINT.SlideRange objSldRng;

        bool bAssistantOn;
        double pixperPoint = 0;
        double offsetx = 0;
        double offsety = 0;
        #endregion

        #region===========操作方法==============
        /// <summary>
        /// 打开PPT文档并播放显示。
        /// </summary>
        /// <param name="filePath">PPT文件路径</param>
        public void PPTOpen(string filePath)
        {
            //防止连续打开多个PPT程序.
            //if (this.objApp != null) { return; }
            try
            {
                objApp = new POWERPOINT.Application();
                //以非只读方式打开,方便操作结束后保存.
                objPresSet = objApp.Presentations.Open(filePath, OFFICECORE.MsoTriState.msoFalse, OFFICECORE.MsoTriState.msoFalse, OFFICECORE.MsoTriState.msoFalse);

                //Prevent Office Assistant from displaying alert messages:
                bAssistantOn = objApp.Assistant.On;
                objApp.Assistant.On = false;

                objSSS = this.objPresSet.SlideShowSettings;
                objSSS.Run();
            }
            catch (Exception ex)
            {
                this.objApp.Quit();
            }
        }
///// <summary>
        ///// 自动播放PPT文档.
        ///// </summary>
        ///// <param name="filePath">PPTy文件路径.</param>
        ///// <param name="playTime">翻页的时间间隔.【以秒为单位】</param>
        //public void PPTAuto(string filePath, int playTime)
        //{
        //    //防止连续打开多个PPT程序.
        //    if (this.objApp != null) { return; }

        //    objApp = new POWERPOINT.Application();
        //    objPresSet = objApp.Presentations.Open(filePath, OFFICECORE.MsoTriState.msoCTrue, OFFICECORE.MsoTriState.msoFalse, OFFICECORE.MsoTriState.msoFalse);

        //    // 自动播放的代码(开始)
        //    int Slides = objPresSet.Slides.Count;
        //    int[] SlideIdx = new int[Slides];
        //    for (int i = 0; i < Slides; i++) { SlideIdx[i] = i + 1; };
        //    objSldRng = objPresSet.Slides.Range(SlideIdx);
        //    objSST = objSldRng.SlideShowTransition;
        //    //设置翻页的时间.
        //    objSST.AdvanceOnTime = OFFICECORE.MsoTriState.msoCTrue;
        //    objSST.AdvanceTime = playTime;
        //    //翻页时的特效!
        //    objSST.EntryEffect = POWERPOINT.PpEntryEffect.ppEffectCircleOut;

        //    //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 = objPresSet.SlideShowSettings;
        //    objSSS.StartingSlide = 1;
        //    objSSS.EndingSlide = Slides;
        //    objSSS.Run();

        //    //Wait for the slide show to end.
        //    objSSWs = objApp.SlideShowWindows;
        //    while (objSSWs.Count >= 1) System.Threading.Thread.Sleep(playTime * 100);

        //    this.objPresSet.Close();
        //    this.objApp.Quit();
        //}
#endregion
    }
}
调用这个方法就行。如果你无法using POWERPOINT = Microsoft.Office.Interop.PowerPoint;你重新装过office的话能行,当然还有更好的方法
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,