【问题标题】:How to create an empty slide object?如何创建一个空的幻灯片对象?
【发布时间】:2015-06-24 11:08:33
【问题描述】:

我只是想通过这样做来制作一个空的幻灯片对象-

Microsoft.Office.Interop.PowerPoint.Slide empty_slide = new Microsoft.Office.Interop.PowerPoint.Slide();

错误是:

Unhandled Exception: System.Runtime.InteropServices.COMException: Retrieving the
 COM class factory for component with CLSID {91493445-5A91-11CF-8700-00AA0060263
B} failed due to the following error: 80040154 Class not registered (Exception f
rom HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)).
   at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOn
ly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Bo
olean& bNeedSecurityCheck)
   at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipChec
kThis, Boolean fillCache, StackCrawlMark& stackMark)
   at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean s
kipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
   at System.Activator.CreateInstance(Type type, Boolean nonPublic)
   at System.Activator.CreateInstance(Type type)
   at Program.GetBulletPointTransition(Application PowerPoint_App, Presentation
presentation) in c:\Users\PEAK\Documents\Peak Sourcing\Work\ppt_test\ppt_test\Pr
ogram.cs:line 539
   at Program.Main(String[] args) in c:\Users\PEAK\Documents\Peak Sourcing\Work\
ppt_test\ppt_test\Program.cs:line 79
Press any key to continue . . .

我将不胜感激。

【问题讨论】:

  • 你为什么将它设置为空? null 与空幻灯片不同!错误是什么?
  • 它说的是“未设置为对象的实例”。好的,我应该把它设置成什么?
  • var empty_slide = new Microsoft.Office.Interop.PowerPoint.Slide();
  • Null 是一个对象的缺失,而不是一个空对象的替代品(尽管不幸的是,它们有时会被这样使用)。

标签: c# powerpoint


【解决方案1】:

正确答案如下:

您不应直接创建 Microsoft.Office.Interop.PowerPoint.Slide 类的新实例。相反,请使用如下代码:

        Presentation ppt = pptApp.Presentations.Open(pptPath, WithWindow: MsoTriState.msoFalse);
        int newSlideNumber = (ppt.Slides.Count + 1);
        var slide = ppt.Slides.Add(newSlideNumber, PpSlideLayout.ppLayoutTitleOnly);
        slide.Shapes[1].TextFrame.TextRange.Text = "hello!";

注意:如果您查看相关论坛以获取问题的可用答案,则不应更改项目的构建设置以使用 x86 平台目标。

【讨论】:

    【解决方案2】:

    只需从您的第一个代码中删除 empty_slide = null; 部分。那应该这样做。 使用以下 -

    Microsoft.Office.Interop.PowerPoint.Slide empty_slide = new Microsoft.Office.Interop.PowerPoint.Slide();
    

    【讨论】:

    • 请查看更新后的代码。它仍然给出错误
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-10
    • 2013-08-16
    • 1970-01-01
    • 2011-08-11
    • 1970-01-01
    • 2022-01-20
    相关资源
    最近更新 更多